2013-08-29 42 views
1

我試圖找出如何使用提升的憑據運行PowerShell腳本,被告知要做到這一點的最好辦法是用開始處理PowerShell的 - 啓動過程中推出的hello world不工作

這網站,http://social.technet.microsoft.com/Forums/windowsserver/en-US/132e170f-e3e8-4178-9454-e37bfccd39ea/startprocess-verb-runas-credential也是很好的參考

但我仍然有麻煩。

我創建用於測試目的一個腳本,hello.ps1

write-host Hello World 

這本身運行良好

然後,我創建了另一個腳本調用的Hello World升高的憑據

<#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#> 

$password = get-content C:\Script\cred.txt | convertto-securestring 
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password 
$script = "C:\script\hello.ps1" 
Start-Process powershell -Credential $credentials -verb runas -ArgumentList "-file $script" 

而我得到的錯誤:

At C:\script\my_script.ps1:6 char:14 
+ Start-Process <<<< powershell -Credential $credentials -verb runas -ArgumentList "-file $script" 
    + CategoryInfo   : InvalidArgument: (:) [Start-Process], ParameterBindingException 
    + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.StartProcessCommand 

編輯

@Adi因巴爾

我更新的代碼如下

$password = get-content C:\Script\cred.txt | convertto-securestring 
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password 
$script = "C:\Script\hello.ps1" 
Start-Process powershell -Credential $credentials -ArgumentList "-file $script" 

但現在一個cmd窗口彈出,輸出爲空白,而不是預期的 「Hello World」

編輯

而且我讀到你必須包含-FilePath如果包含-Credential,但代碼仍然是不工作:-(

它只是彈出,cmd窗口並沒有輸出寫入powershell_ise.exe GUI

<#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#> 

$password = get-content C:\Script\cred.txt | convertto-securestring 
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password 
$script = "C:\Script\hello.ps1" 
Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Credential $credentials -ArgumentList "-file $script" 
+0

但你知道,這是我所說的相反嗎?是的,我在下面看到您的評論,並且我明白您確實需要以其他帳戶身份運行,但這並沒有改變這樣一個事實:具有本地管理員權限的憑證的「-Credential $ credential」不會自動爲您提供提升會話,就像以本地管理員身份登錄時交互啓動PowerShell一樣,默認情況下不會爲您提供高級會話。 –

回答

0

嗯,我能回答我的問題的一部分,因爲我還有一個更大的問題,我會後單獨

「noexit」在-ArgumentList保持cmd窗口持久,但至少它的輸出價值,所以至少我知道該程序正在工作

<#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#> 

$password = get-content C:\Script\cred.txt | convertto-securestring 
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\Username",$password 
$script = "C:\Script\hello.ps1" 

start-process powershell -Credential $credentials -ArgumentList '-noexit','-File', 'C:\script\hello.ps1' 
2

-Verb-Credential在不同的參數集。它們不能一起使用。 -Verb runas不作爲不同的用戶運行指定的進程(不要與命令混淆),它使用UAC在用戶的上下文中以提升的權限運行進程,如右鍵單擊並選擇「以管理員身份運行」。

只要擺脫-Credential $credentials,並在使用具有本地管理員權限的帳戶登錄時運行腳本。

+0

但我必須使用-Credential $憑證運行腳本。我有一個複雜的腳本,當我手動使用run-as,但由於它正在被第三方程序調用時,我建議使用Start-Process和-credentials開關http://stackoverflow.com/questions/18512613/PowerShell的腳本中調用的逐第三方,如何對硬代碼升高,credentia – Glowie