问题
I am writing a PowerShell script to host a website in IIS.
I tried this script in a machine where there is no IIS installed and I got error so I want to check if IIS is installed and then I want to host a website in ISS.
Below is the script I am trying but it is not working:
$vm = "localhost";
$iis = Get-WmiObject Win32_Service -ComputerName $vm -Filter "name='IISADMIN'"
if ($iis.State -eq "Running") {
Write-Host "IIS is running on $vm"
}
else {
Write-Host "IIS is not running on $vm"
}
Please help me with any PowerShell script to check if IIS is installed or not.
回答1:
if ((Get-WindowsFeature Web-Server).InstallState -eq "Installed") {
Write-Host "IIS is installed on $vm"
}
else {
Write-Host "IIS is not installed on $vm"
}
回答2:
I needed to do this for a list of several hundred servers today. I modified the previous answer to use get-service w3svc instead of WMI installed state. This seems to be working for me so far.
$servers = get-content c:\listofservers.txt
foreach($server in $servers){
$service = get-service -ComputerName $server w3svc -ErrorAction SilentlyContinue
if($service)
{
Write-Host "IIS installed on $server"
}
else {
Write-Host "IIS is not installed on $server"
}}
回答3:
In an elevated Powershell window this works for me:
On Windows Server 2012
(on Windows Server 2008
first run this: Import-Module ServerManager
)
if ((Get-WindowsFeature Web-Server).Installed) {
Write-Host "IIS installed"
}
On Windows 10
if ((Get-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole").State -eq "Enabled") {
Write-Host "IIS installed"
}
来源:https://stackoverflow.com/questions/47673926/check-whether-iis-is-installed-or-not