2009-09-30 25 views
0

這可能有點緊張,我不確定這是否可能。 但基本上,我試圖創建一個小應用程序,其中包含許多PowerShell代碼,我想以簡單的方式運行它。VB.NET - 使用文本文件作爲菜單和文本框的源代碼

我已經成功地創造了一切,它確實有效。但是所有的PowerShell代碼都是手動硬編碼的,這給我一個巨大的缺點。

我在想什麼是創建某種動態結構,在這裏我可以讀取幾個文本文件(可能有大量的文本文件),並將它們用作組合框和richtextbox的源代碼,用於在PowerShell中運行的字符串。

我的想法是這樣的:

組合框 - 「選擇小命令」 - 使用 「menucode.txt」 源 的RichTextBox - 使用 「code.txt」 源

但是,事情是,Powershell片段需要一些參數才能使其工作。 所以我有幾個組合框和幾個文本框提供了這些參數的輸入。這是手動完成,因爲它是現在。因此,重寫這個小應用程序還應該搜索文本文件中的某些關鍵字,並使用組合框和文本框來替換這些關鍵字。我不知道如何做到這一點。

那麼,這是否需要大量的文本文件?或者我可以使用一個文本文件並將每個PowerShell cmdlet片段與某些內容分開?像某種頭像?


現在,我有了這個代碼在事件處理程序(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 

,上面這段代碼讓我既可以使用文本文件作爲在使用的每個用戶名的來源PowerShell的片段。就這樣你知道:)而且我知道,這可能被編碼爲愚蠢的,因爲它得到。但它確實有用! :)

回答

0

它可能會相當可怕的調試時,你已經離開了一年,並試圖弄清楚它是如何掛在一起,但如果你這樣做,我會建議這可能是使用XML的好時機,因爲您可以輕鬆地爲文件創建一個很好的結構,這將很容易理解。

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

如果您的腳本代碼包含任何可能干擾xml的字符,您必須小心。