繁体   English   中英

无法使用Powershell从远程服务器获取IIS状态

[英]Unable to fetch IIS status from Remote server using Powershell

我正在尝试使用Powershell从远程服务器获取IIS状态。 我已经使用命令Get-Service,但是我没有从该命令接收任何输出。 下面是我的代码块。

$pass='pass'|ConvertTo-SecureString -AsPlainText -Force;
$Credentials =  New-Object   
System.Management.Automation.PsCredential("user",$pass);
$Service=invoke-command -computername "server" -credential $Credentials - 
scriptblock {Get-Service|Where-Object Name -eq 'IISADMIN'}
if($Service.Status -eq 'Running')
{
write-host "IIS Running"
}
else
{
 throw "IIS not running or Not installed"

}

我检查了您的代码,但没有发现任何问题,您是否使用Get-Service或Service Manager检查服务在本地是否存在?

无论如何,您不必为此使用Invoke-Command ,可以使用Get-Service cmdlet的内置-ComputerName参数,

如果需要提供凭据,则可以使用WMI:

$Credential = New-Object System.Management.Automation.PsCredential($username, $encrypted)
$Service = Get-WmiObject -Class win32_service -ComputerName Server -Filter 'Name = "W3SVC"' -Credential $Credentials

尝试使用WMI调用,我发现使用远程服务器时它更加可靠。

$ Service = Get-WmiObject-计算机名$ computer-凭据$ credentials Win32_service -ErrorAction继续| 其中{$ _。Name -eq'IISADMIN'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM