PowerShell:检查是否安装了FTP服务和/或SMTP服务和IIS服务



我想检查环境中所有机器的FTP/SMTP和IIS服务的状态。我们有混合操作系统,如2008,2012R2,2016和2019。

如何调整我的脚本?

下面是我的脚本:
$w3svc = get-service w3svc -ErrorAction SilentlyContinue
$smtpsvc = get-service smtpsvc -ErrorAction SilentlyContinue
$ftpsvc = get-service ftpsvc -ErrorAction SilentlyContinue

If (($w3svc -eq $null) -and (($ftpsvc -eq $null) -and ($smtpsvc -eq $null))) {

Write-Host "IIS is not installed , FTP is not installed , SMTP is not installed $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"

}
Elseif  (($w3svc -ne $null) -and (($ftpsvc -eq $null) -and ($smtpsvc -eq $null)))
{
Write-Host "IIS installed $($w3svc.status) , FTP is not installed , SMTP is not installed $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"
}
Elseif  (($w3svc -ne $null) -and (($ftpsvc -ne $null) -and ($smtpsvc -ne $null)))
{
Write-Host "IIS installed $($w3svc.status) , FTP installed $($ftpsvc.status) , SMTP installed $($smtpsvc.status) $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"
}

Elseif  (($w3svc -ne $null) -and (($ftpsvc -eq $null) -and ($smtpsvc -ne $null)))
{
Write-Host "IIS installed $($w3svc.status), FTP is not installed , SMTP installed $($smtpsvc.status) $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"
}

Elseif  (($w3svc -ne $null) -and (($ftpsvc -ne $null) -and ($smtpsvc -eq $null)))
{

Write-Host "IIS installed $($w3svc.status) , FTP installed $($ftpsvc.status) , SMTP is not installed $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"
}

Elseif  (($w3svc -eq $null) -and (($ftpsvc -ne $null) -and ($smtpsvc -ne $null)))
{
Write-Host "IIS is not installed , FTP installed $($ftpsvc.status) , SMTP installed $($smtpsvc.status) $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"
}

Elseif (($w3svc -eq $null) -and (($ftpsvc -eq $null) -and ($smtpsvc -ne $null)))
{
Write-Host "IIS is not installed , FTP is not installed , SMTP installed $($smtpsvc.status) $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"
}

Elseif  (($w3svc -eq $null) -and (($ftpsvc -ne $null) -and ($smtpsvc -eq $null)))
{
Write-Host "IIS is not installed , FTP  installed $($ftpsvc.status) , SMTP is not installed $($(Get-WmiObject -class Win32_OperatingSystem).Caption)"
}

如果您有一个计算机名数组要测试,您可以这样做:

$result = foreach ($machine in $listOfComputers) {
'w3svc', 'smtpsvc', 'ftpsvc' | ForEach-Object {
$svc = Get-Service -Name $_ -ComputerName $machine -ErrorAction SilentlyContinue
[PsCustomObject]@{
ComputerName = $machine
ServiceName  = $_
IsInstalled  = [bool]($svc)
DisplayName  = if ($svc) {$svc.DisplayName} else {'Not available'}
Status       = if ($svc) {$svc.Status} else {'Not available'}
}
}
}
# output on screen
$result | Format-Table -AutoSize  # or use Out-GridView
# save to Csv file
$result | Export-Csv -Path 'X:PathTooutput.csv' -NoTypeInformation

输出如下:

ComputerName ServiceName IsInstalled DisplayName                              Status
------------ ----------- ----------- -----------                              ------
Server01     w3svc              True World Wide Web Publishing-service       Running
Server01     smtpsvc           False Not available                     Not available
Server01     ftpsvc            False Not available                     Not available
Server02     w3svc              True World Wide Web Publishing-service       Running
Server02     smtpsvc           False Not available                     Not available
Server02     ftpsvc            False Not available                     Not available

我没有这么多操作系统可以测试

最新更新