如何在名称存储在变量中的计算机上执行命令? [英] How to execute command on computer whose name is stored in variable?

查看:23
本文介绍了如何在名称存储在变量中的计算机上执行命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

$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 中的其余代码.有人知道怎么做这个吗?请注意,其余代码可以是十多种不同脚本中的一个,因此我可以将所选脚本作为变量提供,然后远程执行它们.理想情况下,代码也将自动传递参数,以便现在必须使用我们目前拥有的代码.

Now I would like to execute a the rest of the code on the computer stored in the variable $domain. Does anybody know how to do this? Note that the rest of the code can be one of over a dozen different script so I'm fine with giving the chosen script as a variable and then executing them remotely. Ideally the code would also pass on the arguments automatically so as to now to have to chance the code we have so far.

调用命令 [[-ComputerName] ] [-ScriptBlock] [-ApplicationName ] [-ArgumentList ] [-AsJob] [-Authentication ] [-CertificateThumbprint ] [-ConfigurationName ] [-Credential] [-EnableNetworkAccess] [-HideComputerName] [-InDisconnectedSession] [-InputObject ] [-JobName ] [-Port ] [-SessionName ;] [-SessionOption <PSSessionOption>] [-ThrottleLimit <Int32>] [-UseSSL] [ <CommonParameters>]

Invoke-Command [[-ComputerName] <String[]>] [-ScriptBlock] <ScriptBlock> [-ApplicationName <String>] [-ArgumentList <Object[]>] [-AsJob] [-Authentication <AuthenticationMechanism>] [-CertificateThumbprint <String>] [-ConfigurationName <String>] [-Credential <PSCredential>] [-EnableNetworkAccess] [-HideComputerName] [-InDisconnectedSession] [-InputObject <PSObject>] [-JobName <String>] [-Port <Int32>] [-SessionName <String[]>] [-SessionOption <PSSessionOption>] [-ThrottleLimit <Int32>] [-UseSSL] [ <CommonParameters>]

看起来不错,但我必须使用它,第一个参数是 Invoke-Command -ComputerName $domain ScriptBlock 是代码的其余部分?这是如何运作的?

looks quite good, but I would have to use it with the first argument being Invoke-Command -ComputerName $domain ScriptBlock being the rest of the code? How does that work?

或者 Enter-PSSession 命令是否可行?

Alternively maybe the Enter-PSSession command might work?

推荐答案

把你的代码改成这样:

$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

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

A scriptblock is a piece of code in curly brackets. Scriptblocks normally can't use variables from the surrounding script, though, so you need to pass them into the scriptblock via the -ArgumentList parameter.

这篇关于如何在名称存储在变量中的计算机上执行命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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