1

当通过调用命令调用时,我观察到 IIS7 CLI 实用程序 appcmd 的这种奇怪行为。

这是我正在尝试执行的一小段命令。

PS C:\>appcmd list config /section:applicationPools | Select-String -SimpleMatch "SomeAppPool" -context 0,4 | Out-String -Stream | Select-String " pingingEnabled=""true"""

该命令执行得非常好[在设置了所有 %PATH% 变量的 IIS7 机器上完成]。我得到一个像

<processModel pingingEnabled="true" />

通过远程机器的调用命令完成同样的事情。

远程版本-I

PS C:\>invoke-command -computername iismachine -credential $creds -scriptblock {
    appcmd list config /section:applicationPools | Select-String -SimpleMatch "SomeAppPool" -context 0,4 | Out-String -Stream | Select-String " pingingEnabled=""true"""
}

没有输出,只有三个空行!!!

虽然如果我通过创建一个远程 powershell 会话来做到这一点,它开始工作正常..

远程版本-II

PS C:\>enter-pssession -computername iismachine -credential $creds;
[iismachine]: PS C:\Users\onewildgamer\Documents>invoke-command -scriptblock {
    appcmd list config /section:applicationPools | Select-String -SimpleMatch "SomeAppPool" -context 0,4 | Out-String -Stream | Select-String " pingingEnabled=""true""" 
}

现在行为有所不同,我不怀疑用户权限问题。

如果我做错了什么,请告诉我。我一生都无法弄清楚为什么这不起作用。

4

1 回答 1

1

我怀疑这实际上是一个权限和/或双跳问题。

首先,Remote Version-II 不在远程机器上执行。Invoke-Command不带-ComputerNameor参数的调用-PSSession会在本地机器上执行脚本块。

首先启动会话的正确方法是使用New-PSSession然后将其返回值传递给-PSSession参数Invoke-Command

$session = New-PSSession -ComputerName iismachine -Credential $creds
Invoke-Command -PSSession $session -ScriptBlock { #code }

如果您尝试这种方式,您可能会看到它停止工作。

故障排除:

Invoke-Command -computername iismachine -credential $creds -scriptblock {
    appcmd list config /section:applicationPools # | Select-String -SimpleMatch "SomeAppPool" -context 0,4 | Out-String -Stream | Select-String " pingingEnabled=""true"""
}

这会注释掉所有其他内容,我认为这可能会掩盖错误消息。通过以这种方式运行它,您应该会看到原始输出,并且可能会更好地了解正在发生的事情。

通常,Invoke-Command您会遇到双跳身份验证问题,即您在远程会话中运行的命令尝试使用当前凭据对资源进行身份验证,但无法做到。即使资源不是远程机器的远程资源,有时也会出现这种情况,而 IIS 是我见过的情况之一。

调用也有可能appcmd是成功的,但是它的输出与您没有预料到的不同,并且您的Select-String调用通过给您空白行来掩盖这一点。

于 2015-01-09T03:01:28.870 回答