【发布时间】:2017-10-26 11:44:25
【问题描述】:
您好,我的脚本无法正常运行:
即使 powershell 版本高于4,它在第一次写入输出时仍然失败。它仅在我删除 And $winver -eq $os1 -or $os2 -or $os3 时有效。
否则它一直告诉我我的 powershell 版本需要升级。我目前在 V5 上,$PSVersionTable.PSVersion.Major 确实说过5。
我做错了什么?
$winver = (Get-WmiObject -class Win32_OperatingSystem).Caption
$powershellversion = $PSVersionTable.PSVersion.Major
$os1 = "Microsoft Windows 7 Professional"
$os2 = "Microsoft Windows 10 Pro"
$os3 = "Microsoft Windows 10 Enterprise"
if($winver -ne ($os1, $os2, $os3) -contains $winver){
Write-Host "Bitlocker not supported on $winver"
Exit 0
}
if($powershellversion -lt 4){
Write-Host "Upgrade Powershell Version"
Exit 1010
}
else
{
$bitlockerkey = (Get-BitLockerVolume -MountPoint C).KeyProtector.RecoveryPassword
$pcsystemtype = (Get-WmiObject -Class Win32_ComputerSystem).PCSystemType
if ($pcsystemtype -eq "2"){
$setsystemtype = "Laptop"
}
else {
$setsystemtype = "Desktop"
}
if ($setsystemtype -eq "laptop" -And $bitlockerkey -eq $null -and ($os1, $os2, $os3) -contains $winver){
Write-Host "$setsystemtype without bitlocker"
Exit 1010
}
if ($setsystemtype -eq "desktop" -And $bitlockerkey -eq $null -and ($os1, $os2, $os3) -contains $winver){
Write-Host "$setsystemtype without bitlocker"
Exit 0
}
if ($winver -eq ($os1, $os2, $os3) -contains $winver){
Write-Host "$bitlockerkey"
Exit 0
}
}
【问题讨论】:
-
` if ($powershellversion -lt 4 -And $winver -eq $os1 -or $os2 -or $os3){` 如果是Windows上的PoSh4呢8? ;) 没有?比你为什么要同时检查操作系统和 PowreShell 版本?
-
尝试嵌套一些 if 语句并考虑使用Switch
-
哦,下一个:不是
proffesional,是professional。但是为什么你会认为这有什么不同吗? -
Windows 8只有企业版有bitlocker,我们没有win 8企业的客户,所以我们可以跳过。除了 os 1 2 和 3 之外的任何东西都应该在最新的行中输出。 Powershell 版本检查应该在 powershell 3 之前进行,没有 get bilickervolume 命令。我会研究开关,但我必须为此重新学习很多东西。
-
只需检查 BitLocker 和它的 CmdLets 是否可用...比如
if (Get-Command -Name "Get-BitLockerVolume" -ErrorAction SilentlyContinue) {}然后如果可用,请使用Get-BitLockerVolume检查是否有加密卷。
标签: powershell if-statement operator-precedence bitlocker