[英]Unable to fetch IIS status from Remote server using Powershell
I am trying to fetch IIS status from remote server using powershell. 我正在尝试使用Powershell从远程服务器获取IIS状态。 I have used command Get-Service but i don't recieve any output from this command. 我已经使用命令Get-Service,但是我没有从该命令接收任何输出。 Below is my code block. 下面是我的代码块。
$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"
}
I Checked your code and did not see any problem with it, Did you checked the service exists locally using Get-Service
or Service Manager? 我检查了您的代码,但没有发现任何问题,您是否使用Get-Service
或Service Manager检查服务在本地是否存在?
Anyway you don't have to use Invoke-Command
for this, you can use the built in -ComputerName
parameter of the Get-Service
cmdlet, 无论如何,您不必为此使用Invoke-Command
,可以使用Get-Service
cmdlet的内置-ComputerName
参数,
And if you need to provide credentials, you can use WMI: 如果需要提供凭据,则可以使用WMI:
$Credential = New-Object System.Management.Automation.PsCredential($username, $encrypted)
$Service = Get-WmiObject -Class win32_service -ComputerName Server -Filter 'Name = "W3SVC"' -Credential $Credentials
Try using a WMI call instead, I have found it far more reliable when working with remote servers. 尝试使用WMI调用,我发现使用远程服务器时它更加可靠。
$Service = Get-WmiObject -Computername $computer -Credential $credentials Win32_service -ErrorAction Continue | $ Service = Get-WmiObject-计算机名$ computer-凭据$ credentials Win32_service -ErrorAction继续| Where {$_.Name -eq 'IISADMIN'} 其中{$ _。Name -eq'IISADMIN'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.