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 1 day 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
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 »

These are the strings:
Attachments
Windows PowerShell ISE000604.jpg
Windows PowerShell ISE000604.jpg (188.66 KiB) Viewed 2529 times
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 an example of how to dynamically assign images to a listview. Run it then analyze how the imagelists are built.

You can also assign at design time from files and they will be encoded into the script.
Attachments
Demo-ListViewIcons.psf
(40.44 KiB) Downloaded 118 times
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 »

When i try yout code this is the error:
  1. New-Object : Impossibile trovare un overload per "ListViewItem" e il numero di argomenti: "4".
  2. In C:\Users\Administrator\Desktop\FORM USER-COMPUTER MANAGER TEST BL 3.ps1:589 car:13
  3. +             $item1 = New-Object System.Windows.Forms.ListViewItem($_.name, 'n/a', 'n/a',  ...
  4. +                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5.     + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
  6.     + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
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 »

This is my listview2 in attachment:
Attachments
listview2.ps1
(1.89 KiB) Downloaded 127 times
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 »

Sorry. Try it this way.
  1. function STATUS {
  2.    
  3.     Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' |
  4.         ForEach-Object {
  5.             $item1 = New-Object System.Windows.Forms.ListViewItem
  6.             $item1.Text = $_.Name
  7.             $item1.Name = $_.Name
  8.             $item1.SubItems.Add('n/a')
  9.             $item1.SubItems.Add('n/a')     
  10.             $listview2.Items.Add($item1)
  11.             if(Test-Connection -ComputerName $_.dnshostname -Count 1 -Quiet){
  12.                 $item1.ImageIndex = 0
  13.                 $item1.SubItems[1].Text = 'Online'
  14.                 $item1.SubItems[1].ForeColor = 'Green'
  15.                 $ProtectionStatus = Invoke-Command -ScriptBlock { (Get-BitLockerVolume).ProtectionStatus } -ComputerName $_.name -Credential $Global:DomainCredentials
  16.                 if ($ProtectionStatus -match 'On') {
  17.                     $item1.SubItems[2].Text = 'On'
  18.                     $item1.UseItemStyleForSubItems = $true;
  19.                 }else{
  20.                     $item1.SubItems[2].Text = 'Off'
  21.                     $item1.UseItemStyleForSubItems = $false;
  22.                     $item1.SubItems[2].ForeColor = 'DarkRed'
  23.                 }          
  24.             }else{
  25.                 $item1.ImageIndex = 1
  26.                 $item1.SubItems[1].Text = 'Offline'
  27.                 $item1.SubItems[1].ForeColor = 'Red'
  28.             }
  29.         }
  30. }
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 another way to do thos:
  1. function STATUS {
  2.    
  3.     Get-ADComputer -Filter * -SearchBase 'OU=DOMUSERS,DC=DOM,DC=LOCAL' |
  4.         ForEach-Object {
  5.        
  6.             $item1 = New-Object System.Windows.Forms.ListViewItem
  7.             $listview2.Items.Add($item1)
  8.             $item1.Text = $_.Name
  9.             $item1.Name = 'Computer'
  10.             $status = $item1.SubItems.Add('n/a')
  11.             $bitlocker = $item1.SubItems.Add('n/a')    
  12.             $item1.UseItemStyleForSubItems = $false
  13.        
  14.             if(Test-Connection -ComputerName $_.dnshostname -Count 1 -Quiet){
  15.                 $item1.ImageIndex = 0
  16.                 $status.Text = 'Online'
  17.                 $status.ForeColor = 'Green'
  18.                 $ProtectionStatus = Invoke-Command -ScriptBlock { (Get-BitLockerVolume).ProtectionStatus } -ComputerName $_.name -Credential $Global:DomainCredentials
  19.                 if ($ProtectionStatus -match 'On') {
  20.                     $bitlocker.Text = 'On'
  21.                 }else{
  22.                     $bitlocker.Text = 'Off'
  23.                     $bitlocker.ForeColor = 'DarkRed'
  24.                 }          
  25.             }else{
  26.                 $item1.ImageIndex = 1
  27.                 $status.Text = 'Offline'
  28.                 $status.ForeColor = 'Red'
  29.             }
  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 »

You can't add images to a list before you have crated them. You can add them with a keyname and use ImageKey to reference the image.
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 »

don't worry

this is the result near the solution but not complete: only the second pc is colored green o red, take a look about VM8
Attachments
b.jpg
b.jpg (18.88 KiB) Viewed 2526 times
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 »

Without seeing the code that did this I cannot comment.
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 »

The code is your first good solution:

Postby jvierra » Sat Apr 15, 2017 1:03 pm
This topic is 7 years and 1 day 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