2012-04-11 52 views
0

我想用不同的用戶憑證從我的腳本中創建一個新的PowerShell實例。 我的想法是在這個腳本部分做一些管理員用不同的憑據。我想用正常的用戶權限執行主要部分。PowerShell - 使用不同憑據創建新實例

的想法,但不工作:

$script= {$InScript = Get-Process} 

$pp= get-credential 

[System.Diagnostics.Process]::Start("c:\windows\syswow64\WindowsPowerShell\v1.0\powershell.exe", "-command $script", $pp.UserName.Split('\')[1] , $pp.Password , $pp.UserName.Split('\')[0]) 

[System.Diagnostics.Process]::Start("c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe", "-command $script", $pp.UserName.Split('\')[1] , $pp.Password , $pp.UserName.Split('\')[0]) 


$InScript 

異常在與啓動兩行命令:

異常調用 「開始」 和 「5」 的說法(S):「目錄名稱 無效「

當我用管理員權限執行腳本時,它工作的很完美。

回答

1

您可以使用.NET:

這是::Start方法的構造:

::Start(string fileName, string arguments, string userName, System.Security.SecureString password, string domain) 

編輯點評後:

儘量把完整路徑EXE:

[System.Diagnostics.Process]::Start("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe", "-command $script", $pp.UserName.Split('\')[1] , $pp.Password , $pp.UserName.Split('\')[0]) 

或使用啓動過程:

start-process -FilePath "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Credential $pp -ArgumentList $script 
+0

感謝您的評論!但我得到一個執行。使用「5」參數調用「開始」的異常:「目錄名稱無效」在行:2 char:36 – LaPhi 2012-04-11 10:13:03

+0

發佈發生錯誤的行。它似乎是可執行文件路徑中的一些錯誤.. – 2012-04-11 10:26:23

+0

PS C:\ Windows \ System32 \ WindowsPowerShell \ v1.0> [System.Diagnostics.Process] :: Start(「powershell.exe」,「-command $ script 「 ,$ pp.UserName.Split('\')[1],$ pp.Password,$ pp.UserName.Split('\')[0]) 使用」5「參數調用」開始「 s):「目錄名稱無效」 在線:1 char:36 + [System.Diagnostics.Process] ::開始<<<<(「powershell.exe」,「-command $ script」,$ pp .UserName.Split('\')[1],$ pp.Pas sword,$ pp.UserName.Split('\')[0]) + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException – LaPhi 2012-04-11 11:19:02