SOLVED

Using @ character in exchange powershell remoting (2010)

%3CLINGO-SUB%20id%3D%22lingo-sub-890117%22%20slang%3D%22en-US%22%3EUsing%20%40%20character%20in%20exchange%20powershell%20remoting%20(2010)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-890117%22%20slang%3D%22en-US%22%3E%3CP%3EHello%2C%3C%2FP%3E%3CP%3Ehow%20can%20i%20use%20this%3A%3CBR%20%2F%3ESet-MailPublicFolder%20-Identity%20Set-MailPublicFolder%20-Identity%20%22CN%3DPF1%2CCN%3DMicrosoft%20Exchange%20System%20Objects%2CDC%3Dxxxxxxx%2CDC%3Dlocal%22%20-GrantSendOnBehalfTo%20%40%7Badd%3D'domain%5Cjohn.doe'%7D%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ein%20vb.net%3F%3C%2FP%3E%3CP%3EVisualStudio%20say%20error%2C%20cannot%20find%20object%20%40%7Badd%3D'domain%5Cjohn.doe'%7D.%3C%2FP%3E%3CP%3EThank%20you!%3C%2FP%3E%3CP%3Evlci%3C%2FP%3E%3CP%3Ecode%3A%3C%2FP%3E%3CPRE%3E%20%20%20%20%20%20%20%20%20%20%20%20Dim%20strvalue%20As%20String%20%3D%20%22%40add%3D%22%22domain%5C%22%20%26amp%3B%20_username%20%26amp%3B%20%22%22%0A%20%20%20%20%20%20%20%20%20%20%20%20Dim%20R2%20As%20System.Management.Automation.Runspaces.Runspace%20%3D%20System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(pConnectionInfo)%0A%20%20%20%20%20%20%20%20%20%20%20%20R2.Open()%0A%20%20%20%20%20%20%20%20%20%20%20%20Dim%20P2%20As%20System.Management.Automation.Runspaces.Pipeline%20%3D%20R2.CreatePipeline()%0A%20%20%20%20%20%20%20%20%20%20%20%20Dim%20scriptParams%20As%20New%20Command(%22Set-MailPublicFolder%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20scriptParams.Parameters.Add(%22Identity%22%2C%20strIdentity)%0A%20%20%20%20%20%20%20%20%20%20%20%20scriptParams.Parameters.Add(%22GrantSendOnBehalfTo%22%2C%20Trim(strvalue))%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20P2.Commands.Add(scriptParams)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20Dim%20results%20As%20Collection(Of%20PSObject)%20%3D%20P2.Invoke()%0A%20%20%20%20%20%20%20%20%20%20%20%20R2.Close()%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-890117%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3Eexchange%202010%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20Server%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-898485%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20%40%20character%20in%20exchange%20powershell%20remoting%20(2010)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-898485%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F419136%22%20target%3D%22_blank%22%3E%40vlciho%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20recommend%20you%20to%20set%20complete%20commands%20in%20one%20string%20variable%20and%20use%20AddScript%20function%20to%20add%20the%20string%20text%20as%20commands%3C%2FP%3E%3CPRE%3EDim%20strScript%20As%20String%20%3D%20%22Set-MailPublicFolder%20-Identity%20'%22%2BstrIdentity%2B%22'%20-GrantSendOnBehalfTo%20%40%7Badd%3D%20%22%2B%20_username%20%2B%22%20%7D%22%0ADim%20R2%20As%20System.Management.Automation.Runspaces.Runspace%20%3D%20System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(pConnectionInfo)%0AR2.Open()%0ADim%20P2%20As%20System.Management.Automation.Runspaces.Pipeline%20%3D%20R2.CreatePipeline()%0AP2.Commands.AddScript(strScript)%3B%0ADim%20results%20As%20Collection(Of%20PSObject)%20%3D%20P2.Invoke()%3CBR%20%2F%3ER2.Close()%3C%2FPRE%3E%3CP%3EFor%20your%20case%2C%20the%20string%20text%20should%20be%20like%20below%20%3A%3C%2FP%3E%3CPRE%3EDim%20strScript%20As%20String%20%3D%20%22Set-MailPublicFolder%20-Identity%20Set-MailPublicFolder%20-Identity%20'CN%3DPF1%2CCN%3DMicrosoft%20Exchange%20System%20Objects%2CDC%3Dxxxxxxx%2CDC%3Dlocal'%20-GrantSendOnBehalfTo%20%40%7Badd%3D'domain%5Cjohn.doe'%7D%22%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-902330%22%20slang%3D%22en-US%22%3ERe%3A%20Using%20%40%20character%20in%20exchange%20powershell%20remoting%20(2010)%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-902330%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F38365%22%20target%3D%22_blank%22%3E%40Kevin%20Morgan%3C%2FA%3E%3C%2FP%3E%3CP%3EThank%20you%20very%20much!%20This%20is%20works!%20%3B)%3C%2Fimg%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3EDim%20strScript%20As%20String%20%3D%20%22Set-MailPublicFolder%20-Identity%20'%22%20%2B%20strIdentity%20%2B%20%22'%20-GrantSendOnBehalfTo%20%40%7Badd%3D'%22%20%2B%20My.Settings.txtDomain.ToString%20%2B%20%22%5C%22%20%2B%20_username%20%2B%20%22'%7D%22%0ADim%20R2%20As%20System.Management.Automation.Runspaces.Runspace%20%3D%20System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(pConnectionInfo)%0AR2.Open()%0ADim%20P2%20As%20System.Management.Automation.Runspaces.Pipeline%20%3D%20R2.CreatePipeline()%0AP2.Commands.AddScript(strScript)%0ADim%20results%20As%20Collection(Of%20PSObject)%20%3D%20P2.Invoke()%0AR2.Close()%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

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()
2 Replies
Best Response confirmed by vlciho (New Contributor)
Solution

@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'}"

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()