检查是否安装了IIS

时间:2017-12-06 12:06:19

标签: powershell iis service wmi

我正在编写一个PowerShell脚本来托管IIS中的网站。

我在没有安装IIS的机器上尝试过这个脚本,但是我得到了错误,所以我想检查是否安装了IIS,然后我想在ISS中托管一个网站。

以下是我正在尝试的脚本,但它无效:

$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"
}

请帮我处理任何PowerShell脚本,以检查是否安装了IIS。

3 个答案:

答案 0 :(得分:3)

if ((Get-WindowsFeature Web-Server).InstallState -eq "Installed") {
    Write-Host "IIS is installed on $vm"
} 
else {
    Write-Host "IIS is not installed on $vm"
}

答案 1 :(得分:0)

我今天需要列出数百台服务器。我修改了先前的答案,以使用get-service w3svc代替WMI安装状态。到目前为止,这似乎对我有用。

$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"
}}

答案 2 :(得分:0)

在高架的Powershell窗口中,这对我有用:

Windows Server 2012上(在Windows Server 2008上首先运行:Import-Module ServerManager

if ((Get-WindowsFeature Web-Server).Installed) {
    Write-Host "IIS installed"
} 

Windows 10

if ((Get-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole").State -eq "Enabled") {
    Write-Host "IIS installed"
}