I am trying to executing the following powershell command in C#
Invoke-Command -Session $session -ScriptBlock {
Get-MailboxPermission -Identity ${identity} -User ${user}
}
I tried with following C# code but couldn't set the identity and user parameters.
var command = new PSCommand();
command.AddCommand("Invoke-Command");
command.AddParameter("ScriptBlock", ScriptBlock.Create("Get-MailboxPermission -Identity ${identity} -User ${user}"));
command.AddParameter("identity", mailbox);
command.AddParameter("user", user);
When I hard code the values when creating the ScriptBlock, it's working fine. How can I set the params dynamically.
Is there a better way to do this rather concatenate values as below.
command.AddParameter("ScriptBlock", ScriptBlock.Create("Get-MailboxPermission -Identity " + mailbox + " -User " + user));