远程会话上的Invoke-Command返回本地值 [英] Invoke-Command on remote session returns local values

查看:254
本文介绍了远程会话上的Invoke-Command返回本地值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Invoke-Command的脚本块在与PSSession一起运行时是否应始终在远程计算机上运行?

Should the script block of Invoke-Command, when run with a PSSession, always run on the remote computer?

我对服务器列表运行了以下Powershell:

I ran the below powershell against a list of servers:

Clear-Host
$cred = get-credential 'myDomain\myUsername'
$psSessions = New-PSSession -ComputerName @(1..10 | %{'myServer{0:00}' -f $_}) -Credential $cred
Invoke-Command -Session $psSessions -ScriptBlock {
    Get-Item -Path 'HKLM:\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters' 
} | Sort-Object PSComputerName
# $psSessions  | Remove-PSSession

此返回:

    Hive: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos


Name                           Property                   PSComputerName
----                           --------                   --------------
Parameters                     MaxPacketSize : 1          myServer01
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer02
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer03
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer04
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer05
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer06
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer07
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer08
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer09
                               MaxTokenSize  : 65535
Parameters                     MaxPacketSize : 1          myServer10
                               MaxTokenSize  : 65535

一切看起来不错;只有我不希望看到这些值/在设置这些服务器上的值以确保没有覆盖任何内容之前,我已经将其作为一种快速检查来运行.

All looks good; onlyl I'd not expected to see these values / I was running this as a quick sense check before setting the values on these servers to ensure I didn't overwrite anything.

我快速浏览了使用regedit的其中一台服务器;并发现MaxTokenSizeMaxPacketSize不存在.

I had a quick look at one of the servers using regedit; and found that MaxTokenSize and MaxPacketSize did not exist.

然后我修改命令以使用Get-ItemProperty代替Get-Item:

I then amended the command to use Get-ItemProperty instead of Get-Item:

Invoke-Command -Session $psSessions -ScriptBlock {
    Get-ItemProperty -Path 'HKLM:\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters' -Name 'MaxTokenSize'
} | Sort-Object PSComputerName

这次我有10个错误:

Property MaxTokenSize does not exist at path HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters.
    + CategoryInfo          : InvalidArgument: (MaxTokenSize:String) [Get-ItemProperty], PSArgumentException
    + FullyQualifiedErrorId : System.Management.Automation.PSArgumentException,Microsoft.PowerShell.Commands.GetItemPropertyCommand
    + PSComputerName        : myServer01
# ... (and the same for the other 9 servers, with only PSComputerName changing)

关于返回值的来源...它们来自我的本地计算机.修改我的本地注册表项并重新运行原始命令,显示所有服务器"都具有新值.

Regarding where the values that were returned came from... they're from my local machine. Amending my local registry entries and rerunning the original command showed all "servers" as having the new value.

我猜这是一个错误;但是因为到目前为止我还没有玩过PSSession,所以我想在这里检查一下,以防我理解/使用这些命令时遇到问题,或者在使用PSSession时是否有需要注意的地方.

I'm guessing this is a bug; but because I've not played with PSSessions much so far wanted to check here in case it's an issue with my understanding / usage of these commands, or if there are known gotchas to watch out for when using PSSessions.

推荐答案

将其放在fl *或ft *上,因此它不使用格式文件显示注册表项.格式文件在本地运行get-itemproperty,以尝试显示属性.

Pipe it to fl * or ft * so it doesn't use the format file to display the registry keys. The format file runs get-itemproperty locally to try to display the properties.

从$ PSHOME \ Registry.format.ps1xml的底部键入Microsoft.Win32.RegistryKey类型:

From the bottom of $PSHOME\Registry.format.ps1xml for type Microsoft.Win32.RegistryKey:

<ScriptBlock>
  $result = (Get-ItemProperty -LiteralPath $_.PSPath |
      Select * -Exclude PSPath,PSParentPath,PSChildName,PSDrive,PsProvider |
      Format-List | Out-String | Sort).Trim()
  $result = $result.Substring(0, [Math]::Min($result.Length, 5000) )
  if($result.Length -eq 5000) { $result += "..." }
  $result
</ScriptBlock>

这篇关于远程会话上的Invoke-Command返回本地值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆