我正在尝试通过powershell脚本远程验证Win10升级结果。
我构建了这段代码,并运行Invoke-Command -FilePath C:\validate.ps1 -ComputerName remotePC
我的问题是,如果OS版本是win10,那么继续进行剩余的验证。
如果操作系统为win7,则不进行剩余的验证。有什么需要帮忙的吗?非常感谢。
#Auto-validate the Win10 upgrade result
# Get PC name
$PCname = $env:computername
# Check OS Edition
$OSEdition = (Get-WmiObject Win32_OperatingSystem).name
if ($OSEdition -match '10') {
Write-Host "$PCname Win10" -ForegroundColor Green
}
else {
Write-Host "$PCname Win7" -ForegroundColor Red
}
# System Locale
$locale = (Get-WinSystemLocale).Name
if ($locale -eq 'US') {
Write-Host "$PCname locale is correct" -ForegroundColor Green
}
else {
Write-Host "$PCname locale is wrong" -ForegroundColor Red
}
# Check printer status
Get-Printer | Format-Table ComputerName,Name,DriverName,PrinterStatus
发布于 2020-05-14 00:13:54
如果要结束在当前作用域(当前函数或脚本)中的执行,请使用return
关键字,从而使PowerShell将控制权返回给调用方:
if($OSEdition -notmatch '10'){
# OS Name doesn't contain '10', let's return!
return
}
对于版本检测,我建议查看Win32_OperatingSystem
的Version
属性,而不是操作系统名称:
$OSVersion = (Get-WmiObject Win32_OperatingSystem).Version
if($OSVersion -notlike '10.*'){
return
}
要了解有关return
关键字的更多信息,请查看about_Return
help file!
https://stackoverflow.com/questions/61778719
复制相似问题