Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2314

Powershell AD User Identity eingabe

$
0
0

Hallo,


ich bekomme bei meinem Script folgende Meldung:

Cmdlet Set-ADUser an der Befehlspiplinepostion 1
Geben Sie Werte für die folgenden Parameter an: 
Identity:

Script:

# 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()

.CSV Datei

givenname;department;surname;officephone;samaccountname
Frank;AU;Mustermann;+49 40 ***;Mustermann-F
Claus;MaWI / extern / PPS;Lustig;+49 ** ;Wilkens-PPS
Hans;BR;Muster - Betriebsrat;+49 40**;BR-Muster
Peter;BR;Mann - Betriebsrat;+49 40 ***;BR-Mann
Christian;BR;Meier - Betriebsrat;+49 40 ***;BR-Meier


Die * bei der Telefonnummer sind nur Lückenfüller.


Viewing all articles
Browse latest Browse all 2314


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>