Hallo,
Ich habe ein Powershell script was ich gerne wiederholen lassen möchte und nicht immer ernuet aufrufen möchte.
Powershell script:
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() $princ = New-Object System.Security.Principal.WindowsPrincipal($identity) if(!$princ.IsInRole( ` [System.Security.Principal.WindowsBuiltInRole]::Administrator)) { $powershell = [System.Diagnostics.Process]::GetCurrentProcess() $psi = New-Object System.Diagnostics.ProcessStartInfo $powerShell.Path $script = $MyInvocation.MyCommand.Path $prm = $script foreach($a in $args) { $prm += ' ' + $a } $psi.Arguments = $prm $psi.Verb = "runas" [System.Diagnostics.Process]::Start($psi) | Out-Null return; } # Bildschirm ‘leeren’ cls Import-Module ActiveDirectory Write-Host ”Active Directory Module geladen!!!” # Das Auswahl-Menu Write-Host “Was willst Du tun?” Write-Host "1. Bearbeiten ADUser" Write-Host “2. Export ZBS” Write-Host “3. Export CTA” Write-Host "4. Export CTB" Write-Host “5. Bye” Write-Host ” “ # Die einzige Variable $a = Read-Host “Deine Wahl“ Write-Host ”Vorgang war erfolgreich!” # switch-case | Auswahl Abfangen und Verarbeiten switch ($a) { 1 { # startet das Skript im STA-Mode neu, wenn dies erforderlich sein sollte. If ($host.Runspace.ApartmentState -ne 'STA') { write-host "Script is not running in STA mode. Switching " $Script = $MyInvocation.MyCommand.Definition Start-Process powershell.exe -ArgumentList "-sta $Script" Exit } # Grundform Function Form { [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null [Reflection.Assembly]::LoadWithPartialName("System.Drawing") | Out-Null [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.VisualStyles") | Out-Null $ProgressForm = New-Object System.Windows.Forms.Form $ProgressForm.Text = 'Bitte warten...' $ProgressForm.Width = 350 $ProgressForm.Height = 144 $ProgressForm.MaximizeBox = $False $ProgressForm.MinimizeBox = $False $ProgressForm.ControlBox = $False $ProgressForm.ShowIcon = $False $ProgressForm.StartPosition = 1 $ProgressForm.Visible = $False $ProgressForm.FormBorderStyle = "FixedDialog" #$ProgressForm.WindowState = "Normal" $InText = New-Object System.Windows.Forms.Label $InText.Text = 'Die Anfrage wird bearbeitet...' $InText.Location = '18,26' $InText.Size = New-Object System.Drawing.Size(330,18) $progressBar1 = New-Object System.Windows.Forms.ProgressBar $ProgressBar1.Name = 'LoadingBar' $ProgressBar1.Style = "Marquee" $ProgressBar1.Location = "17,61" $ProgressBar1.Size = "300,18" $ProgressBar1.MarqueeAnimationSpeed = 40 $ProgressForm.Controls.Add($InText) $ProgressForm.Controls.Add($ProgressBar1) $sharedData.Form = $ProgressForm # Speichern der Form in die Hashtable [System.Windows.Forms.Application]::EnableVisualStyles() [System.Windows.Forms.Application]::Run($ProgressForm) #[System.Windows.Forms.Application]::DoEvents() } # Hashtable für Datenaustausch zwischen den Threads $sharedData = [HashTable]::Synchronized(@{}) $sharedData.Form = $Null # Thread eigener Runspace mit der Hashtable $newRunspace = [RunSpaceFactory]::CreateRunspace() $newRunspace.ApartmentState = "STA" $newRunspace.ThreadOptions = "ReuseThread" $newRunspace.Open() $newRunspace.SessionStateProxy.setVariable("sharedData", $sharedData) # Thread eigene asynchrone Powershell $PS = [PowerShell]::Create() $PS.Runspace = $newRunspace $PS.AddScript($Function:Form) $AsyncResult = $PS.BeginInvoke() # -------------------------------------------------------- # Eigentliches Script #AD-Module importieren Import-Module ActiveDirectory [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $dialog = New-Object System.Windows.Forms.OpenFileDialog $dialog = New-Object System.Windows.Forms.OpenFileDialog $dialog.DefaultExt = '.csv' $dialog.Filter = 'CSV Datei|*.csv|All Files|*.*' $dialog.FilterIndex = 0 $dialog.InitialDirectory = $home $dialog.Multiselect = $false $dialog.RestoreDirectory = $true $dialog.Title = "Select a csv file" $dialog.ValidateNames = $true $dialog.ShowDialog() $dialog.FileName $script:ErrorActionPreference = "silentlyContinue" #Eingabeaufforderung #$Pfad = Read-Host "Bitte Pfad zur .CSV Datei angeben #Beispiel: C:\users\Administrator\desktop\powershell\Export_cherwell_bearbeitet.csv" #CSV-Datei importieren $DatenSaetze = Import-Csv $dialog.FileName -UseCulture -Encoding UTF8 #Jeden Datensatz der CSV-Datei durchlaufen ForEach ($DatenSatz in $DatenSaetze){ # Set-AdUser-Aufruf mit dem SamAccountName des derzeitigen CSV-Eintrags initieren. $Kommando = "Set-AdUser $($Datensatz.samAccountName)" # Für jeden CSV-Eintrag die entsprechenden Attribute durchlaufen ForEach ($Attribut in (Get-Member -InputObject $DatenSatz -MemberType NoteProperty)){ $Wert = $DatenSatz.($Attribut.Name) #Überprüfen, ob Wert nicht leer ist und nicht den SamAccountName enthält if ($Wert -and ($Wert.Name -ne 'samAccountName')){ # Zuvor initierten Aufruf von Set-AdUser um entsprechende Werte erweitern $Kommando+= " -$($Attribut.Name) '$Wert'" } } #Inhalt von $Kommando mittels Invoke-Expression ausführen Invoke-Expression $Kommando } # -------------------------------------------------------- # Form schließen If($sharedData.Form) { $sharedData.Form.close() } # neue Powershell beenden und Objekt entfernen $PS.Endinvoke($AsyncResult) $PS.dispose() break; } 2 { Get-ADUser -SearchBase "ou=user,ou=ZBS,ou=hhla,dc=myad,dc=local" -Filter "Enabled -eq 'True'" -Properties * | Select-Object -Property SamAccountName, GivenName, sn, telephonenumber, company, department, emailaddress, displayname, name | Export-csv C:\users\administrator\export_ad_zbs.csv -Encoding UTF8 break; } 3 { Get-ADUser -SearchBase "ou=user,ou=CTA,ou=hhla,dc=myad,dc=local" -Filter "Enabled -eq 'True'" -Properties * | Select-Object -Property SamAccountName, GivenName, sn, telephonenumber, company, department, emailaddress, displayname, name | Export-csv C:\Users\Administrator\export_ad_cta.csv -Encoding UTF8 break; } 4 { Get-ADUser -SearchBase "ou=user,ou=CTB,ou=hhla,dc=myad,dc=local" -Filter "Enabled -eq 'True'" -Properties * | Select-Object -Property SamAccountName, GivenName, sn, telephonenumber, company, department, emailaddress, displayname, name | Export-csv C:\Users\administrator\export_ad_ctb.csv -Encoding UTF8 break; } 5 { break; } } $b = Read-Host “Bitte drücken Sie eine Taste“
Vielen dank für die hilfe.