Forums
New posts
Search forums
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
C# Community
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
VB.NET
VB.NET General Discussion
Unable to Retrieve String From Pipeline
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="shauno100, post: 177233, member: 53130"] Hi All, I don't know if anyone can assist but i have a weird issue where i am executing PowerShell code from Vb.NET via a Runspace to return the list of security groups we have setup in our Exchange servers that grant full access to a specified shared mailbox. Basically when i specify the SamAccountName of shared mailbox within double quotes "" the PowerShell code will return the group correctly but if i pass a variable to the code even though that variable 100% contains a string value the Pipeline returns nothing. it is a pretty complex setup i have but i am going to post all of the code if this can assist: 1. Firstly User selects the Display Name of a Shared resource from a listbox and clicks a button with the following code [CODE] Dim displayname As String = lbADUsers.SelectedItem.ToString() 'Get SAMAccountName of selected mailbox is Listbox. Dim objConnection3 = CreateObject("ADODB.Connection") objConnection3.Open("Provider=ADsDSOObject;") Dim objCommand3 = CreateObject("ADODB.Command") objCommand3.ActiveConnection = objConnection3 objCommand3.CommandText = "<LDAP://vbnedcs05.dpwservices.dpw.qld.gov.au>;(&(objectCategory=User)" & "(DisplayName=" & displayname & "));samaccountname;subtree" Dim objRecordSet3 = objCommand3.Execute SAMusername = CType(objRecordSet3.GetString, String) SAMusername = SAMusername.ToString() PassNAME = SAMusername TextBox2.Text = (RunScript(GetSRESGroup(PassNAME))).ToString() [/CODE] 2. The below functions are used with the PassNAME variable piped into them [CODE]Public Function GetSRESGroup(filter As String) As String Dim Script As New StringBuilder() Script.Append("Import-Module ActiveDirectory" + vbCrLf) Script.Append("$mailsession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://vbnecas03.dpwservices.dpw.qld.gov.au/PowerShell -Authentication Kerberos" + vbCrLf) Script.Append("Import-PSSession $mailsession | out-null" + vbCrLf) Script.Append("$SRESGroup = Get-Mailbox " + Chr(34) + filter + Chr(34) + " | Get-MailboxPermission | where {$_.user.tostring() -ne " + Chr(34) + "NT AUTHORITY\SELF" + Chr(34) + " -and $_.IsInherited -eq $false} | Select User" + vbCrLf) Script.Append("$SResGroup | ForEach-Object{" + vbCrLf) Script.Append("$String = $_.user" + vbCrLf) Script.Append("$String = " + Chr(34) + "$string" + Chr(34) + "" + vbCrLf) Script.Append("$String= $string.replace(" + Chr(34) + "DPWSERVICES\" + Chr(34) + "," + Chr(34) + "" + Chr(34) + ")" + vbCrLf) Script.Append("$String= $string.trimend(" + Chr(34) + "}" + Chr(34) + ")" + vbCrLf) Script.Append("$String" + vbCrLf) Script.Append("}" + vbCrLf) Return Script.ToString() End Function Public Function RunScript(ByVal scriptText As String) As String Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace() MyRunSpace.Open() Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline() MyPipeline.Commands.AddScript(scriptText) 'MyPipeline.Commands.Add("Out-String") Dim results As Collection(Of PSObject) = MyPipeline.Invoke() Dim MyStringBuilder As New StringBuilder() For Each obj As PSObject In results MyStringBuilder.AppendLine(obj.ToString()) Next Return MyStringBuilder.ToString() MyRunSpace.Close() End Function [/CODE] In conclusion Textbox2 is meant to display the output from the PowerShell script and the Pipeline after clicking the button. This works when i specify the SAMAccountName (e.g. TextBox2.Text = (RunScript(GetSRESGroup([B]"HPW-FINANCE-TEAM"[/B]))).ToString() and the textbox then displays the security group granting access correctly but if have it as TextBox2.Text = (RunScript(GetSRESGroup([B]PassNAME[/B]))).ToString() the textbox displays nothing but i can see that a blank line has been added as the cursor has moved down a line which means the pipeline was empty. Just to clarify PassNAME 100% contains the correct SAMAccountName as a string value, i even set the PassNAME variable to (PassNAME = "HPW-FINANCE-TEAM") as a test but the pipeline returned nothing. No idea what i am missing here. Any assistance would be much appreciated. [/QUOTE]
Insert quotes…
Verification
Post reply
VB.NET
VB.NET General Discussion
Unable to Retrieve String From Pipeline
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top
Bottom