Powershell асинхр.

i am working with powershell and wpf to build a gui. I want to change the content of a button while runtime of the tool. But it got changed when the other commands are finished. I tried to start the command as job because i read that powershell works synchronously. Even if the command to change the content is in the first position

try { $btnCreateUser1.Content = "Bitte warten..." $oldinformationJob = Invoke-Command -AsJob -ComputerName test -Credential $cred -ScriptBlock {Get-Aduser -identity $using:ReferenceUserTextBox.Text -Properties * -Credential $Using:cred} $oldinformation = Wait-Job $oldinformationJob | Receive-Job $Passwort = 'test' $Attributes = @{ SAMAccountname = $NewUserUsernameTextBox.Text UserPrincipalName = "$($NewUserUsernameTextBox.Text)@test.de" Name = "$($FirstNameTextBox.Text) $($LastNameTextBox.Text)" AccountPassword = (ConvertTo-SecureString $Passwort -AsPlainText -Force) Path = ($oldinformation.DistinguishedName).Split(",",2)[1] GivenName = $FirstNameTextBox.Text Surname = $LastNameTextBox.text Enabled =$true DisplayName = "$($LastNameTextBox.Text), $($FirstNameTextBox.Text)" City = "" PostalCode = "" Country = "" State = "" Company = $oldinformation.company StreetAddress = $oldinformation.StreetAddress OfficePhone = $NummerTextBox.text Department = $oldinformation.Department Office = $RaumTextBox.Text Title = $PositionBox.Text HomeDirectory = "\\test\user\Home\$($NewUserUsernameTextBox.Text)" Homedrive = "U" } Invoke-Command -AsJob -ComputerName test -Credential $cred -ScriptBlock { New-ADUser @Using:Attributes -Credential $Using:cred } Start-Sleep -Seconds 5 Invoke-Command -AsJob -ComputerName test -Credential $cred -ScriptBlock {Get-ADPrincipalGroupMembership -Identity $($Using:oldinformation.SAMAccountname) -ResourceContextServer "test" -Credential $Using:cred | ForEach-Object {Add-ADGroupmember -Identity $_.SAMAccountname -Members $($Using:Attributes.SAMAccountname) -Credential $Using:cred -ErrorAction SilentlyContinue}} [System.Windows.Forms.MessageBox]::Show("Der User $($NewUserUsernameTextBox.Text) wurde erstellt ","Active Directory Tool",0) $btnCreateUser1.Content = "Benutzer erstellen" 

Ray61's user avatar

See: Using Dispatcher Invoke with a Runspace in Powershell

WPF controls, like the button in your script, can only be updated from the UI thread. If you’re trying to update a control from a different thread (like a job thread), you’ll need to use the Dispatcher.Invoke method to marshal the call to the UI thread.

$btnCreateUser1.Dispatcher.Invoke({ $btnCreateUser1.Content = "Bitte warten..."
}, [System.Windows.Threading.DispatcherPriority]::Background)

Martin Iszac's user avatar

:/>  Ключи активации Office (Для всех версий Офиса) свежие 2021