Jump to content

Combobox Problem in Powershell


Der letzte Beitrag zu diesem Thema ist mehr als 180 Tage alt. Bitte erstelle einen neuen Beitrag zu Deiner Anfrage!

Empfohlene Beiträge

Hallo,

 

Nach ersten Gehversuchen mit Powershell und Sapien hab ich mir eine kleine Toolbox gebastelt um täglichen Aufgaben zu erledigen.

 

 

Jeder Suchbegriff wird in der $ComBox1 gespeichert.

Ich Suche einen Weg, um die items in der Combox abzuspeichern und beim nächsten Start wieder laden zu können.

Nebenbei wäre es schön, dabei die doppelten Einträge zu entfernen.

 

Happy Halloween allen!

 

 

 

 

 

Vielleicht kann jemand was damit anfangen:

 

import-module activedirectory#Generated Form Function
function GenerateForm {
########################################################################
# Code Generated By: SAPIEN Technologies PrimalForms (Community Edition) v1.0.10.0
# Generated On: 29.10.2013 10:54
# Generated By: peter
#########################################################################region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null# Load Visual Basic assembly
[void][system.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')#endregion#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$button12 = New-Object System.Windows.Forms.Button
$button11 = New-Object System.Windows.Forms.Button
$button10 = New-Object System.Windows.Forms.Button
$button9 = New-Object System.Windows.Forms.Button
$button8 = New-Object System.Windows.Forms.Button
$button7 = New-Object System.Windows.Forms.Button
$richTextBox1 = New-Object System.Windows.Forms.RichTextBox
$button6 = New-Object System.Windows.Forms.Button
$button5 = New-Object System.Windows.Forms.Button
$button4 = New-Object System.Windows.Forms.Button
$button3 = New-Object System.Windows.Forms.Button
$button2 = New-Object System.Windows.Forms.Button
$button1 = New-Object System.Windows.Forms.Button
#$textBox1 = New-Object System.Windows.Forms.TextBox # momentan unbenutzt
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState#endregion Generated Form Objects#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$prozessData6=
{
#TODO: Place custom script here}$prozessData5=
{
#TODO: Place custom script here}$prozessData1=
{
#TODO: Place custom script here}$prozessData10=
{
#TODO: Place custom script here}$prozessData11=
{
#TODO: Place custom script here}$prozessData7=
{
#TODO: Place custom script here}$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
 $form1.WindowState = $InitialFormWindowState
}#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 717
$System_Drawing_Size.Width = 742
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.Name = "form1"
$form1.Text = "IT-Service"#richTextBox1
$richTextBox1.Anchor = 15
$richTextBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 31
$System_Drawing_Point.Y = 109
$richTextBox1.Location = $System_Drawing_Point
$richTextBox1.Name = "richTextBox1"
#$richTextBox1.font = "Courier New"
$richTextBox1.Font = New-Object System.Drawing.Font("Courier New",9.5,1,3,0)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 608
$System_Drawing_Size.Width = 688
$richTextBox1.Size = $System_Drawing_Size
$richTextBox1.TabIndex = 7
$richTextBox1.Text = ""$form1.Controls.Add($richTextBox1)#comboBox1
$comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox1.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 31
$System_Drawing_Point.Y = 12
$comboBox1.Location = $System_Drawing_Point
$comboBox1.Name = "comboBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.Width = 135
$comboBox1.Size = $System_Drawing_Size
$comboBox1.TabIndex = 14
#$comboBox1.add_SelectedValueChanged($prozessData13)
$comboBox1.add_SelectedValueChanged({$FNprozessData13})$form1.Controls.Add($comboBox1)#Button12
$button12.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 612
$System_Drawing_Point.Y = 83
$button12.Location = $System_Drawing_Point
$button12.Name = "button12"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button12.Size = $System_Drawing_Size
$button12.TabIndex = 13
$button12.Text = "button12"
$button12.UseVisualStyleBackColor = $True
$button12.add_Click($prozessData12)$form1.Controls.Add($button12)
#Button11
$button11.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 496
$System_Drawing_Point.Y = 83
$button11.Location = $System_Drawing_Point
$button11.Name = "button11"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button11.Size = $System_Drawing_Size
$button11.TabIndex = 12
$button11.Text = "button11"
$button11.UseVisualStyleBackColor = $True
$button11.add_Click($prozessData11)$form1.Controls.Add($button11)
#Button10
$button10.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 380
$System_Drawing_Point.Y = 83
$button10.Location = $System_Drawing_Point
$button10.Name = "button10"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button10.Size = $System_Drawing_Size
$button10.TabIndex = 11
$button10.Text = "button10"
$button10.UseVisualStyleBackColor = $True
$button10.add_Click($prozessData10)$form1.Controls.Add($button10)
#Button9
$button9.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 263
$System_Drawing_Point.Y = 83
$button9.Location = $System_Drawing_Point
$button9.Name = "button9"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button9.Size = $System_Drawing_Size
$button9.TabIndex = 10
$button9.Text = "button9"
$button9.UseVisualStyleBackColor = $True
$button9.add_Click($prozessData9)$form1.Controls.Add($button9)
#Button8
$button8.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 148
$System_Drawing_Point.Y = 83
$button8.Location = $System_Drawing_Point
$button8.Name = "button8"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button8.Size = $System_Drawing_Size
$button8.TabIndex = 9
$button8.Text = "button8"
$button8.UseVisualStyleBackColor = $True
$button8.add_Click($prozessData8)$form1.Controls.Add($button8)
#Button7
$button7.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 31
$System_Drawing_Point.Y = 83
$button7.Location = $System_Drawing_Point
$button7.Name = "button7"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button7.Size = $System_Drawing_Size
$button7.TabIndex = 8
$button7.Text = "Grp. übertragen"
$button7.UseVisualStyleBackColor = $True
$button7.add_Click($prozessData7)
$button7.add_Click({FNprocessData7})$form1.Controls.Add($button7)
#Button6
$button6.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 611 #561
$System_Drawing_Point.Y = 52
$button6.Location = $System_Drawing_Point
$button6.Name = "button6"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button6.Size = $System_Drawing_Size
$button6.TabIndex = 6
$button6.Text = "Computer"
$button6.UseVisualStyleBackColor = $True
#$button6.add_Click($prozessData6)
$button6.add_Click({FNprocessData6})$form1.Controls.Add($button6)
#Button5
$button5.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 495 #455
$System_Drawing_Point.Y = 52
$button5.Location = $System_Drawing_Point
$button5.Name = "button5"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button5.Size = $System_Drawing_Size
$button5.TabIndex = 5
$button5.Text = "G. Mitglieder"
$button5.UseVisualStyleBackColor = $True
#$button5.add_Click($prozessData5)
$button5.add_Click({FNprocessData5})$form1.Controls.Add($button5)
#Button4
$button4.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 379 #349
$System_Drawing_Point.Y = 52
$button4.Location = $System_Drawing_Point
$button4.Name = "button4"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button4.Size = $System_Drawing_Size
$button4.TabIndex = 4
$button4.Text = "Gruppen"
$button4.UseVisualStyleBackColor = $True
#$button4.add_Click($prozessData4)
$button4.add_Click({FNprocessData4})$form1.Controls.Add($button4)
#Button3
$button3.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 263 #243
$System_Drawing_Point.Y = 52
$button3.Location = $System_Drawing_Point
$button3.Name = "button3"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button3.Size = $System_Drawing_Size
$button3.TabIndex = 3
$button3.Text = "User MemberOf"
$button3.UseVisualStyleBackColor = $True
#$button3.add_Click($prozessData3)
$button3.add_Click({FNprocessData3})$form1.Controls.Add($button3)
#Button2
$button2.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 147 #137
$System_Drawing_Point.Y = 52
$button2.Location = $System_Drawing_Point
$button2.Name = "button2"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.width = 110
$button2.Size = $System_Drawing_Size
$button2.TabIndex = 2
$button2.Text = "User"
$button2.UseVisualStyleBackColor = $True
#$button2.add_Click($prozessData2)
$button2.add_Click({FNprocessData2})$form1.Controls.Add($button2)
#Button1
$button1.DataBindings.DefaultDataSourceUpdateMode = 0$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 31
$System_Drawing_Point.Y = 52
$button1.Location = $System_Drawing_Point
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 25
$System_Drawing_Size.Width = 110
$button1.Size = $System_Drawing_Size
$button1.TabIndex = 1
$button1.Text = "Suche"
$button1.UseVisualStyleBackColor = $True
#$button1.add_Click($prozessData1)
$button1.add_Click({FNprocessData1})$form1.Controls.Add($button1)#$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
#$System_Drawing_Point = New-Object System.Drawing.Point
#$System_Drawing_Point.X = 31
#$System_Drawing_Point.Y = 12
#$textBox1.Location = $System_Drawing_Point
#$textBox1.Name = "textBox1"
#$System_Drawing_Size = New-Object System.Drawing.Size
#$System_Drawing_Size.Height = 25
#$System_Drawing_Size.Width = 150
#$textBox1.Size = $System_Drawing_Size
#$textBox1.TabIndex = 0
#$textBox1.Text = "knobldie"$form1.Controls.Add($textBox1)#endregion Generated Form Code#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null} #End Function#Button 1
function FNprocessData1 {
  # Read the contents of the textBox1 and perfom desired command with it
  # $net = net user ($textBox1.text)
 
  $gname = $combobox1.text
  $g1name = "*"+$gname+"*"
  $ComboBox1.Items.Add($gname)
  $comboBox1.sorted = $true;
   
    get-aduser -Filter "name -like '$g1name'" -properties *| sort name | ft -auto Name,SamAccountname,telephonenumber > c:\temp\result1.txt
   
  $net = get-content c:\temp\result1.txt   
  $richTextBox1.Clear()
  # Parse each line of the result, add a linebreak and append it to the RichTextBox
  foreach ($line in $net) {
      $richTextBox1.Appendtext($line+[char]13+[char]10)
  }
 
 }#Button 2
function FNprocessData2 {
  # Read the contents of the textBox1 and perfom desired command with it
  # $net = net user ($textBox1.text)
  $gname = $combobox1.text
  $ComboBox1.Items.Add($gname)
    get-aduser $gname -properties * | select Name,Department,SamAccountname,ScriptPath,ProfilePath,lastlogondate,city,telephonenumber,created,Modified,ChangePasswordAtLogon,PasswordExpired,PasswordLastSet,PasswordNeverExpires,PasswordNotRequired,CannotChangePassword,LastBadPasswordAttempt,logoncount,publicDelegates,publicDelegatesBL,mDBStorageQuota,mDBOverQuotaLimit > c:\temp\result1.txt
 
  $net = get-content c:\temp\result1.txt   
  $richTextBox1.Clear()
  # Parse each line of the result, add a linebreak and append it to the RichTextBox
  foreach ($line in $net) {
      $richTextBox1.Appendtext($line+[char]13+[char]10)
  }
 
 }
 
 #Button 3 User ist Mitglied von?
function FNprocessData3 {
  # Read the contents of the textBox1 and perfom desired command with it
  $gname = $combobox1.text
  $ComboBox1.Items.Add($gname)
        Get-ADUser $gname -properties * | select-object -ExpandProperty memberof | Get-ADGroup | sort name | select name > c:\temp\result1.txt
         $file = "c:\temp\result1.txt"
          (Get-Content $file | Select-Object -Skip 3) | Set-Content $file
   
  $net = get-content c:\temp\result1.txt
  $richTextBox1.Clear()                                              #cls
  # Parse each line of the result, add a linebreak and append it to the RichTextBox
  foreach ($line in $net) {
      $line=$line.trim()                                             #Drecks Leerzeichen entfernen für schöneres copy und paste
      $richTextBox1.Appendtext($line+[char]13+[char]10)
      $line >> c:\temp\result2.txt                                   # for later use
  }
 
 } #Button 4
function FNprocessData4 {
  # Read the contents of the textBox1 and perfom desired command with it
  # $net = net user ($textBox1.text)
  $gname = $ComboBox1.text
  $ComboBox1.Items.Add($gname)
  $g1name = "*"+$gname+"*"   get-adgroup -Filter "name -like '$g1name'" | foreach { $_.Samaccountname } > c:\temp\result1.txt
       
 
  $net = get-content c:\temp\result1.txt   
  $richTextBox1.Clear()
  # Parse each line of the result, add a linebreak and append it to the RichTextBox
  foreach ($line in $net) {
      $richTextBox1.Appendtext($line+[char]13+[char]10)
  }
 
 }
 #Button 5
function FNprocessData5 {
  # Read the contents of the textBox1 and perfom desired command with it
  # $net = net user ($textBox1.text)
  $gname = $ComboBox1.text
  $ComboBox1.Items.Add($gname)
  $g1name = "*"+$gname+"*"   get-adgroup -Filter "name -like '$g1name'" | foreach { $_.Samaccountname } > c:\temp\result1.txt
  
   $groups = gc c:\temp\result1.txt     $report = @()
       Foreach ($group in $groups) {
        Get-ADGroupMember -Identity $group | % {
         $report += New-Object PSObject -Property @{
           Group = $group
           Name1 = $_.samaccountname
           Name2 = $_.name
    }
  }
}
$report | ft -Autosize  > c:\temp\result1.txt   
 
  $net = get-content c:\temp\result1.txt   
  $richTextBox1.Clear()
  # Parse each line of the result, add a linebreak and append it to the RichTextBox
  foreach ($line in $net) {
      $richTextBox1.Appendtext($line+[char]13+[char]10)
  }
 
 }
 
 
  #Button 6
function FNprocessData6 {
  # Read the contents of the textBox1 and perfom desired command with it
  # $net = net user ($textBox1.text)
  $richTextBox1.Clear()
  $gname = $ComboBox1.text
  $ComboBox1.Items.Add($gname)
 
   ping -n 3 $gname | out-string -Stream | foreach-object {
    $richTextBox1.lines = $richTextBox1.lines + $_
    $richTextBox1.Select($richTextBox1.Text.Length, 0)
    $richTextBox1.ScrollToCaret()
    $form1.Update()}  
   get-adcomputer $gname -properties * > c:\temp\result1.txt
       
 
  $net = get-content c:\temp\result1.txt   
 
  # Parse each line of the result, add a linebreak and append it to the RichTextBox
  foreach ($line in $net) {
      $richTextBox1.Appendtext($line+[char]13+[char]10)
  }
 
 }
  #Button 7
function FNprocessData7 {
  # Read the contents of the textBox1 and perfom desired command with it
  # $net = net user ($textBox1.text)
  #$richTextBox1.Clear()
  #$sourceacc = $textBox1.text
  #$ComboBox1.Items.Add($sourceacc)
 
  # Checks if both accounts are provided as an argument, otherwise prompts for input
#if (-not $sourceacc) { $sourceacc = [Microsoft.VisualBasic.Interaction]::InputBox("Quelluser eingeben ... ", "Quelle", "") }
#if (-not $destacc) { $destacc = [Microsoft.VisualBasic.Interaction]::InputBox("Zieluser eingeben ... ", "Ziel", "") }  # prompt for input
$sourceacc = [Microsoft.VisualBasic.Interaction]::InputBox("Quelluser eingeben ... ", "Quelle", "")
$destacc = [Microsoft.VisualBasic.Interaction]::InputBox("Zieluser eingeben ... ", "Ziel", "") # Retrieves the group membership for both accounts, if account is not found or error is generated the object is set to $null
try { $sourcemember = get-aduser -filter {samaccountname -eq $sourceacc} -property memberof | select memberof }
catch { $sourcemember = $null}
try { $destmember = get-aduser -filter {samaccountname -eq $destacc} -property memberof | select memberof }
catch { $destmember = $null}# Checks if accounts have group membership, if no group membership is found for either account script will exit
if ($sourcemember -eq $null) {[Microsoft.VisualBasic.Interaction]::MsgBox("Quelluser nicht gefunden",0,"Exit Message");return}
if ($destmember -eq $null) {[Microsoft.VisualBasic.Interaction]::MsgBox("Zieluser nicht gefunden",0,"Exit Message");return}# Checks for differences, if no differences are found script will prompt and exit
if (-not (compare-object $destmember.memberof $sourcemember.memberof)) {
 [Microsoft.VisualBasic.Interaction]::InputBox("Kein Unterschied zwischen den $sourceacc & $destacc Gruppenmitgliedschaften gefunden. $destacc wird keinen zusätzlichen Gruppen hinzugefügt.",0,"Exit Message");return
}# Prompt for adding user to groups, only prompt when there are changes
if (compare-object $destmember.memberof $sourcemember.memberof | where-object {$_.sideindicator -eq '=>'}) {
 $ConfirmAdd = [Microsoft.VisualBasic.Interaction]::MsgBox("Möchtest Du `'$($destacc)`' zu den folgenden Gruppen hinzufügen?:`n`n$((compare-object $destmember.memberof $sourcemember.memberof |
 where-object {$_.sideindicator -eq '=>'} | select -expand inputobject | foreach {([regex]::split($_,'^CN=|,.+$'))[1]}) -join "`n")",4,"Bitte bestätigen")
}# Prompt for removing user from groups, only prompt when there are changes
if (compare-object $destmember.memberof $sourcemember.memberof | where-object {$_.sideindicator -eq '<='}) {
 $ConfirmRemove = [Microsoft.VisualBasic.Interaction]::MsgBox("Möchtest Du `'$($destacc)`' von den folgenden Gruppen entfernen?:`n`n$((compare-object $destmember.memberof $sourcemember.memberof |
 where-object {$_.sideindicator -eq '<='} | select -expand inputobject | foreach {([regex]::split($_,'^CN=|,.+$'))[1]}) -join "`n")",4,"Bitte bestätigen")
}# If the user confirmed adding the groups to the account, the user will be added to the groups
if ($ConfirmAdd -eq "Yes") {
 compare-object $destmember.memberof $sourcemember.memberof | where-object {$_.sideindicator -eq '=>'} |
 select -expand inputobject | foreach {add-adgroupmember "$_" $destacc}
}# If the user confirmed removing any groups not present on the source account, the user will be removed from the groups
if ($ConfirmRemove -eq "Yes") {
 compare-object $destmember.memberof $sourcemember.memberof | where-object {$_.sideindicator -eq '<='} |
 select -expand inputobject | foreach {remove-adgroupmember "$_" $destacc}
}# Prompt after executing script
[Microsoft.VisualBasic.Interaction]::MsgBox("Script erfolgreich ausgeführt",0,"Exit Message")
 }#ComboBox1
function FNprocessData13 {
  # Read the contents of the textBox1 and perfom desired command with it
  # $net = net user ($textBox1.text)
  $textBox1.text = $ComboBox1.text
 
 
 }   #Call the Function
GenerateForm 

Link zu diesem Kommentar
Der letzte Beitrag zu diesem Thema ist mehr als 180 Tage alt. Bitte erstelle einen neuen Beitrag zu Deiner Anfrage!

Schreibe einen Kommentar

Du kannst jetzt antworten und Dich später registrieren. Falls Du bereits ein Mitglied bist, logge Dich jetzt ein.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Editor-Fenster leeren

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...