i've created a WPF PowerShell application which functions OK. But sometimes when entering another user ID it gets on not responding for a while before showing some output. I've searched the internet and I can resolve this by adding runspaces. But I need some help inserting this in my script.. Or is there another way to make it more responsive?
Below my script:
## Add .xaml file, remove errors and set variables to use them in this script
Add-Type -AssemblyName PresentationFramework
$xamlFile="\\zeus\a\lrc\GROUPS\WCO\USR\STU_DATA\EXPERT\EXPERT\ServiceDesk_guide\SCript\SD-Tool\MainWindow.xaml"
$inputXAML = Get-Content -Path $xamlFile -Raw
$inputXAML = $inputXAML -replace 'mc:Ignorable="d"',''-replace "x:N","N" -replace '^<Win.*','<Window'
[XML]$XAML =$inputXAML
$reader = New-Object System.Xml.XmlNodeReader $XAML
try{
$psform = $psform = [Windows.Markup.XamlReader]::Load($reader)
}catch{
Write-host $_.Exception
throw
}
$xaml.SelectNodes("//*[@Name]") | ForEach-Object {
Try{
Set-Variable -Name "var_$($_.Name)" -value $psform.FindName($_.Name) -ErrorAction stop
}catch{
throw
}
}
Get-Variable var_*
## Enter the search burtton to execute the script
Function OnClick {
#Params
$Computernames = Get-Content -Path "\\zeus\a\lrc\GROUPS\WCO\USR\STU_DATA\EXPERT\EXPERT\ServiceDesk_guide\SCript\Data\Computers in a specific workgroup or domain.csv" |
Select-Object -Skip 3 |
ConvertFrom-Csv -Delimiter ","
$pcNamecsv = $Computernames.Where({ $_."Details_Table0_User_Name0" -eq $var_userName.Text})."Details_Table0_Name0" | Where { $_ -like 'BNL0*' -or $_ -like 'BNL5*'}
$script:wwg00m = "wwg00m.rootdom.net"
$dedicatedSb = "OU=vClientD,OU=AZ-BENE,OU=E2,OU=VDI,OU=Prod,OU=AVC,OU=Clients,DC=wwg00m,DC=rootdom,DC=net"
$Mailboxrights = Import-csv "\\zeus\a\lrc\GROUPS\WCO\USR\STU_DATA\EXPERT\EXPERT\ServiceDesk_guide\SCript\Data\mailboxRights2.csv" -delimiter "|"
# if hostname >1 search for hostname last used
$ADcomputing = @(
'Name'
'LastLogonDate'
)
$ADproperty = foreach ($PC in $pcNamecsv)
{Get-ADcomputer $PC -Properties $ADcomputing}
$global:pcName = ($ADproperty | Sort LastLogonDate -Descending | Select -First 1).Name
# Search for IP address with nslookup
$ipAddress = (nslookup $global:pcName | Select-String Address | Where-Object LineNumber -eq 5).ToString().Split(' ')[-1]
# find properties in Active Directory
$adProperties = @(
'Name'
'mail'
'OfficePhone'
'LockedOut'
'Enabled'
'msDS-UserPasswordExpiryTimeComputed'
'AccountExpirationDate'
'Givenname'
'DisplayName'
)
$wwgUser = Get-ADuser -Identity $var_userName.Text -Server $wwg00m -Properties $adProperties
$agfUser = Get-ADuser -Identity $var_userName.Text -Properties $adProperties
$agfExpiry = Get-ADUser -identity $var_userName.Text –Properties "msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property @{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} |
select -expandproperty "ExpiryDate" -first 1
$wwgExpiry = Get-ADUser -identity $var_userName.Text -server $wwg00m –Properties "msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property @{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} |
select -expandproperty "ExpiryDate" -first 1
# Ccheck if VPN connection was made before accessing Windows environment
$windowsLogon = (Get-Childitem -path \\$pcName\c$\Users\*\AppData\Local\Temp\logon.txt | Sort "LastWriteTime" -Descending | Select -First 1).LastWriteTime
$vpnLogon = (Get-ADuser $var_userName.Text -server mgm.agf.be -properties Modified).Modified
# Find properties in Active Directory from nested AVC groups
$SID = (Get-ADuser $var_userName.Text -Properties SID).SID -replace 'SID',''
$AVCSEC = (Get-ADGroup -LDAPFilter "(member=CN=$SID,CN=ForeignSecurityPrincipals,DC=wwg00m,DC=rootdom,DC=net)" `
-Server wwg00m.rootdom.net -Properties Name).Name
$DedicatedProperties = @(
'Name'
'Description')
$dedicatedClients = Get-ADcomputer -filter * -SearchBase $dedicatedSb -server $wwg00m -properties $DedicatedProperties
$dedicatedclientName = $dedicatedClients.Where({ $_."Description" -Match $SID})."Name"
# Shared mailbox properties
$Mailbox = $Mailboxrights.Where({$_."Trustee" -match $var_userName.Text}) | Select-Object "Display Name", "Email Address", "Rights","Domain"
# Computer information, hostname obtained from csv list
$model = (Get-WmiObject -Class Win32_ComputerSystem -ComputerName $pcName -ea SilentlyContinue ).Model
$osVersion = (Get-WMIObject win32_operatingsystem -ComputerName $pcName -ea SilentlyContinue ).Version
$osSystem = (Get-ADcomputer $global:pcName -properties operatingsystem -ea SilentlyContinue ).operatingsystem
# Bitlocker recover key
$objComputer = Get-ADComputer $global:pcName
$Bitlocker_Object = Get-ADObject -Filter {objectclass -eq 'msFVE-RecoveryInformation'} -SearchBase $objComputer.DistinguishedName -Properties 'msFVE-RecoveryPassword'
$bitlockerKey = ($Bitlocker_Object | select msFVE-RecoveryPassword)."msFVE-RecoveryPassword"
## Actual binding from Powershell to WPF all info binded with TextBlocks
# Main Tabitem
$var_fullname.Text = $agfUser.DisplayName
$var_phone.Text = $agfUser.OfficePhone
$var_hostname.Text = $global:pcName
$var_mail.Text = $agfUser.mail
$var_enabled.Text = $agfUser.enabled
$var_exp_acc.Text = $agfUser.AccountExpirationDate
$var_exp_pass.Text = $agfExpiry
$var_locked.Text = $agfUser.LockedOut
# wwg00m Tabitem
$var_enabled_wwg00m.Text = $wwgUser.enabled
$var_exp_acc_wwg00m.Text = $wwgUser.AccountExpirationDate
$var_exp_pass_wwg00m.Text = $wwgExpiry
$var_locked_wwg00m.Text = $wwgUser.LockedOut
# Computer Tabitem
$var_osName.Text = $osVersion
$var_model.Text = $model
$var_iAddress.Text = $ipAddress
$var_bkey.Text = $bitlockerKey
$var_cName.Text = $global:pcName
$var_vName.Text = $osSystem
# Used in more then 1 Tabitem
$var_vpnLog.Text = $vpnLogon
$var_windowsLog.Text = $windowsLogon
$var_ipCurrent.Text = $ipAddress
# AVC Tabitem
$pooledEnv = If ($AVCSEC -like "AVC-P-POOL-AZBENE-VDIW10*") {
$var_Pooled.Text = "Available"}
Else { $var_Pooled.Text = "Not available"}
$dedicatedEnv = If ($AVCSEC -like "AVC-P-POOL-AZBENE-DCW10*") {
$var_Dedicated.Text = "Available"}
Else { $var_Dedicated.Text = "Not available"}
$uatEnv = If ($AVCSEC -like "AVC-QS-POOL-AZBENE-VDIW10*") {
$var_uat.Text = "Available"}
Else { $var_uat.Text = "Not available"}
$pooledRing0 = If ($AVCSEC -like "AVC-P-POOL-AZBENE-RING0-VDIWIN10") {
$var_poolRing0.Text ="Available"}
Else { $var_poolRing0.Text = "Not available"}
$dedicatedRing0 = If ($AVCSEC -like "AVC-P-POOL-AZBENE-RING0-DEDWIN10") {
$var_dedicatedRing0.Text = "Available"}
Else { $var_dedicatedRing0.Text = "Not available"}
$softAdmin = If ($AVCSEC -like "AVC-P-CONF-DCW10-AdminLight"){
$var_adminLight.Text = "Available"}
Else {$var_adminLight.text = "Not Available"}
$var_hostNamededicated.Text = $dedicatedclientName
# DataGrid2 Mail TabItem
$var_dataGrid2.ItemsSource = $Mailbox
# unlock agf account button
$var_unlockAGF.Add_Click{Unlock-ADAccount -Identity $var_userName.Text
Start-Sleep -Seconds 2
[System.Windows.MessageBox]::Show('Account has been unlocked')}
## DataGrid and Dropdownlist Tabitem: Access
$var_comboBox.Text = $null
$var_dataGrid.ItemsSource = $null
$var_comboBox.Add_SelectionChanged({
$agfGroups = (Get-ADuser -identity $var_userName.Text -Properties MemberOf).MemberOf -Replace 'CN=', '' -Replace ',.*', ''
$wwg00mGroups = (Get-ADuser -identity $var_userName.Text -server $script:wwg00m -Properties MemberOf).MemberOf -Replace 'CN=', '' -Replace ',.*', ''
$sid = (Get-ADuser -identity $var_userName.Text -Properties SID).SID -replace 'SID',''
$avcGroups = (Get-ADGroup -LDAPFilter "(member=CN=$sid,CN=ForeignSecurityPrincipals,DC=wwg00m,DC=rootdom,DC=net)" `
-Server $script:wwg00m -Properties Name).Name
$item = $var_comboBox.SelectedItem.Content
if ($item -eq "wwg00m")
{ $var_dataGrid.ItemsSource = $wwg00mGroups
}elseif
($item -eq "agf.be" )
{$var_dataGrid.ItemsSource = $agfGroups
}
else{
($item -eq "AVC" )
$var_dataGrid.ItemsSource = $avcGroups}
})
}
## OnClick function
$var_OnClick.Add_Click({OnClick $this $_})
## Connect to host with msra "Remote assistance"
$var_connectHost.Add_Click({Start-Process -FilePath "C:\Windows\System32\msra.exe" -Args "/Offerra \\$global:pcName"})
## show GUI
$psform.ShowDialog()