无法为多个服务器运行 Powershell 脚本以获取 IIS 详细信息

无法为多个服务器运行 Powershell 脚本以获取 IIS 详细信息

我编写了一个脚本来从多个服务器获取 IIS 详细信息,但是该脚本对于一个服务器运行良好,却无法在多个服务器上运行,并且会引发如下错误:

错误:

[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

我写的脚本


#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

Get-Website只会返回 IIS 服务器中默认网站的配置。您必须使用Get-IISSite来列出所有 IIS 网站。像这样:

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

还要确保 WinRM 服务在远程计算机上运行,​​以便它支持 powershell 远程处理。

相关内容