我正在撰寫一個腳本,我想知道是否有一種方法可以將變數例如 $a 從腳本 firstscript.ps1 提供給由 firstscript.ps1 執行的另一個腳本 secondscript.ps1
為了澄清我在說什么:
我執行 firstscript.ps1 它做了不同的事情然后它在遠程計算機上啟動 secondscript.ps1 我希望 $a 從將 firstscript.ps1 轉移到 secondscript.ps1
注意:我使用 psexec 遠程執行 secondscript.ps1 到具有管理員權限的計算機串列
我可以在第二個中寫它,但我想至少避免修改
uj5u.com熱心網友回復:
然后它在遠程計算機上啟動 secondscript.ps1,我希望 $a 從將 firstscript.ps1 轉移到 secondscript.ps1
假設您使用遠程呼叫腳本Invoke-Command,請通過引數將引數傳遞給腳本-ArgumentList:
## script1.ps1
# assign value to `$a`
$a = $(...)
# ...
# invoke script2.ps1 on remote machine with $a passed as first argument
Invoke-Command -ComputerName $nameOfRemoteComputer -FilePath path\to\script2.ps1 -ArgumentList $a
然后在 中script2.ps1,從以下位置讀取引數值$args[0]:
## script2.ps1
$a = $args[0] # grab argument value passed by script1.ps1
# work with $a here ...
...或宣告一個param()帶有引數的塊,該引數可以系結到:
## script2.ps1
param(
$a # Let the runtime bind the argument value passed by script1.ps1 to this parameter variable
)
# work with $a here ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/474163.html
標籤:电源外壳
上一篇:PS如何在檔案名中間添加符號
