我需要连接到另一个域中的服务器。从该服务器,我连接到其他3个域,以查找每个域中的用户帐户。
我连接到终端服务器,然后运行'invoke-command‘。出现了几个问题:
1-我指定了我的凭据是什么,但当我运行脚本时,它会提示输入凭据。
stating我得到一个错误,指出invokemethod为空
脚本如下:
PARAM (
[Parameter(ValueFromPipeline)]
$FullName
)
###Creds###
$Password = Read-Host -AsSecureString "Enter Your Password"
$apcred = New-Object System.Management.Automation.PSCredential
("server\username",$Password)
### Double Hop ###
$Session = New-PSSession -Name RDP -ComputerName Server.com -Credential
($apcred)
Enable-WSManCredSSP -Role Client -DelegateComputer server.com -Force
Invoke-Command -Session $session -ScriptBlock {Enable-WSManCredSSP -Role
Server -Force}
Invoke-Command -Session $session -ScriptBlock {
$rrs = Get-ADUser -Filter * -Server server2.com -Credential ($apcred)
$rrsusers = ($rrs | Where-Object{$_.name -eq $Name})这是我得到的错误:
You cannot call a method on a null-valued expression.
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull发布于 2016-08-18 02:48:37
脚本块有自己的作用域,因此在您正在运行的脚本块的上下文中,$apcred是空的。您可以在-ArgumentList参数中传递对象,或者在变量前加上"using“前缀,就像在$using:apcred中一样。这会导致ScriptBlock进入外部作用域以获取$apcred变量。
https://stackoverflow.com/questions/39003623
复制相似问题