Loop script

Ask your PowerShell-related questions, including questions on cmdlet development!
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 3 years and 3 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
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Loop script

Post by sekou2331 »

Hi,
I am trying to write a script that check each server to see if it was rebooted - function call / command. if the server was rebooted
Then: store the server name in some place
Else: wait some time and check again - timeout if it exceeds certain amount of time
I am using FOR loop but I am thinking this is not the powershell way can you look at my script below and let me know what I am doing wrong?
  1. $to_check_list = @('server3', 'server3', 'server3', 'server4')
  2. $rebooted_list = @()
  3.  
  4. function IsRebooted($server)
  5. {
  6.     $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $server
  7.     ($OS.ConvertToDateTime($OS.LastBootUpTime)).Day
  8. }
  9.  
  10. Function CheckServerStatus($to_check_list, $rebooted_list)
  11. {
  12.     for ($i = 0; $i -lt $to_check_list.Count;)
  13.     {
  14.         $server = $to_check_list[$i];
  15.         if (IsRebooted($server))
  16.         {
  17.             $rebooted_list.Add($server);
  18.             $to_check_list.Remove($i)
  19.         }
  20.         else
  21.         {
  22.             ++$i
  23.         }
  24.     }
  25. }
  26.  
  27.  
  28. for ($i = 0; ($i -lt 10) -and ($to_check_list.Count -gt 0); ++$i)
  29. {
  30.     CheckServerStatus($to_check_list, $rebooted_list)
  31.     Start-Sleep -Seconds 5
  32.     Write-host("left to be checked: " + $to_check_list)
  33.     Write-host("Already rebooted: " + $rebooted_list)
  34. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Loop script

Post by jvierra »

Can you tell us what it is that is not working. It is simple enough to just report the time listed.

Here is an example that will help you understand how we would program this kind of a request.

Code: Select all

$computers = 'omega','alpha','test1'
foreach($computer in $computers){
    IF(Test-Connection $computer -Quiet -Count 1){
        Try{
            Get-CimInstance Win32_OperatingSystem -ErrorAction Stop -CimSession $computer -OperationTimeoutSec 2|
                Select-Object CSName, LastBootupTIme
        }
        Catch{
            Write-Host $_ -fore Red -Back White
        }
    }else{
        Write-Host $computer not found -fore Blue -Back White
    }
        
}
I recommend starting with the following free book to gain a basic understanding of programming and program design with PowerShell.

https://www.sapien.com/books_training/W ... werShell-4
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Loop script

Post by sekou2331 »

Thanks but I am trying to continuously loop through checking for a certain allotted time. What you gave was a one time check. I am doing this because I am waiting for a reboot to happen for a x amount of machines before I move on to next part of the script. That why with every check I am building another array. I noticed that PowerShell does deal with arrays well so I changed the script around by making it more of a PowerShell script but still had to call C#. But for some reason I still cant get the List Array to be built. I will look a the documentation given as well.
  1. $to_check_list = [System.Collections.ArrayList]@('server1', 'server2', 'server3', 'server4')
  2. $rebooted_list = [System.Collections.ArrayList]@()
  3.  
  4. function IsRebooted
  5. {
  6.     [CmdletBinding()]
  7.     Param (
  8.         [Parameter(ValueFromPipeline = $True, Mandatory = $True)]
  9.         [string]$server
  10.     )
  11.     $OS = Get-WmiObject Win32_OperatingSystem -ComputerName triss-s07-vm
  12.     $osDate = ($OS.ConvertToDateTime($OS.LastBootUpTime)).Day
  13.     if ($osDate -eq 18)
  14.     {
  15.         return $true
  16.     }
  17. }
  18.  
  19. Function CheckServerStatus
  20. {
  21.     [CmdletBinding()]
  22.     Param (
  23.         [Parameter(ValueFromPipeline = $True, Mandatory = $True)]
  24.         [System.Collections.ArrayList]$to_check_list,
  25.         [System.Collections.ArrayList]$rebooted_list
  26.     )
  27.     for ($i = 0; $i -lt $to_check_list.Count; ++$i)
  28.     {
  29.         $server = $to_check_list[$i];
  30.         $rebootCheck = IsRebooted -server $server
  31.        
  32.         if ($rebootCheck -eq $true)
  33.         {
  34.             $rebooted_list += $server
  35.             $to_check_list.Remove($i)
  36.         }
  37.         else
  38.         {
  39.             ++$i
  40.         }
  41.        
  42.     }
  43. }
  44.  
  45.  
  46. for ($i = 0; ($i -lt 10) -and ($to_check_list.Count -gt 0); ++$i)
  47. {
  48.    
  49.     CheckServerStatus -to_check_list $to_check_list -rebooted_list $rebooted_list
  50.     Start-Sleep -Seconds 5
  51.     Write-host("left to be checked: " + $to_check_list)
  52.     Write-host("Already rebooted: " + $rebooted_list)
  53.    
  54. }
  55.  
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Loop script

Post by jvierra »

The example was to set you in the right direction. You will need to learn the basics to understand the example and then see how to implement your plan.

I can also suggest searching for scripts and modules that are proven and that will do what you need. There are dozens available that do what you ask in more standard, flexible and useful ways. Instead of reinventing the wheel start by using what already has been invented. Experience will help you to learn how to use coding and technology together to get your tasks completed.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Loop script

Post by jvierra »

You could also just ask a specific question about your script such as posting any errors or telling us what doesn't work. We can answer questions that are of a specific nature.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Loop script

Post by sekou2331 »

When it loops if the computer rebooted it is suppose to add to the new array and remove from the old array and output but it is not doing it. I am debugging but I cant seem to figure out why it is not outputting to these lines.

  1. Write-host("left to be checked: " + $to_check_list)
  2. Write-host("Already rebooted: " + $rebooted_list)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Loop script

Post by jvierra »

Why are you using arrays? This is why I ask you to read teh book link and learn the basics of PowerShell. WHat you are trying to do just doesn't make any sense to me.

You have failed to state what you are trying to accomplish and the code only makes discovering this more difficult.

To design a solution it is necessary to first state in non-technical language what the goal of the script is.

Example: "I want to reboot all servers every 12 hours."

Once you have the simplest statement of intent then you can start writing the code.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Loop script

Post by sekou2331 »

Ok in the beginning of this this post I stated what I wanted to do asking what is the best PowerShell way to do it. If we ignore the array part all that I am asking is.

How can can I get a list of servers that I rebooted if the reboot did not happen how can continually check until it does? Then get a list back of all the servers that completed.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Loop script

Post by jvierra »

Your beginning does not state anything clearly it is just some comments about what you have tried and then some code. You have to be able to say what you want without code or any statement of method. Just think of how to state what you need to accomplish in simple terms.

This is why I recommend that you first learn basic PowerShell. It will help you understand how to start and will give you the tools and understanding you need to code with PowerShell.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: Loop script

Post by sekou2331 »

Ok thanks for your help. I figured this out. I am using two separate arrays for failure and success. When I have these two separate arrays of servers I want to use the data. I think something got lost in my explanation. All good once again thanks.
This topic is 3 years and 3 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