Power Shell脚本检查和修复一些问题

1. 查看.Net Framework 版本号:

$release = Get-ItemPropertyValue -LiteralPath 'HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Release
switch ($release) {
    { $_ -ge 533320 } { $version = '4.8.1 or later'; break }
    { $_ -ge 528040 } { $version = '4.8'; break }
    { $_ -ge 461808 } { $version = '4.7.2'; break }
    { $_ -ge 461308 } { $version = '4.7.1'; break }
    { $_ -ge 460798 } { $version = '4.7'; break }
    { $_ -ge 394802 } { $version = '4.6.2'; break }
    { $_ -ge 394254 } { $version = '4.6.1'; break }
    { $_ -ge 393295 } { $version = '4.6'; break }
    { $_ -ge 379893 } { $version = '4.5.2'; break }
    { $_ -ge 378675 } { $version = '4.5.1'; break }
    { $_ -ge 378389 } { $version = '4.5'; break }
    default { $version = $null; break }
}

if ($version) {
    Write-Host -Object ".NET Framework Version: $version"
} else {
    Write-Host -Object '.NET Framework Version 4.5 or later is not detected.'
}
cmd /pause

2. Windows OS升级后,SQL Server配置管理工具从开始菜单里消失且在安装目录下执行exe文件也打不开

function createShortcut {
    param ([string]$TargetFile, [string]$ShortcutFile)
    $WScriptShell = New-Object -ComObject WScript.Shell
    $Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
    $Shortcut.TargetPath = $TargetFile
    $Shortcut.Save()
    echo 'Created SQL Server Configuration Manager shortcut'
}

#mannual input sql server version
#$SQLVersion= Read-Host -Prompt "Input SQL Server Version"
#Write-Host $SQLVersion
Write-Host $env:COMPUTERNAME

#Get the SQL Server version dynamically
$result=Invoke-Sqlcmd -ServerInstance $env:COMPUTERNAME -Database master -Query "Select @@version"

foreach($row in $result){
$SQLVersion=$row[0].Substring(21,4)
Write-Host $SQLVersion
}

switch($SQLVersion){
    "2016" {
        $SQLNumber='130';$SQLManagerNumber='13';break
    }
    "2014" {
        $SQLNumber='120';$SQLManagerNumber='12';break
    }
    "2017" {
        $SQLNumber='140';$SQLManagerNumber='14';break
    }
    default {
        $SQLNumber='130';$SQLManagerNumber='13';break
    }
}
Write-Host $SQLnumber
#fix the error when open the sql server configuration manager
mofcomp.exe "C:\Program Files (x86)\Microsoft SQL Server\$SQLNumber\Shared\sqlmgmproviderxpsp2up.mof"

#create sql server configuration manager shortcut
createShortcut "C:\Windows\SysWOW64\SQLServerManager$SQLManagerNumber.msc" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft SQL Server $SQLVersion\Configuration Tools\SQL Server $SQLVersion Configuration Manager.lnk"

cmd /c pause


 3. 禁用防火墙

#Disable firewall
Set-NetFirewallProfile -Enabled False
echo "Prese Enter key"
pause

4. 启用防火墙

# enable firewall
Set-NetFirewallProfile -Enabled True
echo "Prese Enter key"
pause

5. 打开防火墙开关窗口:firewall.cpl      打开防火墙入站出站规则窗口:wf.msc

6. 查看服务器上的证书(LocalComputer / Current User / Root)

   Get-ChildItem Cert:\LocalMachine\ -Recurse | where{$_.Subject -like "*XXX*"}
7. 获取服务器即将过期的证书

$invokeCommandSplat = @{
ComputerName = 'servername'
ScriptBlock = {
Get-ChildItem -Path cert:\* -Recurse -ExpiringInDays 30
}
}
Invoke-Command @invokeCommandSplat

posted on 2023-03-11 15:24  追求高级技术  阅读(57)  评论(0编辑  收藏  举报