I wrote a script to get the IIS deatils from Multiple servers, but the script is working fine for one server, but is unable to run for the multipl servers and it is throwing the error as below:
Error:
[ABDC.domain.com] Connecting to remote server
ABDC.domain.com failed with the following error message : The client
cannot connect to the destination specified in the request. Verify that the
service on the destination is running and is accepting requests. Consult the
logs and documentation for the WS-Management service running on the
destination, most commonly IIS or WinRM. If the destination is the WinRM
service, run the following command on the destination to analyze and configure
the WinRM service: "winrm quickconfig". For more information, see the
about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (ABDC.domain.com:String) [],
PSRemotingTransportException
+ FullyQualifiedErrorId : CannotConnect,PSSessionStateBroken
Cannot index into a null array.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
+ PSComputerName : ABDC.domain.com.tmk.ent.lc
Script i wrote
#Import-Module -Name WebAdministration
$Computers = Get-Content "C:\Chandrakanth\Servers.txt"
foreach ($server in $Computers) {
$iis = Get-WmiObject Win32_Service -Filter "Name = 'IISADMIN'" -ComputerName $server
if ($iis.State -eq 'Running')
{
$details = Write-Host "IIS is running on $server" -ForegroundColor Green
#Chandu
Invoke-Command -ComputerName $Computers -ScriptBlock {
# Changed to newer IISAdministration Module to match Get-IISAppPool
$Websites = Get-Website
foreach ($Website in $Websites) {
$AppPool = Get-IISAppPool -Name $Website.Applications[0].ApplicationPoolName
#$Website.ApplicationPool
[PSCustomObject]@{
#Server_Name = $env:COMPUTERNAME +'.'+ $env:USERDNSDOMAIN
Website_Name = $Website.Name
Website_Id = $Website.Id -join ';'
Website_State = $Website.State -join ';'
Website_PhysicalPath = $Website.PhysicalPath -join ';'
Website_Bindings = $Website.Bindings.Collection -join ';'
Website_Attributes = ($Website.Attributes | ForEach-Object { $_.name + "=" + $_.value }) -join ';'
#AppPool_Name = $AppPool.Name -join';'
#AppPool_State = $AppPool.State -join ';'
#AppPool_ManagedRuntimeVersion = $AppPool.ManagedRuntimeVersion -join ';'
#AppPool_ManagedPipelineMode = $AppPool.ManagedPipelineMode -join ';'
#AppPool_StartMode = $AppPool.StartMode -join ';'
}
}
} #| Export-Excel -Path C:\Chandrakanth\IISite_App-pool_Details.xlsx -AutoSize -BoldTopRow
#| Export-Csv -Path C:\IISite_App-pool_Details.txt
#| Export-Excel -Path C:\IISite_App-pool_Details.txt -AutoSize -BoldTopRow
#chandu end
}
Else
{
Write-Host "IIS is NOT running on $server" -ForegroundColor Red
}
} #Export-Excel -Path C:\Chandrakanth\IISite_App-pool_Details.xlsx -AutoSize -BoldTopRow```
Please help me to get the issue resolved.
Thanks in Advance.