I am trying to invoke powershell command from c# code and need to pass as parameter byte array but it converts in wrong format. C# code:
var command = new Command("Set-UserPhoto");
command.Parameters.Add("Identity", login);
command.Parameters.Add("Confirm", false);
command.Parameters.Add("PictureData", pictureData); //byte array
Actually powershell:
Set-UserPhoto -Identity:'hennadiy.lebedyev@itera.no' -Confirm:$False -PictureData:'255','216','255','224',...,'0','16'
Needed powershell:
Set-UserPhoto -Identity:'hennadiy.lebedyev@itera.no' -Confirm:$False -PictureData:(255,216,255,224,...,0,16)
Using with function AddScript powershell cannot be run.
If your "Command" class is the equivalent of System.Management.Automation.PSCommand , you've done it right.
Below the code I've used to perform this operation:
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 is important to support >10Ko files
Hope that help
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.