Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

How do I use @{add='jbloggs'} syntax with c# remote runspace

$
0
0

Hi,

I need to add/remove from a mailbox -GrantSendOnBehalfTo attribute in Office 365 using c# and remote powershell.  However I am having problems with the syntax in c# with dotnet 3.5. 

I have tried:

Command cmdlet = new Command("set-mailbox");
cmdlet.Parameters.Add("Identity", "jbloggs");
cmdlet.Parameters.Add("GrantSendOnBehalfTo", "jsmith");
PSCommand command = new PSCommand();
command.AddCommand(cmdlet);
powershell_2.Commands = command;
runspace_2.Open();
powershell_2.Runspace = runspace_2;
Collection<PSObject> output = powershell_2.Invoke();

The above works but it over-writes the send on behalf list instead of adding to it.

If I use the multi-value attribute syntax as follows:

set-mailbox -identity jbloggs -GrantSendOnBehalfTo @{add='jsmith'}

in an interactive remote PS session it works correctly and adds to existing entries.
 
So I tried the same syntax in my code:
Command cmdlet = new Command("set-mailbox");
cmdlet.Parameters.Add("Identity", "jbloggs");
cmdlet.Parameters.Add("GrantSendOnBehalfTo", "@{add='jsmith'}");
PSCommand command = new PSCommand();
command.AddCommand(cmdlet);
powershell_2.Commands = command;
runspace_2.Open();
powershell_2.Runspace = runspace_2;
Collection<PSObject> output = powershell_2.Invoke();

This fails after the Invoke with the error:
System.Management.Automation.RemoteException: Couldn't find object "@{add='jsmith'}".
Please make sure that it was spelled correctly or specify a different object.

What method can I use to execute the "@{add='jsmith'}" functionality in a remote runspace using c#?

I would appreciate any advice on this please.


OL2LS


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>