PowerShell远程调用神秘地挂起

时间:2015-04-14 22:25:32

标签: powershell-v2.0 hang invoke-command

我已经创建了一系列基本上收集有关站点的所有IIS配置的函数,当它在本地服务器上运行时它没有问题(尽管很慢)但是当我使用invoke-command in <远程运行它们时强大> PowerShell 2 它贯穿并神秘地停止大约15-20秒进入该过程。它通常会在同一个请求中停止,但并非总是如此。在本地执行的相同命令没有任何问题。没有例外,它只是无限期挂起。

如果有必要,我可以发布代码,但它是几百行,所以我更多地寻找有关如何调查这样的问题的指导,或者是否有人遇到过类似的问题。

Comparing IISConfig between [targetserver] and localhost.
Checking Installed IIS version on [targetserver]:
IIS major version : 7
IIS minor version : 5
IIS7+ detected, using WebAdmin module and IIS metabase

Name                                                        Value
----                                                        -----
name                                                        Default Web Site
id                                                          1
serverAutoStart                                             True
state                                                       1


Site Configuration:

Name        Path        PSPath      Handlers_Ac Access_sslF Asp_AppAllo Asp_AppAllo Asp_limits_ Asp_EnableP Asp_limits_
                                    cessFlags   lags        wClientDebu  wDebugging bufferingLi  arentPaths queueTimeou
                                                                      g                     mit             t
----        ----        ------      ----------- ----------- ----------- ----------- ----------- ----------- -----------
Default ... IIS:Site... WebAdmin... Read,Script                   False       False    25000000        True 00:00:00


WebApp VDir: /MyApp, App Pool: MyApp
App pool Configuration:

AppPoolID   Enable32Bit managedPipe managedRunt AppPoolName AppPoolAuto processMode processMode processMode recycling_l
             AppOnWin64 lineMode    imeVersion                    Start l_idleTimeo l_identityT l_UserName  ogEventOnRe
                                                                        ut          ype                     cycle
---------   ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
                  False Classic     v2.0        MyApp                True 00:20:00    LocalSer...             Time,Req...


Analyzing web directories for /MyApp, this could take a while....
Initial Collection Completed, found 141... took 0.9516122 seconds
0 C:\inetpub\wwwroot\MyApp\Core
1 C:\inetpub\wwwroot\MyApp\Core\AdminTools
2 C:\inetpub\wwwroot\MyApp\Core\AdminTools\Cache
3 C:\inetpub\wwwroot\MyApp\Core\AdminTools\Extra
4 C:\inetpub\wwwroot\MyApp\Core\AdminTools\HTTPPostTest
5 C:\inetpub\wwwroot\MyApp\Core\AdminTools\IISAdmin
6 C:\inetpub\wwwroot\MyApp\Core\AdminTools\Profiling
7 C:\inetpub\wwwroot\MyApp\Core\AdminTools\RecordTestData
8 C:\inetpub\wwwroot\MyApp\Core\AdminTools\ScrambleTest
9 C:\inetpub\wwwroot\MyApp\Core\AdminTools\Sessions
Analyzed 10 so far... took 6.7236862 seconds, remaining time 88.08028922 seconds
Current Folder: C:\inetpub\wwwroot\MyApp\Core\AdminTools\Sessions
10 C:\inetpub\wwwroot\MyApp\Core\AdminTools\SoapTest
11 C:\inetpub\wwwroot\MyApp\Core\AdminTools\StaticContent

有时会达到15左右。我尝试从笔记本电脑和服务器到另一台服务器,行为是一样的。

这是挂起的循环:

$start = [System.DateTime]::Now
$numanalyzed = 0
if ($true) #skip to test
{
# loop through all physical folders as it is much faster
foreach ($folder in $folders)
{

    write-host $numanalyzed $folder.fullname
    #figure out the virtual path to the folder
    $iis7vwebfolderpath = $folder.FullName.Replace($iis7webapp.PhysicalPath, $iis7VDirWebApppath)
    #Get-item $iis7vwebfolderpath | gm
    $iis7VWebDirConfigItem = Get-LNOSIIS7ConfigForPSPath -PSPath $iis7vwebfolderpath

    # add new item to list
    $iis7VWebDirConfig += $iis7VWebDirConfigItem

    # increment counter and report out progress every 10
    $numAnalyzed++
    if ($numanalyzed % 10 -eq 0) 
    {

       $end = [System.DateTime]::Now
       $timeSoFar = (NEW-TIMESPAN –Start $Start –End $End).TotalSeconds
       $timeremaining = ($folders.Count - $numAnalyzed) * ($timeSoFar / $numanalyzed)
       "Analyzed {0} so far... took {1} seconds, remaining time {2} seconds" -f $numanalyzed,$timeSoFar,$timeremaining  | write-host 
       "Current Folder: {0}" -f $folder.FullName | Write-Host
    }
}
}



$end = [System.DateTime]::Now
"Processed web dirs: {0} took {1} seconds" -f $iis7VWebDirConfig.Count,(NEW-TIMESPAN –Start $Start –End $End).TotalSeconds | write-host   | Write-Host

我遇到性能问题的功能,我有一个单独的问题,但这篇文章有该功能的源代码:

web-administration vs WMI to query web directory properties performance problems

3 个答案:

答案 0 :(得分:1)

就我而言,由于Idle-Timeout过期,我的PowerShell调用似乎冻结了(该调用运行了很长时间)。

IdleTimeout的值设置为足够长的时间可以解决我的问题。

再次使用

查询当前配置
winrm get winrm/config/winrs

并使用设置超时

winrm set winrm/config/winrs @{IdleTimeout="18000000"}

答案 1 :(得分:0)

我想我可能已经发现了这个问题,我开始在剧本的其他部分遇到一些奇怪的失败:

 [SEVERNAME] Processing data from remote server SERVERNAME failed with the following error message: The WSMan provider host process did not return a proper response.  A provider in the host process may have behaved improperly. For more information, see the about_Remote_Troubleshooting Help topic. 
 + CategoryInfo          : OpenError: (SERVERNAME:String) [], PSRemotingTransportException
 + FullyQualifiedErrorId : 1726,PSSessionStateBroken

Processing data for a remote command failed with the following error message: Not enough storage is available to complete this operation. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo          : OperationStopped (System.Manageme...pressionSyncJob:PSInvokeExpressionSyncJob) [], PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure

这会引导我访问以下网站:http://www.gsx.com/blog/bid/83018/Troubleshooting-unknown-PowerShell-error-messages

以下建议似乎已经解决了大部分问题,尽管我还有一些测试要做。

摘自以下网站:

当第一条错误消息指定时,远程会话中的内存溢出。在远程服务器上打开PowerShell提示符,并使用以下命令显示winrs的配置:

winrm get winrm/config/winrs

检查&#34; MaxMemoryPerShellMB&#34;值。它在Windows Server 2008 R2和Windows 7上默认设置为150 MB。这是Microsoft在Windows Server 2012和Windows 8中更改为1024 MB的内容。

要解决此问题,您需要使用以下命令将值增加到至少512 MB:

winrm set winrm/config/winrs `@`{MaxMemoryPerShellMB=`"512`"`}

答案 2 :(得分:0)

如果仅供参考,则调用命令 始终 挂起:

  1. 尝试一个简单的命令到系统: Invoke-Command -ComputerName XXXXX -ScriptBlock { Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion }

  2. 启动Windows远程管理服务(在该系统上)

  3. 检查侦听端口:

     netstat -aon | findstr "5985"
          TCP    0.0.0.0:5985           0.0.0.0:0              LISTENING       4
          TCP    [::]:5985              [::]:0                 LISTENING       4