Listview check Service status and change icon

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 7 years and 2 days 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listview check Service status and change icon

Post by jvierra »

Post the code you actually used.
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Listview check Service status and change icon

Post by ramses147 »

Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' |
ForEach-Object {
$item1 = New-Object System.Windows.Forms.ListViewItem
$item1.Text = $_.Name
$item1.Name = $_.Name
$item1.SubItems.Add('')
$item1.SubItems.Add('')
$listview2.Items.Add($item1)
if(Test-Connection -ComputerName $_.dnshostname -Count 1 -Quiet){
$item1.ImageIndex = 0
$item1.SubItems[1].Text = 'Online'
$item1.SubItems[1].ForeColor = 'Green'
$ProtectionStatus = Invoke-Command -ScriptBlock { (Get-BitLockerVolume).ProtectionStatus } -ComputerName $_.name -Credential $Global:DomainCredentials
if ($ProtectionStatus -match 'On') {
$item1.SubItems[2].Text = 'On'
$item1.UseItemStyleForSubItems = $true;
$item1.SubItems[2].ForeColor = 'Green'

}else{
$item1.SubItems[2].Text = 'Off'
$item1.UseItemStyleForSubItems = $false;
$item1.SubItems[2].ForeColor = 'DarkRed'
}
}else{
$item1.ImageIndex = 1
$item1.SubItems[1].Text = 'Offline'
$item1.SubItems[1].ForeColor = 'Red'
}
}
#>
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Listview check Service status and change icon

Post by ramses147 »

is it possible center Status and Bitlocker values text ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listview check Service status and change icon

Post by jvierra »

This is the one that works the way you want:
  1. Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' |
  2.     ForEach-Object {
  3.        
  4.         $item1 = New-Object System.Windows.Forms.ListViewItem
  5.         $item1.Text = $_.Name
  6.         $item1.Name = $_.Name
  7.         $item1.SubItems.Add('')
  8.         $item1.SubItems.Add('')
  9.         $listview2.Items.Add($item1)
  10.         $item1.UseItemStyleForSubItems = $false
  11.        
  12.         if (Test-Connection -ComputerName $_.dnshostname -Count 1 -Quiet) {
  13.             $item1.ImageIndex = 0
  14.             $item1.ForeColor = 'Green'
  15.             $item1.SubItems[1].Text = 'Online'
  16.             $item1.SubItems[1].ForeColor = 'Green'
  17.             $ProtectionStatus = Invoke-Command -ScriptBlock { (Get-BitLockerVolume).ProtectionStatus } -ComputerName $_.name -Credential $Global:DomainCredentials
  18.             if ($ProtectionStatus -match 'On') {
  19.                 $item1.SubItems[2].Text = 'On'
  20.                 $item1.SubItems[2].ForeColor = 'Green'
  21.                
  22.             } else {
  23.                 $item1.SubItems[2].Text = 'Off'
  24.                 $item1.SubItems[2].ForeColor = 'DarkRed'
  25.             }
  26.         } else {
  27.             $item1.ImageIndex = 1
  28.             $item1.SubItems[1].Text = 'Offline'
  29.             $item1.SubItems[1].ForeColor = 'Red'
  30.         }
  31.     }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listview check Service status and change icon

Post by jvierra »

Here is a much easier way to generate columns.
  1. $ix = $listview2.Columns.Add((New-Object System.Windows.Forms.ColumnHeader))
  2. $listview2.Columns[$ix].name = 'Hostname'
  3. $listview2.Columns[$ix].Text = 'Hostname'
  4. $listview2.Columns[$ix].Width = 150
  5.  
  6. $ix = $listview2.Columns.Add((New-Object System.Windows.Forms.ColumnHeader))
  7. $listview2.Columns[$ix].name = 'Status'
  8. $listview2.Columns[$ix].Text = 'Status'
  9. $listview2.Columns[$ix].Width = 60
  10.  
  11. $ix = $listview2.Columns.Add((New-Object System.Windows.Forms.ColumnHeader))
  12. $listview2.Columns[$ix].name = 'Bitlocker'
  13. $listview2.Columns[$ix].Text = 'Bitlocker'
  14. $listview2.Columns[$ix].Width = 60
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Listview check Service status and change icon

Post by jvierra »

Consider this:

$ix = $listview2.Columns.Add((New-Object System.Windows.Forms.ColumnHeader))
$listview2.Columns[$ix].name = 'Hostname'
$listview2.Columns[$ix].Text = 'Hostname'
$listview2.Columns[$ix].Width = 150
$listview2.Columns[$ix].TextAlign = 'Center'

"AutoComplete" in PowerShell will help you find all properties.
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Listview check Service status and change icon

Post by ramses147 »

Great !!!
it works perfectly !!!
Many many thanks !!!
:) :) :)
User avatar
ramses147
Posts: 110
Last visit: Tue Dec 05, 2023 7:11 am
Been upvoted: 1 time

Re: Listview check Service status and change icon

Post by ramses147 »

Is it possible to add the icon in the second or third column ?
This topic is 7 years and 2 days 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