views:

220

answers:

1

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! :)

A: 

It'll probably get fairly horrible to debug when you've been away from it for a year and are trying to figure out how it hangs together, but if you're doing it I'd suggest that this might be a good time to use XML, since you can then create a nice structure to the files easily that will be fairly easy to understand.

<commands>
    <command>
        <name>Cmd1</name>
        <buttonText>NiceName</buttonText>
        <parameters>
            <parameter>textBox1</parameter>
            <parameter>textBox2</parameter>
            <parameter>comboBox1</parameter>
        </parameters>
        <code>your code here</code> 
    </command>
    <command>
    ...
    </command>
</commands>

You'd have to be careful if your script code contains any characters that can interfere with the xml though.

ho1