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.
Post Reply
User avatar
sekou2331
Posts: 309
Joined: Wed Aug 15, 2012 6:07 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.  
  3. $rebooted_list = @()
  4.  
  5.  
  6.  
  7. function IsRebooted($server)
  8.  
  9. {
  10.  
  11.     $OS = Get-WmiObject Win32_OperatingSystem -ComputerName $server
  12.  
  13.     ($OS.ConvertToDateTime($OS.LastBootUpTime)).Day
  14.  
  15. }
  16.  
  17.  
  18.  
  19. Function CheckServerStatus($to_check_list, $rebooted_list)
  20.  
  21. {
  22.  
  23.     for ($i = 0; $i -lt $to_check_list.Count;)
  24.  
  25.     {
  26.  
  27.         $server = $to_check_list[$i];
  28.  
  29.         if (IsRebooted($server))
  30.  
  31.         {
  32.  
  33.             $rebooted_list.Add($server);
  34.  
  35.             $to_check_list.Remove($i)
  36.  
  37.         }
  38.  
  39.         else
  40.  
  41.         {
  42.  
  43.             ++$i
  44.  
  45.         }
  46.  
  47.     }
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54.  
  55. for ($i = 0; ($i -lt 10) -and ($to_check_list.Count -gt 0); ++$i)
  56.  
  57. {
  58.  
  59.     CheckServerStatus($to_check_list, $rebooted_list)
  60.  
  61.     Start-Sleep -Seconds 5
  62.  
  63.     Write-host("left to be checked: " + $to_check_list)
  64.  
  65.     Write-host("Already rebooted: " + $rebooted_list)
  66.  
  67. }

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 309
Joined: Wed Aug 15, 2012 6:07 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.  
  3. $rebooted_list = [System.Collections.ArrayList]@()
  4.  
  5.  
  6.  
  7. function IsRebooted
  8.  
  9. {
  10.  
  11.     [CmdletBinding()]
  12.  
  13.     Param (
  14.  
  15.         [Parameter(ValueFromPipeline = $True, Mandatory = $True)]
  16.  
  17.         [string]$server
  18.  
  19.     )
  20.  
  21.     $OS = Get-WmiObject Win32_OperatingSystem -ComputerName triss-s07-vm
  22.  
  23.     $osDate = ($OS.ConvertToDateTime($OS.LastBootUpTime)).Day
  24.  
  25.     if ($osDate -eq 18)
  26.  
  27.     {
  28.  
  29.         return $true
  30.  
  31.     }
  32.  
  33. }
  34.  
  35.  
  36.  
  37. Function CheckServerStatus
  38.  
  39. {
  40.  
  41.     [CmdletBinding()]
  42.  
  43.     Param (
  44.  
  45.         [Parameter(ValueFromPipeline = $True, Mandatory = $True)]
  46.  
  47.         [System.Collections.ArrayList]$to_check_list,
  48.  
  49.         [System.Collections.ArrayList]$rebooted_list
  50.  
  51.     )
  52.  
  53.     for ($i = 0; $i -lt $to_check_list.Count; ++$i)
  54.  
  55.     {
  56.  
  57.         $server = $to_check_list[$i];
  58.  
  59.         $rebootCheck = IsRebooted -server $server
  60.  
  61.        
  62.  
  63.         if ($rebootCheck -eq $true)
  64.  
  65.         {
  66.  
  67.             $rebooted_list += $server
  68.  
  69.             $to_check_list.Remove($i)
  70.  
  71.         }
  72.  
  73.         else
  74.  
  75.         {
  76.  
  77.             ++$i
  78.  
  79.         }
  80.  
  81.        
  82.  
  83.     }
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. for ($i = 0; ($i -lt 10) -and ($to_check_list.Count -gt 0); ++$i)
  92.  
  93. {
  94.  
  95.    
  96.  
  97.     CheckServerStatus -to_check_list $to_check_list -rebooted_list $rebooted_list
  98.  
  99.     Start-Sleep -Seconds 5
  100.  
  101.     Write-host("left to be checked: " + $to_check_list)
  102.  
  103.     Write-host("Already rebooted: " + $rebooted_list)
  104.  
  105.    
  106.  
  107. }
  108.  
  109.  

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 309
Joined: Wed Aug 15, 2012 6:07 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.  
  3. Write-host("Already rebooted: " + $rebooted_list)

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 309
Joined: Wed Aug 15, 2012 6:07 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: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

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: 309
Joined: Wed Aug 15, 2012 6:07 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.

Post Reply