VB.NET - using textfile as source for menus and textboxes
- by Kenny Bones
Hi, this is probably a bit tense and I'm not sure if this is possible at all. 
But basically, I'm trying to create a small application which contains alot of PowerShell-code which I want to run in an easy matter. 
I've managed to create everything myself and it does work. But all of the PowerShell code is manually hardcoded and this gives me a huge disadvantage. 
What I was thinking was creating some sort of dynamic structure where I can read a couple of text files (possible a numerous amount of text files) and use these as the source for both the comboboxes and the richtextbox which provovides as the string used to run in PowerShell. 
I was thinking something like this:
Combobox - "Choose cmdlet" - Use "menucode.txt" as source
Richtextbox - Use "code.txt" as source
But, the thing is, Powershell snippets need a few arguments in order for them to work. 
So I've got a couple of comboboxes and a few textboxes which provides as input for these arguments. And this is done manually as it is right now. So rewriting this small application should also search the textfile for some keywords and have the comboboxes and textboxes to replace those keywords. And I'm not sure how to do this.
So, would this requre a whole lot of textfiles? Or could I use one textfile and separate each PowerShell cmdlet snippets with something? Like some sort of a header?
Right now, I've got this code at the eventhandler (ComboBox_SelectedIndexChanged)
        If ComboBoxFunksjon.Text = "Set attribute" Then
        TxtBoxUsername.Visible = True
    End If
    If chkBoxTextfile.Checked = True Then
        If txtboxBrowse.Text = "" Then
            MsgBox("You haven't choses a textfile as input for usernames")
        End If
        LabelAttribute.Visible = True
        LabelUsername.Visible = False
        ComboBoxAttribute.Visible = True
        TxtBoxUsername.Visible = False
        txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-QADUser -Identity $a -ObjectAttributes @{" & ComboBoxAttribute.SelectedItem & "='" & TxtBoxValue.Text & "'}" & vbCrLf & "}"
        If ComboBoxAttribute.SelectedItem = "Outlook WebAccess" Then
            TxtBoxValue.Visible = False
            CheckBoxValue.Visible = True
            CheckBoxValue.Text = "OWA Enabled?"
            txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-CASMailbox -Identity $a -OWAEnabled" & " " & "$" & CheckBoxValue.Checked & " '}" & vbCrLf & "}"
        End If
        If ComboBoxAttribute.SelectedItem = "MobileSync" Then
            TxtBoxValue.Visible = False
            CheckBoxValue.Visible = True
            CheckBoxValue.Text = "MobileSync Enabled?"
            Dim value
            If CheckBoxValue.Checked = True Then
                value = "0"
            Else
                value = "7"
            End If
            txtBoxCode.Text = "$users = Get-Content " & txtboxBrowse.Text & vbCrLf & "foreach ($a in $users)" & vbCrLf & "{" & vbCrLf & "Set-QADUser -Identity $a -ObjectAttributes @{msExchOmaAdminWirelessEnable='" & value & " '}" & vbCrLf & "}"
        End If
    Else
        LabelAttribute.Visible = True
        LabelUsername.Visible = True
        ComboBoxAttribute.Visible = True
        txtBoxCode.Text = "Set-QADUser -Identity " & TxtBoxUsername.Text & " -ObjectAttributes @{" & ComboBoxAttribute.SelectedItem & "='" & TxtBoxValue.Text & " '}"
        If ComboBoxAttribute.SelectedItem = "Outlook WebAccess" Then
            TxtBoxValue.Visible = False
            CheckBoxValue.Visible = True
            CheckBoxValue.Text = "OWA Enabled?"
            txtBoxCode.Text = "Set-CASMailbox " & TxtBoxUsername.Text & " -OWAEnabled " & "$" & CheckBoxValue.Checked
        End If
        If ComboBoxAttribute.SelectedItem = "MobileSync" Then
            TxtBoxValue.Visible = False
            CheckBoxValue.Visible = True
            CheckBoxValue.Text = "MobileSync Enabled?"
            Dim value
            If CheckBoxValue.Checked = True Then
                value = "0"
            Else
                value = "7"
            End If
            txtBoxCode.Text = "Set-QADUser " & TxtBoxUsername.Text & " -ObjectAttributes @{msExchOmaAdminWirelessEnable='" & value & "'}"
        End If
    End If
Now, this snippet above lets me either use a text file as a source for each username used in the powershell snippet. Just so you know :) And I know, this is probably coded as stupidly as it gets. But it does work! :)