C#运行powershell命令如何传递字节数组

C#运行powershell命令如何传递字节数组,c#,arrays,powershell,C#,Arrays,Powershell,我试图从c#代码调用powershell命令,需要作为参数字节数组传递,但它转换为错误的格式。 C#代码: 实际上,powershell: Set-UserPhoto -Identity:'hennadiy.lebedyev@itera.no' -Confirm:$False -PictureData:'255','216','255','224',...,'0','16' Set-UserPhoto -Identity:'hennadiy.lebedyev@itera.no' -Confir

我试图从c#代码调用powershell命令,需要作为参数字节数组传递,但它转换为错误的格式。 C#代码:

实际上,powershell:

Set-UserPhoto -Identity:'hennadiy.lebedyev@itera.no' -Confirm:$False -PictureData:'255','216','255','224',...,'0','16'
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'
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


希望您不要使用实际的powershell命令提供帮助。您可以加载一个文件,通过管道将其传输到命令,并使用管道对象作为参数。事实上,
Set UserPhoto
在流而不是字节缓冲区中也能正常工作,也许与您的问题正交,但为什么要使用C#调用powershell?您不能直接调用脚本正在使用的任何API,绕过所有powershell限制吗?