What Am I doing wrong?!

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
whiggs2
Posts: 38
Last visit: Fri May 13, 2022 11:03 am

What Am I doing wrong?!

Post by whiggs2 »

To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build: SAPIEN PowerShell Studio 2017
Version 5.4.145
32 or 64 bit version of product: 64-bit
Operating system: Windows 10 1709
32 or 64 bit OS: 64-bit

*** Please add details and screenshots as needed below. ***
Ok. So I am trying to write a GUI script which will, using the credentials that the user provides, connect to azure (this step is done as part of the "startup"), retrieve a list of VMs running under the user's subscription, and output information about these vms to a listview in the GUI (which is triggered by clicking a button on the GUI). Initially, I tried using a dataview, but I couldn't get that to work, so I tried the list view (which I would like to stick with). It would be helpful if the console was generating some kind of error, but it does not. After I click the button, it just hangs indefinitely until I stop the script. I have tried both using the helper functions generated when the control object is created and just adding the data to the control manually, but it all results in the same hang. :

Code: Select all

$buttonConnectToAzure_Click={
	#TODO: Place custom script here
	#$datagridviewResults.Columns.AddRange("Name", "Resource Group", "Location", "Hardware Profile", "Provisioning State")
	$vmhold = New-Object System.Collections.ArrayList
	Get-AzureRmVM | % {$vmhold.Add(@($_.Name, $_.ResourceGroup, $_.Location, $_.Hardwareprofile.VMsize, $_.ProvisioningState))}
	#$datagridviewResults.Refresh()
	foreach ($vmhol in $vmhold)
	{
		$datagridviewResults.Rows.Add($vmhol)
	}
	$formMain.Refresh()
	#Update-DataGridView -Item $vmhold -DataGridView $datagridviewResults
	#[void][System.Windows.Forms.MessageBox]::Show($datatable, 'Title') # Casting the method to [void] suppresses the output. $datatable.Rows
	#Update-DataGridView -DataGridView $datagridviewResults -Item $datatable
	#$datagridviewResults.Refresh()
}

Code: Select all

$buttonConnectToAzure_Click={
	#TODO: Place custom script here
		Add-ListViewItem -ListView $listview1 -Items (Get-AzureRmVM)
		$listview1.Refresh()
		
}
What am I missing!?!? Is it because the information which I want to populate into the datagridview/listview control is a strange object type (PSVirtualMachineList is object name which is returned by (Get-azurermvm).Gettype())?? How can I get the information to populate into the GUI controls??

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: What Am I doing wrong?!

Post by Alexander Riedel »

Moved to correct forum by moderator
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: What Am I doing wrong?!

Post by jvierra »

With DataGridView:

Code: Select all

$buttonConnectToAzure_Click = {
    $vms = Get-AzureRmVM |
        select Name,ResourceGroup,Location, 
            @{n='VMSize';e={$_.Hardwareprofile.VMsize}},
            ProvisioningState
    $datagriViewResults.DataSiurce = [System.Collections.ArrayList]$vms
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: What Am I doing wrong?!

Post by jvierra »

To add items to a ListView:

Code: Select all

$form1_Load={
    $listview1.Columns.Add('Name')
    $listview1.Columns.Add('Fullname')
    $listview1.Columns.Add('length')
    Get-ChildItem | Select-Object name, Fullname, Length |
        ForEach-Object{
            Add-ListViewItem -ListView $listview1 -Items $_.Name -SubItems $_.Fullname,$_.Length
        }
}
User avatar
whiggs2
Posts: 38
Last visit: Fri May 13, 2022 11:03 am

Re: What Am I doing wrong?!

Post by whiggs2 »

I am still having issues with this. I modified the event for the control to the below code, and it the form still hangs?! What am I doing wrong??

Code: Select all

$buttonConnectToAzure_Click = {
		#TODO: Place custom script here
		$vms = Get-azurermvm | Select-Object -property Name, ResourceGroupName, Location, @{name='VMSize'; expression={$_.Hardwareprofile.VMsize}}, ProvisioningState
        $listview1.BeginUpdate()
		foreach ($vm in $vms)
		{
            $listviewitem = $listview1.Items.Add($vm.Name.toString(), -1)
            $listviewitem.SubItems.Add($vm.ResourceGroupName.toString())
            $listviewitem.SubItems.Add($vm.Location.toString())
            $listviewitem.SubItems.Add($vm.VMsize)
            $listviewitem.SubItems.Add($vm.ProvisioningState.toString())
			#Add-ListViewItem -ListView $listview1 -Items $vm.Name.toString() -SubItems $vm.ResourceGroupName.toString(),$vm.Location.toString(),$vm.VMsize.toString(),$vm.ProvisioningState.toString()
		}
        $listview1.EndUpdate()
		$listview1.Refresh()
		
	}
To better assist in identifying the issue, I exported the project to a script file and have included the code below:

Code: Select all

#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: acbea3b8-f1fd-4b6e-becb-5a6016007dfc
# Source File: C:\Users\wmh02\Documents\SAPIEN\PowerShell Studio\Projects\vagrant\vagrant.psproj
#------------------------------------------------------------------------
#region Project Recovery Data (DO NOT MODIFY)
<#RecoveryData:
SwIAAB+LCAAAAAAABACFUl1PgzAUfV+y/0D6aMIAP6aJ0AezYPbgR8biq7mUy6iWlpQyN3+9hTKz
RY0vtz3t4ZycQ+MVMrVFvV+AATqdeF78rNUbMuNle8kqrST/xCIhKYgWyXCYcmFQJ+Rs1rTRbT9r
txRuiXa1GDblMFs73UmFopmZnSGDkbV6Qd1yJen5LIyDAxjvrAsuFxRYjnCR3/hlVBb+ZT5HP0eW
+1cwD6N5GF4XJYuDkTx+OiZY7xukVvgYHsSVKKydFxy7tSMYoXfXcWGTh4RmBrTpmj6M8/qL6WUV
6L6ute5sWyssUaNkmHaSGRsuIUu5Ve/o3wuVg3i1ZRHq9m3f3D/qv+hllfrwH4DLVOna6pWEHlD/
B04EHXAxv2tx6EnzDZcgesIj1Ei3sNEgjdVoLC8OfhCmkzg4eTxfL4q2G0sCAAA=#>
#endregion
<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.145
     Generated on:       12/27/2017 11:56 AM
     Generated by:       WMH02
    --------------------------------------------------------------------------------
    .DESCRIPTION
        Script generated by PowerShell Studio 2017
#>



#region Source: Startup.pss
#region File Recovery Data (DO NOT MODIFY)
<#RecoveryData:
2gMAAB+LCAAAAAAABAC9k0tLAzEQgO+C/yH0vGR33a5rIbsglV4EFVfUazadLcE8yiRpyb+3rVJF
QaGHMJd5MPMxL/YIwm4A4w33nOwUJ61pJxe0nHTnZ4Swe5QrabhaSAV3XEPXe44+rOnaOZb/ih5y
rp0DPSgJ7mB/eWKnnbCo5JCR50/WlBZ7ycg8KB8QWgPBI1cZeQi7GuIW4pN9A9MOTcNrUV+Ws2oK
xdWM5ceqPyl9dB50CgZ9kWZpt44uLGqXhLjfVBoQ8q00q1NYRTXWYzOW5bIueMX/Z71qlaSnuUVI
MzyJILzF2ANupICTbuPvMR7Nj0dj+fdf7t4BtbYVHtoDAAA=#>
#endregion
#----------------------------------------------
#region Import Assemblies
#----------------------------------------------
[void][Reflection.Assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][Reflection.Assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
[void][Reflection.Assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
[void][Reflection.Assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
#endregion Import Assemblies

#Define a Param block to use custom parameters in the project
#Param ($CustomParameter)

function Main {
<#
    .SYNOPSIS
        The Main function starts the project application.
    
    .PARAMETER Commandline
        $Commandline contains the complete argument string passed to the script packager executable.
    
    .NOTES
        Use this function to initialize your script and to call GUI forms.
		
    .NOTES
        To get the console output in the Packager (Forms Engine) use: 
		$ConsoleOutput (Type: System.Collections.ArrayList)
#>
	Param ([String]$Commandline)
		
	#--------------------------------------------------------------------------
	#TODO: Add initialization script here (Load modules and check requirements)
	If (!(Test-path $env:ProgramFiles\WindowsPowerShell\Modules\AzureRM))
	{
		Get-PackageProvider -Name NuGet -ForceBootstrap
		Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
		Install-Module AzureRM -AllowClobber -Force
		Install-Module Azure -AllowClobber -Force
	}
	Else
	{
		Update-Module AzureRM
		Update-Module Azure
	}
	If (!(Test-Path "$env:USERPROFILE\Documents\sub.json"))
	{
		Try
		{
			$azurecred = Get-SBCredential -UserName "$env:USERNAME@acuitysso.com" -ErrorAction Stop
			Add-AzureRmAccount -Credential $azurecred -ErrorAction Stop
		}
		Catch
		{
			Write-Host "You either did not enter your credentials or the credentials you entered are invalid.  Exiting." -ForegroundColor DarkRed
			Start-Sleep 4
			Exit
		}
		Get-AzureRmSubscription | % { If ($_.Name -like "Visual Studio Enterprise") { Set-AzureRmContext -Subscription $_.id -Force -Confirm:$false } }
		Save-AzureRmContext -Path "$env:USERPROFILE\Documents\sub.json" -Force -Confirm:$false
	}
	Import-AzureRmContext -Path "$env:USERPROFILE\Documents\sub.json" -Confirm:$false
	
	#--------------------------------------------------------------------------
	
	if((Show-MainForm_psf) -eq 'OK')
	{
		
	}
	
	$script:ExitCode = 0 #Set the exit code for the Packager
}







#endregion Source: Startup.pss

#region Source: Globals.ps1
	#--------------------------------------------
	# Declare Global Variables and Functions here
	#--------------------------------------------
	
	
	#Sample function that provides the location of the script
	function Get-ScriptDirectory
	{
	<#
		.SYNOPSIS
			Get-ScriptDirectory returns the proper location of the script.
	
		.OUTPUTS
			System.String
		
		.NOTES
			Returns the correct path within a packaged executable.
	#>
		[OutputType([string])]
		param ()
		if ($null -ne $hostinvocation)
		{
			Split-Path $hostinvocation.MyCommand.path
		}
		else
		{
			Split-Path $script:MyInvocation.MyCommand.Path
		}
	}
	function New-RDPSession
	{
		[CmdletBinding()]
		Param
		(
			[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True)]
			[string]$Computername,
			[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True)]
			[ValidateSet('FullScreen', 'Normal')]
			[string]$WindowStyle = 'FullScreen',
			[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True, ParameterSetName = 'SuppliedCredentials')]
			[string]$Username,
			[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True, ParameterSetName = 'SuppliedCredentials')]
			[string]$Password,
			[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True, ParameterSetName = 'SuppliedCredentials')]
			[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True, ParameterSetName = 'UseCredentialObject')]
			[switch]$Persist = $false,
			[Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True, ParameterSetName = 'UseCredentialCache')]
			[switch]$UseCache,
			[Parameter(Mandatory = $True, ValueFromPipelineByPropertyName = $True, ParameterSetName = 'UseCredentialObject')]
			[pscredential]$Credential
			
			
		)
		
		Process
		{
			Write-Verbose -Message 'New-RDPSession cmdlet starting'
			Write-Verbose -Message "$($pscmdlet.ParametersetName) parameterset in use"
			Switch ($WindowStyle)
			{
				'Normal'
				{
					$SizeParam = ''
				}
				Default
				{
					$SizeParam = '/f'
				}
			}
			$CachedCredentialExists = C:\Windows\System32\cmdkey.exe /list | Where-Object -FilterScript {
				$psItem -like "*TERMSRV/$computername"
			}
			If (($CachedCredentialExists) -and ($PsCmdlet.ParameterSetName -in 'SuppliedCredentials', 'UseCredentialObject'))
			{
				Write-Verbose -Message 'A cached credential already exists'
				$useCachedCredentials = Read-Host -Prompt "Cached credentials for $computerName already exist.  Do you wish to use these instead? (y/n)"
			}
			$process = [System.Diagnostics.Process]::new()
			
			If (($pscmdlet.ParameterSetName -eq 'CachedCredentialExists') -or ($useCachedCredentials -eq 'y'))
			{
				Write-Verbose -Message 'Using cached credentials for connection'
			}
			ElseIf ($pscmdlet.ParameterSetName -eq 'SuppliedCredentials')
			{
				Write-Verbose -Message 'Using supplied credentials for connection'
				$process.StartInfo.FileName = "$env:systemroot\system32\cmdkey.exe"
				$process.StartInfo.Arguments = "/generic:TERMSRV/$computername /user:$username /pass:$password"
				$null = $process.Start()
			}
			ElseIf ($pscmdlet.ParameterSetName -eq 'UseCredentialObject')
			{
				Write-Verbose -Message 'Using credential object for connection'
				$process.StartInfo.FileName = "$env:systemroot\system32\cmdkey.exe"
				$username = $credential.UserName
				$password = $credential.GetNetworkCredential().Password
				$process.StartInfo.Arguments = "/generic:TERMSRV/$computername /user:$username /pass:$password"
				$null = $process.Start()
			}
			$process.StartInfo.FileName = "$Env:SystemRoot\system32\mstsc.exe"
			$process.StartInfo.Arguments = "$sizeParam /v $computername"
			$process.StartInfo.UseShellExecute = $false
			$null = $process.Start()
			
			If ((!$Persist) -and - ($useCachedCredentials -ne 'y'))
			{
				While (($process.HasExited -eq $false) -and ($process.MainWindowTitle -notlike "*$computername*"))
				{
					$process = [System.Diagnostics.Process]::GetProcessById($process.id)
					Write-Verbose -Message "Window Title - $process.MainWindowTitle"
					Start-Sleep -Seconds 1
				}
				Write-Debug -Message 'Removing temporary cached credential'
				$process = [System.Diagnostics.Process]::new()
				$process.StartInfo.FileName = "$env:systemroot\system32\cmdkey.exe"
				$process.StartInfo.Arguments = "/delete:TERMSRV/$computername"
				$null = $process.Start()
			}
			Write-Verbose -Message 'New-RDPSession cmdlet exiting'
		}
	}
	
	function Get-SBCredential
	{
		[CmdletBinding(ConfirmImpact = 'Low')]
		Param (
			[Parameter(Mandatory = $false,
					   ValueFromPipeLine = $true,
					   ValueFromPipeLineByPropertyName = $true,
					   Position = 0)]
			[String]$UserName = $env:USERNAME,
			[Parameter(Mandatory = $false,
					   Position = 1)]
			[Switch]$Refresh = $false
		)
		
		$CredPath = "$env:Temp\$($UserName.Replace('\', '_')).txt"
		if ($Refresh)
		{
			try
			{
				Remove-Item -Path $CredPath -Force -Confirm:$false -ErrorAction Stop
				Write-Verbose "Deleted password file '$CredPath'"
			}
			catch
			{
				Write-Error "Failed to delete password file '$CredPath'"
			}
		}
		if (!(Test-Path -Path $CredPath))
		{
			$Temp = Read-Host "Enter the pwd for $UserName" -AsSecureString | ConvertFrom-SecureString
			try
			{
				$Temp | Out-File $CredPath -ErrorAction Stop
				Write-Verbose "Wrote to password file '$CredPath'"
			}
			catch
			{
				Write-Error "Failed to write to password file '$CredPath'"
			}
		}
		$Pwd = Get-Content $CredPath | ConvertTo-SecureString
		try
		{
			New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Pwd -ErrorAction Stop
			Write-Verbose "'$UserName' crednetial obtained successfully"
		}
		catch
		{
			Write-Error "Failed to obtain credential for $UserName"
		}
	}
	#Sample variable that provides the location of the script
	[string]$ScriptDirectory = Get-ScriptDirectory
	
	
	
#endregion Source: Globals.ps1

#region Source: MainForm.psf
function Show-MainForm_psf
{
#region File Recovery Data (DO NOT MODIFY)
<#RecoveryData:
ihQAAB+LCAAAAAAABADdWG1v2jAQ/j5p/yHKZ0QIhEIliETpaKvRrQLW7ltlkgO8OjZyHCj99bu8
UPG2xJ1aJCokROK7e557fNw5aQ3AEwuQq0uiiIE/Qip426yWbdP9+sUwWj8lnVJOWI8y+EECcG8J
5T0hg/I8nLSsveXUafwHPGWo1Rza5nAVKgjKD5T7YhmWY9/0u2QcWioZ9xkLp1yJPyWjGzEVSWhz
iJQkrGTcRWNGve+wGokn4O1xo0HqXv3MPq85UGmemwZHKm1zgvFs0/BmlPkS7cyu4EoKFqa5IdE7
KeYg1Spz6ERKDD3C4JIGwGMSaHpWMuxay1qbFrneCh9Mt4dIhT5dRoGrIX1Bh7pdLRnVZrXQKVbZ
dJPUCm1H8KxiLjLYN/22QOzMri+In8V8jH+3rGR1bVq8nX0aqnsKyw/fUoZACwTK3dYDSgsWBXzD
AE1ukGdeTqnPNRAf5MfltcEIOQ1gApiSBxlvL+EwSzhgytYmfytO4JQTqn62hGqfLSHnsyVUz01o
r0nuN5JexNhALIfAsCOa7khGoOF1JanfpxxCbY++8IhCVUw3ngv2/ljYd7lF2eiaWI+wUAcnnSav
XVXDIx1XTrOJ48rWcRiR8Q334RlT0bD+FUJXBHNMfsxgqIiCm4BM4QJmZEGF1M8sHkemewmKUBYe
st8cgKls4CdEuzPCp+BvyPJ4aH17SO4EzEZSx/e3A23e35myVjpm9YfuRaSU4B8+cscJDBLnyGck
Oi8Y5G3Td7uYq45OHaSFeQhbu0btioNo528q0YqOdXKqykgZShi6tLZqO44yAESVlE+1+wKGuKdh
RNhQrRhcEO8JW2z8t/iX/1ZR4nY/HRb1MVnLqcjti+LiPE7nP3hMyj0Mx3VVeG5+oL6amW7jbMfy
ZESo5oswgFBEEifklRTRXFcOu9I8VT1q+XqsG5SuEs3d58GTEcLJF+KaSH9JJBi4OsFHev3SOFlF
6vmK4M0FjVGxSxvJeURbE9vJ1aQThhAgXQjXcbI7KzcIPSEZHb9Dyi3rNeouSiryMTDefSOLEeP3
WccBkmSJtfE/WJXapD5pTGzbr1dIjRRj/Q7YUXLqCgnvD/R6mZZ8y9p89+j+BZpZc4uKFAAA#>
#endregion
	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$form1 = New-Object 'System.Windows.Forms.Form'
	$listview1 = New-Object 'System.Windows.Forms.ListView'
	$buttonConnectToAzure = New-Object 'System.Windows.Forms.Button'
	$columnheader1 = New-Object 'System.Windows.Forms.ColumnHeader'
	$columnheader2 = New-Object 'System.Windows.Forms.ColumnHeader'
	$columnheader3 = New-Object 'System.Windows.Forms.ColumnHeader'
	$columnheader4 = New-Object 'System.Windows.Forms.ColumnHeader'
	$columnheader5 = New-Object 'System.Windows.Forms.ColumnHeader'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	$form1_Load={
		#TODO: Initialize Form Controls here
	}
	
	#region Control Helper Functions
	function Update-ListViewColumnSort
	{
	<#
		.SYNOPSIS
			Sort the ListView's item using the specified column.
		
		.DESCRIPTION
			Sort the ListView's item using the specified column.
			This function uses Add-Type to define a class that sort the items.
			The ListView's Tag property is used to keep track of the sorting.
		
		.PARAMETER ListView
			The ListView control to sort.
		
		.PARAMETER ColumnIndex
			The index of the column to use for sorting.
		
		.PARAMETER SortOrder
			The direction to sort the items. If not specified or set to None, it will toggle.
		
		.EXAMPLE
			Update-ListViewColumnSort -ListView $listview1 -ColumnIndex 0
		
		.NOTES
			Additional information about the function.
	#>
		
		param
		(
			[Parameter(Mandatory = $true)]
			[ValidateNotNull()]
			[System.Windows.Forms.ListView]
			$ListView,
			[Parameter(Mandatory = $true)]
			[int]
			$ColumnIndex,
			[System.Windows.Forms.SortOrder]
			$SortOrder = 'None'
		)
		
		if (($ListView.Items.Count -eq 0) -or ($ColumnIndex -lt 0) -or ($ColumnIndex -ge $ListView.Columns.Count))
		{
			return;
		}
		
		#region Define ListViewItemComparer
		try
		{
			[ListViewItemComparer] | Out-Null
		}
		catch
		{
			Add-Type -ReferencedAssemblies ('System.Windows.Forms') -TypeDefinition  @" 
	using System;
	using System.Windows.Forms;
	using System.Collections;
	public class ListViewItemComparer : IComparer
	{
	    public int column;
	    public SortOrder sortOrder;
	    public ListViewItemComparer()
	    {
	        column = 0;
			sortOrder = SortOrder.Ascending;
	    }
	    public ListViewItemComparer(int column, SortOrder sort)
	    {
	        this.column = column;
			sortOrder = sort;
	    }
	    public int Compare(object x, object y)
	    {
			if(column >= ((ListViewItem)x).SubItems.Count)
				return  sortOrder == SortOrder.Ascending ? -1 : 1;
		
			if(column >= ((ListViewItem)y).SubItems.Count)
				return sortOrder == SortOrder.Ascending ? 1 : -1;
		
			if(sortOrder == SortOrder.Ascending)
	        	return String.Compare(((ListViewItem)x).SubItems[column].Text, ((ListViewItem)y).SubItems[column].Text);
			else
				return String.Compare(((ListViewItem)y).SubItems[column].Text, ((ListViewItem)x).SubItems[column].Text);
	    }
	}
"@ | Out-Null
		}
		#endregion
		
		if ($ListView.Tag -is [ListViewItemComparer])
		{
			#Toggle the Sort Order
			if ($SortOrder -eq [System.Windows.Forms.SortOrder]::None)
			{
				if ($ListView.Tag.column -eq $ColumnIndex -and $ListView.Tag.sortOrder -eq 'Ascending')
				{
					$ListView.Tag.sortOrder = 'Descending'
				}
				else
				{
					$ListView.Tag.sortOrder = 'Ascending'
				}
			}
			else
			{
				$ListView.Tag.sortOrder = $SortOrder
			}
			
			$ListView.Tag.column = $ColumnIndex
			$ListView.Sort() #Sort the items
		}
		else
		{
			if ($SortOrder -eq [System.Windows.Forms.SortOrder]::None)
			{
				$SortOrder = [System.Windows.Forms.SortOrder]::Ascending
			}
			
			#Set to Tag because for some reason in PowerShell ListViewItemSorter prop returns null
			$ListView.Tag = New-Object ListViewItemComparer ($ColumnIndex, $SortOrder)
			$ListView.ListViewItemSorter = $ListView.Tag #Automatically sorts
		}
	}
	
	
	function Add-ListViewItem
	{
	<#
		.SYNOPSIS
			Adds the item(s) to the ListView and stores the object in the ListViewItem's Tag property.
	
		.DESCRIPTION
			Adds the item(s) to the ListView and stores the object in the ListViewItem's Tag property.
	
		.PARAMETER ListView
			The ListView control to add the items to.
	
		.PARAMETER Items
			The object or objects you wish to load into the ListView's Items collection.
			
		.PARAMETER  ImageIndex
			The index of a predefined image in the ListView's ImageList.
		
		.PARAMETER  SubItems
			List of strings to add as Subitems.
		
		.PARAMETER Group
			The group to place the item(s) in.
		
		.PARAMETER Clear
			This switch clears the ListView's Items before adding the new item(s).
		
		.EXAMPLE
			Add-ListViewItem -ListView $listview1 -Items "Test" -Group $listview1.Groups[0] -ImageIndex 0 -SubItems "Installed"
	#>
		
		Param( 
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		[System.Windows.Forms.ListView]$ListView,
		[ValidateNotNull()]
		[Parameter(Mandatory=$true)]
		$Items,
		[int]$ImageIndex = -1,
		[string[]]$SubItems,
		$Group,
		[switch]$Clear)
		
		if($Clear)
		{
			$ListView.Items.Clear();
	    }
	    
	    $lvGroup = $null
	    if ($Group -is [System.Windows.Forms.ListViewGroup])
	    {
	        $lvGroup = $Group
	    }
	    elseif ($Group -is [string])
	    {
	        #$lvGroup = $ListView.Group[$Group] # Case sensitive
	        foreach ($groupItem in $ListView.Groups)
	        {
	            if ($groupItem.Name -eq $Group)
	            {
	                $lvGroup = $groupItem
	                break
	            }
	        }
	        
	        if ($null -eq $lvGroup)
	        {
	            $lvGroup = $ListView.Groups.Add($Group, $Group)
	        }
	    }
	    
		if($Items -is [Array])
		{
			$ListView.BeginUpdate()
			foreach ($item in $Items)
			{		
				$listitem  = $ListView.Items.Add($item.ToString(), $ImageIndex)
				#Store the object in the Tag
				$listitem.Tag = $item
				
				if($null -ne $SubItems)
				{
					$listitem.SubItems.AddRange($SubItems)
				}
				
				if($null -ne $lvGroup)
				{
					$listitem.Group = $lvGroup
				}
			}
			$ListView.EndUpdate()
		}
		else
		{
			#Add a new item to the ListView
			$listitem  = $ListView.Items.Add($Items.ToString(), $ImageIndex)
			#Store the object in the Tag
			$listitem.Tag = $Items
			
			if($null -ne $SubItems)
			{
				$listitem.SubItems.AddRange($SubItems)
			}
			
			if($null -ne $lvGroup)
			{
				$listitem.Group = $lvGroup
			}
		}
	}
	
	#endregion
	
	$listview1_ControlAdded=[System.Windows.Forms.ControlEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.ControlEventArgs]
		#TODO: Place custom script here
		
	}
	
	$listview1_SelectedIndexChanged={
		#TODO: Place custom script here
		
	}
	$buttonConnectToAzure_Click = {
		#TODO: Place custom script here
		$vms = Get-azurermvm | Select-Object -property Name, ResourceGroupName, Location, @{name='VMSize'; expression={$_.Hardwareprofile.VMsize}}, ProvisioningState
        $listview1.BeginUpdate()
		foreach ($vm in $vms)
		{
            $listviewitem = $listview1.Items.Add($vm.Name.toString(), -1)
            $listviewitem.SubItems.Add($vm.ResourceGroupName.toString())
            $listviewitem.SubItems.Add($vm.Location.toString())
            $listviewitem.SubItems.Add($vm.VMsize)
            $listviewitem.SubItems.Add($vm.ProvisioningState.toString())
			#Add-ListViewItem -ListView $listview1 -Items $vm.Name.toString() -SubItems $vm.ResourceGroupName.toString(),$vm.Location.toString(),$vm.VMsize.toString(),$vm.ProvisioningState.toString()
		}
        $listview1.EndUpdate()
		$listview1.Refresh()
		
	}
		# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$form1.WindowState = $InitialFormWindowState
	}
	
	$Form_StoreValues_Closing=
	{
		#Store the control values
		$script:MainForm_listview1 = $listview1.SelectedItems
	}

	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$listview1.remove_SelectedIndexChanged($listview1_SelectedIndexChanged)
			$listview1.remove_ControlAdded($listview1_ControlAdded)
			$buttonConnectToAzure.remove_Click($buttonConnectToAzure_Click)
			$form1.remove_Load($form1_Load)
			$form1.remove_Load($Form_StateCorrection_Load)
			$form1.remove_Closing($Form_StoreValues_Closing)
			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$form1.SuspendLayout()
	#
	# form1
	#
	$form1.Controls.Add($listview1)
	$form1.Controls.Add($buttonConnectToAzure)
	$form1.AutoScaleDimensions = '6, 13'
	$form1.AutoScaleMode = 'Font'
	$form1.ClientSize = '512, 282'
	$form1.Name = 'form1'
	$form1.Text = 'Form'
	$form1.add_Load($form1_Load)
	#
	# listview1
	#
	[void]$listview1.Columns.Add($columnheader1)
	[void]$listview1.Columns.Add($columnheader2)
	[void]$listview1.Columns.Add($columnheader3)
	[void]$listview1.Columns.Add($columnheader4)
	[void]$listview1.Columns.Add($columnheader5)
	$listview1.FullRowSelect = $True
	$listview1.GridLines = $True
	$listview1.Location = '12, 12'
	$listview1.MultiSelect = $False
	$listview1.Name = 'listview1'
	$listview1.Size = '488, 211'
	$listview1.TabIndex = 1
	$listview1.UseCompatibleStateImageBehavior = $False
	$listview1.View = 'Details'
	$listview1.add_SelectedIndexChanged($listview1_SelectedIndexChanged)
	$listview1.add_ControlAdded($listview1_ControlAdded)
	#
	# buttonConnectToAzure
	#
	$buttonConnectToAzure.Location = '12, 241'
	$buttonConnectToAzure.Name = 'buttonConnectToAzure'
	$buttonConnectToAzure.Size = '104, 29'
	$buttonConnectToAzure.TabIndex = 0
	$buttonConnectToAzure.Text = 'Connect to Azure'
	$buttonConnectToAzure.UseCompatibleTextRendering = $True
	$buttonConnectToAzure.UseVisualStyleBackColor = $True
	$buttonConnectToAzure.add_Click($buttonConnectToAzure_Click)
	#
	# columnheader1
	#
	$columnheader1.Text = 'Name'
	$columnheader1.Width = 76
	#
	# columnheader2
	#
	$columnheader2.Text = 'Resource Group'
	$columnheader2.Width = 108
	#
	# columnheader3
	#
	$columnheader3.Text = 'Location'
	$columnheader3.Width = 83
	#
	# columnheader4
	#
	$columnheader4.Text = 'Hardware Profile'
	$columnheader4.Width = 103
	#
	# columnheader5
	#
	$columnheader5.Text = 'Provisioning State'
	$columnheader5.Width = 114
	$form1.ResumeLayout()
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $form1.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$form1.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$form1.add_FormClosed($Form_Cleanup_FormClosed)
	#Store the control values when form is closing
	$form1.add_Closing($Form_StoreValues_Closing)
	#Show the Form
	return $form1.ShowDialog()

}
#endregion Source: MainForm.psf

#Start the application
Main ($CommandLine)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: What Am I doing wrong?!

Post by jvierra »

Using the debugger you will have to identify where it is hanging. We cannot do that for you.
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: What Am I doing wrong?!

Post by mxtrinidad »

Ok! Which Azure module and version are you currently using?
This will help us.

Thanks
User avatar
whiggs2
Posts: 38
Last visit: Fri May 13, 2022 11:03 am

Re: What Am I doing wrong?!

Post by whiggs2 »

Turns out it was never an issue with the GUI. I was using the security token that was created when the application was first run to authenticate to azure (using Save-azurermcontext) without realizing that they expire. When I accessed azure using credentials, it worked.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: What Am I doing wrong?!

Post by jvierra »

I am guessing the debugger showed you that it was hanging due to connections timing out due to a bad token? You can avoid this by implementing error control and aborting the script on the first error.

See: help about_try_catch
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked