簡體   English   中英

使用不同的憑據運行Powershell腳本

[英]Run a powershell script with different credentials

我正在嘗試運行Powershell腳本來搜索某個文件的網絡驅動器。 在測試中,我發現我的腳本運行正常,但是我需要搜索的網絡驅動器需要我的Domain Admin登錄。

我有

Start-Process powershell.exe -Credential "domain\\adminusername" -NoNewWindow -ArgumentList "Start-Process powershell.exe -Verb runAs"

作為腳本的第一行,但是每當我運行腳本時,都會出現此錯誤:

Start-Process : This command cannot be run due to the error: The directory 
name is invalid.
At Path\to\script.ps1:1 char:1
+ Start-Process powershell.exe -Credential "domain\adminusername" -NoN ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [Start-Process], 
InvalidOperationException
+ FullyQualifiedErrorId : 
InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

它在說什么目錄名? 如果我將腳本移動到實際的網絡驅動器,仍然會出現相同的錯誤。 您如何以其他用戶身份運行腳本?

您可以使用net use命令獲得訪問權限,也可以使用new-psdrive命令。 另一個選擇是start-process一個cmd提示並在其中使用runas 另外,您可能需要包括powershell.exe的完整路徑或將其添加到path變量。 %SystemRoot%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe

New-PSDrive工作了! 非常感謝你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM