Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 检查是否安装了IIS_Powershell_Iis_Service_Wmi - Fatal编程技术网

Powershell 检查是否安装了IIS

Powershell 检查是否安装了IIS,powershell,iis,service,wmi,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-

我正在编写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是否已安装。

我今天需要为数百台服务器的列表执行此操作。我修改了前面的回答,使用get service w3svc而不是WMI安装状态。到目前为止,这似乎对我有效

if ((Get-WindowsFeature Web-Server).InstallState -eq "Installed") {
    Write-Host "IIS is installed on $vm"
} 
else {
    Write-Host "IIS is not installed on $vm"
}
$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"
}}

在提升的Powershell窗口中,这对我很有用:

Windows Server 2012
上(在
Windows Server 2008
上首先运行此:
导入模块服务器管理器

windows10上

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

Get-WindowsFeature Web服务器…检查安装状态属性Get-WindowsFeature有时在Windows 10电脑中不存在。我认为Get-WindowsOptionalFeature可能是一种方法。@Anthony您可能需要运行
Import Module servermanager
来启用它。在Windows虚拟机(例如Windows Server)上,默认情况下安装服务器管理器,因此此处提供了
Get WindowsFeature
。但在其他Windows 10 PC上,情况并非如此。
if ((Get-WindowsOptionalFeature -Online -FeatureName "IIS-WebServerRole").State -eq "Enabled") {
    Write-Host "IIS installed"
}