Unable to Retrieve String From Pipeline

shauno100

Member
Joined
Oct 10, 2016
Messages
8
Programming Experience
1-3
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
VB.NET:
        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()

2. The below functions are used with the PassNAME variable piped into them
VB.NET:
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

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("HPW-FINANCE-TEAM"))).ToString() and the textbox then displays the security group granting access correctly but if have it as TextBox2.Text = (RunScript(GetSRESGroup(PassNAME))).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.
 
Top Bottom