Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何在名称存储在变量中的计算机上执行命令?_Windows_Powershell - Fatal编程技术网

Windows 如何在名称存储在变量中的计算机上执行命令?

Windows 如何在名称存储在变量中的计算机上执行命令?,windows,powershell,Windows,Powershell,我有以下代码: $ADForest = Get-ADForest $ADForestGlobalCatalogs = $ADForest.GlobalCatalogs $domain= $ADForestGlobalCatalogs | Select-Object -first 1 //connect with $domain the rest of the code has to be executed on $domain //rest of code goes here (one out

我有以下代码:

$ADForest = Get-ADForest
$ADForestGlobalCatalogs = $ADForest.GlobalCatalogs
$domain= $ADForestGlobalCatalogs | Select-Object -first 1
//connect with $domain the rest of the code has to be executed on $domain
//rest of code goes here (one out of several different scripts). 
Remove-Mailbox "$A" -confirm:$false
Get-Addresslist | where {$_.Name -like "$k*"} | Update-Addresslist
Get-GlobalAddresslist | where {$_.Name -like "$k*"} | Update-GlobalAddresslist
现在,我想对存储在变量
$domain
中的计算机上的其余代码执行一个命令。有人知道怎么做吗?请注意,代码的其余部分可以是十几个不同脚本中的一个,因此我可以将所选脚本作为变量,然后远程执行它们。理想情况下,代码也会自动传递参数,以便现在有机会获得我们目前拥有的代码

编辑:

调用命令[[-ComputerName]][-ScriptBlock][ApplicationName][ArgumentList][AsJob][Authentication][CertificateThumbprint][ConfigurationName][Credential][EnableNetworkAccess][HideComputerName][InputObject][JobName][Port SessionName][SessionOption][ThrottleLimit UseSSL][]

看起来很好,但是我必须使用它,第一个参数是
Invoke Command-ComputerName$domain
ScriptBlock,其余的代码是什么?这是如何工作的


或者,
Enter-PSSession
命令可能会起作用?

将代码更改为以下内容:

$ADForest = Get-ADForest
$ADForestGlobalCatalogs = $ADForest.GlobalCatalogs
$domain = $ADForestGlobalCatalogs | Select-Object -first 1

Invoke-Command -Computer $domain -ScriptBlock {
  Param(
    [string]$Mailbox,
    [string]$Name
  )

  Remove-Mailbox $Mailbox -confirm:$false
  Get-Addresslist | where {$_.Name -like "$Name*"} | Update-Addresslist
  Get-GlobalAddresslist | where {$_.Name -like "$Name*"} | Update-GlobalAddresslist
} -ArgumentList $A, $k

scriptblock是一段用花括号括起来的代码。但是scriptblock通常不能使用周围脚本中的变量,因此您需要通过
-ArgumentList
参数将它们传递到scriptblock中。

为什么要进行关闭投票?是否要评论?我没有进行关闭投票,但这是为了“不清楚您在问什么”,事实上这有点难理解。你试过了吗?Invoke命令看起来很不错,阅读了它现在的功能,希望我能很好地理解它以使用。我希望这更清楚,现在看看是否有一种方法不必更改脚本的参数,以及如何将我已经拥有的代码实际存储在Invoke逗号中ND您看过
Invoke Command
示例了吗?我相信您会在远程机器上找到大量正在运行的命令。您是否尝试过一些特定的方法需要帮助?现在您似乎希望我们发布一些示例。