我对powershell很新,我想知道为什么这不起作用。基本上,我有一个foreach循环,用于检查IIS是否正在运行并报告操作系统版本(如果是)并将其写入文件。但是,它没有正确报告。它似乎获取第一个项目(IIS状态和操作系统)的值,然后通过我的循环中的其他项填充它。
#Get List of Servers to Scan
$servers = (Get-Content e:\servers.txt)
#Determine if IIS is Running
foreach($server in $servers){
$iis = get-wmiobject Win32_Service -ComputerName $server -Filter "name='IISADMIN'";
if($iis.State -eq "Running")
{$OSVer= [environment]::OSVersion.Version}
Write-Host "$server,$IISRun,$OSVer" | Out-File -FilePath "E:\results.txt"
}
结果:
Server1,true,5.2.3790.131072 <---- Correct
Server2,true,5.2.3790.131072 <---- Correct
Server3,true,5.2.3790.131072 <---- Wrong OS reported (windows 2008)
Server4,true,5.2.3790.131072 <---- Wrong OS reported (windows 2008)
Server5,true,5.2.3790.131072 <---- IIS isn't installed here at all
Server6,true,5.2.3790.131072 <---- IIS isn't installed here at all
感谢您的任何建议......
答案 0 :(得分:4)
[environment]::OSVersion.Version
为您提供本地操作系统版本。请使用Get-WmiObject Win32_OperatingSystem -ComputerName $server
来获取目标计算机的操作系统信息,例如:
foreach ($server in $servers)
{
$iis = Get-WmiObject Win32_Service -ComputerName $server -Filter "name='IISADMIN'"
$os = Get-WmiObject Win32_OperatingSystem -ComputerName $server
$iisRunning = $iis.State -eq "Running"
$osVersion = $os.Version
Write-Host "$server, $iisRunning, $osVersion"
}