Powershell OS 在 运行 使用 IF 语句的命令之前检查
Powershell OS check before running a command using an IF statement
有没有办法检查 OS PowerShell 脚本 运行 是否开启,并创建一个 If
声明:
[伪代码]
if its this OS
do this
if its this other OS
do this
只针对脚本中的某一行?我必须制作一个设置私人消息队列的 PowerShell 脚本。遗憾的是,我公司的一些客户不使用 Windows Server 2012,因此添加私人消息队列的更简单版本的方式在 Windows Server 2008 和过时的 PowerShell 版本上不起作用。为了解决这个问题,我还必须改用非常复杂的旧方法,但我希望这两种方法都存在。
开关会更干净,但既然你问过如何以特定方式做到这一点...
设置变量以检查 OS 版本(从链接线程 Get operating system without using WMI 中提取):
$OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
构造您的 IF 语句:
If($OSVersion -eq "Windows Server 2008 R2 Standard")
{
Write-Host "Hooray It's Server 2K8 r2!"
Invoke-Item "C:\Pictures\Hooray.jpg"
}
ElseIf($OSVersion -eq "Windows 7 Professional")
{
Write-Host "Okay, Windows 7 is cool, too!"
Invoke-Item "C:\Pictures\Smiley.jpg"
}
ElseIf($OSVersion -eq "Windows Vista")
{
Write-Host "What have I done with my life?!"
Invoke-Item "C:\Pictures\GunToHead.jpg"
}
ElseIf($OSVersion -eq "Windows Millennium Edition")
{
Write-Host "Go away, operating system. You are drunk."
Invoke-Item "C:\Pictures\LiquorAndHiccups.jpg"
}
希望对您有所帮助。我假设您是 PowerShell 的新手,但当您熟悉后,就可以开始学习开关了。
对于其他不熟悉 switches here's the Switch statement equivalent of @Nate's answer for comparison. There's also more advanced 方法的人,您可以选择它们(包括正则表达式),如果它们变得非常复杂,您可以将测试移至命名良好的函数
$OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
switch ($OSVersion)
{
"Windows Server 2008 R2 Standard"
{
Write-Host "Hooray It's Server 2K8 r2!"
Invoke-Item "C:\Pictures\Hooray.jpg"
}
"Windows 7 Professional"
{
Write-Host "Okay, Windows 7 is cool, too!"
Invoke-Item "C:\Pictures\Smiley.jpg"
}
"Windows Vista"
{
Write-Host "What have I done with my life?!"
Invoke-Item "C:\Pictures\GunToHead.jpg"
}
"Windows Millennium Edition"
{
Write-Host "Go away, operating system. You are drunk."
Invoke-Item "C:\Pictures\LiquorAndHiccups.jpg"
}
}
有没有办法检查 OS PowerShell 脚本 运行 是否开启,并创建一个 If
声明:
[伪代码]
if its this OS
do this
if its this other OS
do this
只针对脚本中的某一行?我必须制作一个设置私人消息队列的 PowerShell 脚本。遗憾的是,我公司的一些客户不使用 Windows Server 2012,因此添加私人消息队列的更简单版本的方式在 Windows Server 2008 和过时的 PowerShell 版本上不起作用。为了解决这个问题,我还必须改用非常复杂的旧方法,但我希望这两种方法都存在。
开关会更干净,但既然你问过如何以特定方式做到这一点...
设置变量以检查 OS 版本(从链接线程 Get operating system without using WMI 中提取):
$OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
构造您的 IF 语句:
If($OSVersion -eq "Windows Server 2008 R2 Standard")
{
Write-Host "Hooray It's Server 2K8 r2!"
Invoke-Item "C:\Pictures\Hooray.jpg"
}
ElseIf($OSVersion -eq "Windows 7 Professional")
{
Write-Host "Okay, Windows 7 is cool, too!"
Invoke-Item "C:\Pictures\Smiley.jpg"
}
ElseIf($OSVersion -eq "Windows Vista")
{
Write-Host "What have I done with my life?!"
Invoke-Item "C:\Pictures\GunToHead.jpg"
}
ElseIf($OSVersion -eq "Windows Millennium Edition")
{
Write-Host "Go away, operating system. You are drunk."
Invoke-Item "C:\Pictures\LiquorAndHiccups.jpg"
}
希望对您有所帮助。我假设您是 PowerShell 的新手,但当您熟悉后,就可以开始学习开关了。
对于其他不熟悉 switches here's the Switch statement equivalent of @Nate's answer for comparison. There's also more advanced 方法的人,您可以选择它们(包括正则表达式),如果它们变得非常复杂,您可以将测试移至命名良好的函数
$OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
switch ($OSVersion)
{
"Windows Server 2008 R2 Standard"
{
Write-Host "Hooray It's Server 2K8 r2!"
Invoke-Item "C:\Pictures\Hooray.jpg"
}
"Windows 7 Professional"
{
Write-Host "Okay, Windows 7 is cool, too!"
Invoke-Item "C:\Pictures\Smiley.jpg"
}
"Windows Vista"
{
Write-Host "What have I done with my life?!"
Invoke-Item "C:\Pictures\GunToHead.jpg"
}
"Windows Millennium Edition"
{
Write-Host "Go away, operating system. You are drunk."
Invoke-Item "C:\Pictures\LiquorAndHiccups.jpg"
}
}