https://www.sapien.com/blog/2012/05/16/ ... ive-forms/
I am having a bit of time trying to retrieve an array variable which the background job populates while it is running. At first I tried declaring the array prior to the creation and execution of the background job, eg:
Code: Select all
$array = New-object system.collections.arraylist
Try
{
Add-JobTracker -Name ReloadList -JobScript {
# Code to process
$array
}`
-completedScript {
param ($job)
#do stuff with $array
}`
-UpdateScript {
param ($Job)
$results2 = Receive-Job -Job $Job | Select-Object -Last 1
}
}
Catch
{
Write-Debug $_
}
Code: Select all
Try
{
Add-JobTracker -Name ReloadList -JobScript {
Get-AzureRmSubscription | % {
If ($_.Name -like $combobox2.Text) { Set-AzureRmContext -Subscription $_.Id }
}
$resourcegr = Get-AzureRmResourceGroup
$nic1 = Get-AzureRmNetworkInterface
$currentvms = Get-azurermvm | Select-Object -property Name, ResourceGroupName, OSProfile, @{ name = 'VMSize'; expression = { $_.Hardwareprofile.VMsize } }, ProvisioningState
$pubip = New-Object System.Collections.ArrayList
Get-AzureRmPublicIpAddress | % { $pubip.Add($_) }
foreach ($vm in $currentvms)
{
$inip = $null
foreach ($ip in $pubip)
{
If ($ip.ResourceGroupName -like $vm.ResourceGroupName)
{
$inip = $ip.IpAddress
}
}
Foreach ($nic in $nic1)
{
If ($nic.ResourceGroupName -like $vm.ResourceGroupName)
{
$privip = $nic.IpConfigurationstext | ConvertFrom-Json
}
}
$os = $null
If ($vm.osprofile.LinuxConfiguration -eq $null)
{
$os = "Windows"
}
Else
{
$os = "linux"
}
$custobj = New-Object -TypeName PSObject
$custobj | Add-Member -MemberType NoteProperty -Name "VMName" -Value $vm.Name
$custobj | Add-Member -MemberType NoteProperty -Name "VMResourceGroup" -Value $vm.ResourceGroupName
$custobj | Add-Member -MemberType NoteProperty -Name "VMOS" -Value $os
$custobj | Add-Member -MemberType NoteProperty -Name "VMType" -Value $vm.Vmsize
$custobj | Add-Member -MemberType NoteProperty -Name "VMPublicIP" -Value $inip
$custobj | Add-Member -MemberType NoteProperty -Name "VMprivateIP" -Value $privip.privateipaddress
$custobj | Add-Member -MemberType NoteProperty -Name "VMStatus" -Value $vm.ProvisioningState
$vmarray.Add($custobj)
}
$vmarray
}`
-CompletedScript {
param ($Job)
$progressbaroverlay1.Visible = $false
$results = Receive-Job -Job $job | Select-object -last 1
foreach ($ob in $results)
{
Add-ListViewItem -ListView $listview1 -Items $ob.VMName -SubItems $ob.VMResourceGroup, $ob.VMOS, $ob.VMType, $ob.VMPublicIP, $ob.VMprivateIP, $ob.VMStatus
}
$listview1.Refresh()
$combobox1.Enabled = $true
$textbox1.Enabled = $true
}`
-UpdateScript {
param ($Job)
$results2 = Receive-Job -Job $Job | Select-Object -Last 1
}
}
Catch [Exception]
{
Write-Debug $_
}