簡體   English   中英

Powershell調用命令ASPX / C#與命令行

[英]Powershell Invoke-Command ASPX/C# vs. Commandline

因此,我嘗試從ASPX / C#應用程序調用PS腳本。 當我從命令行運行PS腳本( PS C:\\scripts> ./do-barrelRoll.ps1 -s remoteComputer )時,它可以按預期工作。 但是,當我使用C#時,它無法連接到遠程計算機(錯誤的用戶名/密碼)。 有趣的事實:要使用的用戶和密碼在PS腳本本身的config部分中(請參閱$SETUP變量)! 我究竟做錯了什么?

來自ASPX的確切錯誤消息(服務器名稱正確!):

[remoteComputer] Connecting to remote server remoteComputer failed with the following error message : The user name or password is incorrect

PS腳本的相關部分:

set-alias new New-Object
$passwd = $SETUP["password"] | ConvertTo-SecureString -asplaintext -force
$session = new -TypeName System.Management.Automation.PSCredential -argumentlist $SETUP["user"], $passwd

Invoke-Command –ComputerName $PARAM["s"] -Credential $session –ScriptBlock {
    # Do a barrel roll
}

ASPX / C#應用程序的相關部分:

using System.Management.Automation;

public ActionResult doAction(string server, string action)
{
    var split = server.Split(';');
    //...
    var ps = PowerShell.Create();
    ps.AddScript("Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -f; C:\\scripts\\do-barrelRoll.ps1 -s " + split[1]);
    Collection<PSObject> results = ps.Invoke();
    // Here I retrive the results and the content of the error stream and display them
}

它很可能在命令行上按預期方式工作,這很可能是因為您的帳戶具有完整的腳本訪問權限並且可以運行所有內容。 .NET運行的帳戶應受到更多限制。 您必須檢查該帳戶是否有權運行不受限制的腳本。 檢查本地政策,然后從那里開始。 您很可能與運行.NET應用程序的帳戶不同。

暫無
暫無

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

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