簡體   English   中英

C#運行powershell命令如何傳遞字節數組

[英]C# running powershell command how to pass byte array

我正在嘗試從C#代碼調用powershell命令,並且需要將其傳遞為參數字節數組,但是它轉換的格式錯誤。 C#代碼:

var command = new Command("Set-UserPhoto");
command.Parameters.Add("Identity", login);
command.Parameters.Add("Confirm", false);
command.Parameters.Add("PictureData", pictureData); //byte array

實際上是powershell:

Set-UserPhoto -Identity:'hennadiy.lebedyev@itera.no' -Confirm:$False -PictureData:'255','216','255','224',...,'0','16'

需要的PowerShell:

Set-UserPhoto -Identity:'hennadiy.lebedyev@itera.no' -Confirm:$False -PictureData:(255,216,255,224,...,0,16)

不能與AddScript powershell功能一起使用。

如果您的“ Command”類與System.Management.Automation.PSCommand等效,那么您做對了。

在我用來執行此操作的代碼下面:

const string connectionUri = "https://outlook.office365.com/powershell-liveid/?proxymethod=rps";
const string schemaUri = "http://schemas.microsoft.com/powershell/Microsoft.Exchange";

const string loginPassword = "password";
SecureString secpassword = new SecureString();
foreach (char c in loginPassword)
{
    secpassword.AppendChar(c);
}
PSCredential exchangeCredential = new PSCredential("demo@sample.com", secpassword);

WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(connectionUri), schemaUri, exchangeCredential)
{
    MaximumConnectionRedirectionCount = 5,
    AuthenticationMechanism = AuthenticationMechanism.Basic
};

using (var remoteRunspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
    remoteRunspace.Open();

    using (PowerShell powershell = PowerShell.Create())
    {
        powershell.Runspace = remoteRunspace;

        var command = new PSCommand()
            .AddCommand("Set-UserPhoto")
            .AddParameter("Identity", "user@sample.com")
            .AddParameter("PictureData", File.ReadAllBytes(@"C:\Test\MyProfilePictures\pic.jpg"))
            .AddParameter("Confirm", false);
        powershell.Commands = command;
        powershell.Invoke();
    }
}

?proxymethod = rps 對於支持> 10Ko文件很重要

希望對您有所幫助

暫無
暫無

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

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