Dpw*_*808 1 powershell operating-system message-queue
有没有一种方法可以检查正在运行PowerShell脚本的操作系统,并发出一条If声明:
[伪代码]
if its this OS
do this
if its this other OS
do this
Run Code Online (Sandbox Code Playgroud)
仅针对脚本中的特定行?我必须制作一个设置私人消息队列的PowerShell脚本。遗憾的是,我公司的某些客户端未使用Windows Server 2012,因此添加私有消息队列的更简单版本无法在Windows Server 2008和过时的PowerShell版本上使用。要解决此问题,我还必须改用非常复杂的旧方法,但是我希望这两种方法都存在。
开关会更干净,但是既然您问过如何以这种特定方式来做...
设置变量以检查OS版本(从链接线程获取,而不使用WMI来获取操作系统):
$OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName
Run Code Online (Sandbox Code Playgroud)
构造您的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"
}
Run Code Online (Sandbox Code Playgroud)
希望能有所帮助。我假设您是PowerShell的新手,但是当您感到舒适时,就开始学习开关。
| 归档时间: |
|
| 查看次数: |
9989 次 |
| 最近记录: |