且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Powershell 将变量传递给远程脚本

更新时间:2023-01-08 18:40:09

您需要在脚本中使用一个 param 块,您传递给文件的参数将被分配给 $domainName 并且您将使用它来传递值到脚本块:

You need to use a param block in the script, the argument you pass to the file will be assign to $domainName and you will use it to pass the value to the scriptblock :

PowerShell.exe -noexit E:\wwwroot\domains\processes\AddDirectory.ps1 testdomain.co.uk


# script file

param($domainName)

$script = {
    Param($Param1)

    ...
    $domain = $Param1
    ...   
}

$Session = New-PSSession -ComputerName 192.168.0.25
Invoke-Command -Session $Session -ScriptBlock $script -ArgumentList $domainName