[英]Powershell, calling a script as a different user with multiple arguments in ArgumentList
我有一個 cmd 腳本(最終將成為任務調度程序觸發的任務)需要以不同的用戶身份啟動新的 Powershell 進程以調用其他腳本remote.ps1 。
解釋
層次結構如下:
1.0 - cmd 文件,運行 powershell:
powershell -ExecutionPolicy Bypass -File %SCRIPT_DIR%\credential.ps1
2.1 - credential.ps1從通過 Powershell ISE Read-Host -AsSecureString | 創建的其他用戶的加密密碼文件創建憑證 object ConvertFrom-安全字符串 | 外檔 object
$username = "myuser"
$securestringpwd = Get-Content -Path "C:\Desktop\pwd" | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential $username, $securestringpwd
2.2 -...在最后調用一個新的 powershell 進程,用附加的 arguments 執行remote.ps1腳本
$abc = "example1"
$def = "example2"
$dir = "$env:SCRIPT_DIR"
Start-Process powershell.exe -Credential $credential -ArgumentList @('-ExecutionPolicy"Bypass"', '-File"$dir\remote.ps1"', '$abc', '$def')
我已經測試過,憑證 obj 確實成功驗證並在新用戶下創建了一個新的 PS 進程。
問題
我似乎無法想出一種方法來啟動新的 powershell 腳本文件並傳入多個 arguments。我想我不能在 ArgumentList 中使用 ExecutionPolicy。
預期結果
為了讓我以新用戶身份運行最終的remote.ps1腳本同時傳入多個 arguments,正確的方法是什么? 提前致謝。
平台:Windows Server 2008 R2
Powershell:$PSVersionTable.PSVersion
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
你在 remote.ps1 里有什么? 我願意添加一條評論,但這很難閱讀。 我不確定,但這看起來像是缺少空格和錯誤引號的問題。 (在單引號內,變量不被解釋)
如果我運行它,在需要解釋的變量周圍加上正確的引號,或者甚至沒有引號作為最后一個例子(我添加了另一個參數“-noexit”以便能夠在屏幕上看到 output 並手動定義 $credential擺脫正在工作的 whole.bat 等):
$credential = Get-Credential
$abc = "example1"
$def = "example2"
$dir = "c:\temp"
Start-Process powershell.exe -Credential $creds -ArgumentList @('-ExecutionPolicy "Bypass"', '-noexit', "-File ""$dir\remote.ps1""", "$abc", $def)
然后,在 c:\temp\remote.ps1 我只有:
whoami
$args
正如預期的那樣,output 是:
DOMAIN\username
example1
exemple2
注意:我從 powershell 提示符運行了第一個腳本,提示符是“-version 2”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.