<# WaitForAD Beispiel für die Variablen: [string]$VMName = "SERVER-VM01" [System.Management.Automation.PSCredential] $tempCred = new-object -typename System.Management.Automation.PSCredential ("Domain\Admin", (ConvertTo-SecureString P@ssw0rd! -AsPlainText -Force)) Oder ggfs. die Variablen abfragen: $VMName = Read-Host "Bitte den Namen des virtuellen Computers eingeben" $tempCred = Get-Credential #> function WaitForAD { param ( [Parameter(Mandatory=$true)] [String] $VMName, [Parameter(Mandatory=$true)] [System.Management.Automation.PSCredential] $Credentials ) $VM = Get-VM -Name $VMName $Uptime = $VM.Uptime.TotalSeconds $Date = $(Get-Date).AddSeconds(-($Uptime)) Write-Host "[$($VMName)]:: Auf Active Directory warten" -ForegroundColor Yellow -NoNewline Invoke-Command -VMName $VMName -Credential $Credentials -ArgumentList $Date -ScriptBlock { param($Date) do { do { Write-Host "." -ForegroundColor Yellow -NoNewline Start-Sleep -Seconds 5 $AD = get-service ADWS, DNS, KDC, NETLOGON, NTDS } until ($AD[0].Status -eq "running" -and $AD[1].Status -eq "running" -and $AD[2].Status -eq "running" -and $AD[3].Status -eq "running" -and $AD[4].Status -eq "running") Write-Host "." -ForegroundColor Yellow -NoNewline Start-Sleep -Seconds 5 $Event = Get-EventLog -LogName "Active Directory Web Services" -Source ADWS -After $Date -InstanceId 1073743024 -Newest 1 } until ($Event.Message -match "Die angegebene Verzeichnisinstanz wird nun von Active Directory-Webdiensten bedient" -or $Event.Message -match "Active Directory Web Services is now servicing the specified directory instance") } Write-Host " -> Ok!" -ForegroundColor Yellow } [string]$VMName = "SERVER-VM01" [System.Management.Automation.PSCredential] $tempCred = new-object -typename System.Management.Automation.PSCredential ("Domain\Administrator", (ConvertTo-SecureString P@ssw0rd! -AsPlainText -Force)) WaitForAD $VMName $tempCred