Forum Discussion

vlciho's avatar
vlciho
Copper Contributor
Oct 02, 2019

Using @ character in exchange powershell remoting (2010)

Hello,

how can i use this:
Set-MailPublicFolder -Identity Set-MailPublicFolder -Identity "CN=PF1,CN=Microsoft Exchange System Objects,DC=xxxxxxx,DC=local" -GrantSendOnBehalfTo @{add='domain\john.doe'} in vb.net?

VisualStudio say error, cannot find object @{add='domain\john.doe'}.

Thank you!

vlci

code:

            Dim strvalue As String = "@add=""domain\" & _username & ""
            Dim R2 As System.Management.Automation.Runspaces.Runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(pConnectionInfo)
            R2.Open()
            Dim P2 As System.Management.Automation.Runspaces.Pipeline = R2.CreatePipeline()
            Dim scriptParams As New Command("Set-MailPublicFolder")
            scriptParams.Parameters.Add("Identity", strIdentity)
            scriptParams.Parameters.Add("GrantSendOnBehalfTo", Trim(strvalue))

            P2.Commands.Add(scriptParams)

            Dim results As Collection(Of PSObject) = P2.Invoke()
            R2.Close()
  • vlciho 

     

    I would recommend you to set complete commands in one string variable and use AddScript function to add the string text as commands

    Dim strScript As String = "Set-MailPublicFolder -Identity '"+strIdentity+"' -GrantSendOnBehalfTo @{add= "+ _username +" }"
    Dim R2 As System.Management.Automation.Runspaces.Runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(pConnectionInfo)
    R2.Open()
    Dim P2 As System.Management.Automation.Runspaces.Pipeline = R2.CreatePipeline()
    P2.Commands.AddScript(strScript);
    Dim results As Collection(Of PSObject) = P2.Invoke()
    R2.Close()

    For your case, the string text should be like below :

    Dim strScript As String = "Set-MailPublicFolder -Identity Set-MailPublicFolder -Identity 'CN=PF1,CN=Microsoft Exchange System Objects,DC=xxxxxxx,DC=local' -GrantSendOnBehalfTo @{add='domain\john.doe'}"
  • Kevin_Morgan's avatar
    Kevin_Morgan
    Iron Contributor

    vlciho 

     

    I would recommend you to set complete commands in one string variable and use AddScript function to add the string text as commands

    Dim strScript As String = "Set-MailPublicFolder -Identity '"+strIdentity+"' -GrantSendOnBehalfTo @{add= "+ _username +" }"
    Dim R2 As System.Management.Automation.Runspaces.Runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(pConnectionInfo)
    R2.Open()
    Dim P2 As System.Management.Automation.Runspaces.Pipeline = R2.CreatePipeline()
    P2.Commands.AddScript(strScript);
    Dim results As Collection(Of PSObject) = P2.Invoke()
    R2.Close()

    For your case, the string text should be like below :

    Dim strScript As String = "Set-MailPublicFolder -Identity Set-MailPublicFolder -Identity 'CN=PF1,CN=Microsoft Exchange System Objects,DC=xxxxxxx,DC=local' -GrantSendOnBehalfTo @{add='domain\john.doe'}"
    • vlciho's avatar
      vlciho
      Copper Contributor

      Hi, Kevin_Morgan

      Thank you very much! This is works! 😉

      Dim strScript As String = "Set-MailPublicFolder -Identity '" + strIdentity + "' -GrantSendOnBehalfTo @{add='" + My.Settings.txtDomain.ToString + "\" + _username + "'}"
      Dim R2 As System.Management.Automation.Runspaces.Runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(pConnectionInfo)
      R2.Open()
      Dim P2 As System.Management.Automation.Runspaces.Pipeline = R2.CreatePipeline()
      P2.Commands.AddScript(strScript)
      Dim results As Collection(Of PSObject) = P2.Invoke()
      R2.Close()

       

Resources