0

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.

1 Answer 1

0

Get-Website will only return the configuration of default web site in IIS server. You have to use Get-IISSite for listing all IIS websites. Like this:

#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-IISSite

    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```

Also make sure WinRM service is running in the remote computer so it supports powershell remoting.

2
  • Even though i use the Get-IISSIte and my WIRM Service in running in the remote computer but still facing the same error to retrieve the IIS deatils from the remote server.
    – chandu
    Commented Jul 7, 2020 at 14:46
  • Can any one please help me with the above error mentioned in the question.
    – chandu
    Commented Jul 7, 2020 at 17:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .