none
Help - Powershell 2.0 Scripting - Restart workstation and verify bootup is successfully.

    Question

  • Hi,

    I'm new to Powershell and I need help writing a script for my boss.

    Script will run from Windows 2008 box, the outcome is to restart approximately 20 machines (from list) nightly and then verify the systems have bootup successfully.

    What I have in mind is:
    1st Script Restart script: Shutdown/Restart machine from list provided. Displays a message with a 1 min time before shutdown.
    2nd Script: Check bootup. Input result to a file successfull or FAILED.
    3rd Scritp: Send file via e-mail (Subject line to Display "Company Name %date% Shutdown Report"

    This is the best possible method I can think. Suggestions are accepted.

    Tuesday, June 01, 2010 9:24 PM

Answers

  • In message
    <4a1b43a4-dc48-43a8-a86d-4f28d60f155e@communitybridge.codeplex.com>,
    Ncruz1980@?.?.invalid writes
    >
    > 1) I should be pretty simple simple
    >
    > > machines.txt | % { shutdown $_  /r /t 60 }  ## check help to see
    > additional option shuch as "why this was rebooted"
    >
    >Taking a step at a time.... i'm truly a beginner here. :)
    >"> machines.txt |" - This commands tells the script where to obtain the
    >information from? If so, every machine needs to be in its own line
    >correct?
     
    Yes - this pipes the machines.txt file into the pipeline, line at a
    time. Each line is in effect an object that will be sent downstream.
     
     
    >"% { shutdown $_  /r /t 60 }" I understand the shutdown command and
    >have used it for DOS. but what does the % does in PS?
     
    % is an alias for 'foreach'. Thus, for each machine in machines.txt, run
    the shutdown.
     
     
    >btw.... I'm totally graceful for your reply. Thank you very much.
     
    HTH
     

    Thomas Lee Doctordns@Gmail.Com
    Friday, June 04, 2010 9:46 AM

All replies

  • In your 1st script, the message displayed is on the remote machine?  Do you know if you ping all the workstations/servers involved in the reboot process?  Are they all part of the same domain so you'll be running this script as a user with high enough privileges to connect to the remote computer and get it to restart?
    Wednesday, June 02, 2010 11:49 AM
    Moderator
  • 1) I should be pretty simple simple

    > machines.txt | % { shutdown $_  /r /t 60 }  ## check help to see additional option shuch as "why this was rebooted"

    2) May be more difficult and I suspect I'm not qualified here

    > machines.txt | % { Invoke-Command {1} -Computer $_ -Cred <someone that can get on to any machine> -ErrorVariable e}   ## This is the equivalent to "Hello World" to verify if remoting works. At least I've never seen a simpler script

    But there's also the event log and who knows what else that should be checked

    3)  if ($e.count -gt 0) {[string] $body =  $e | % {[DateTime]::Now $_}}

    4) Send mail. There are a lot of good posts on this here are two.

    http://www.searchmarked.com/windows/how-to-send-an-email-using-a-windows-powershell-script.php

    http://powershell.com/cs/media/p/247.aspx

    Wednesday, June 02, 2010 2:04 PM
  • In your 1st script, the message displayed is on the remote machine?  Do you know if you ping all the workstations/servers involved in the reboot process?  Are they all part of the same domain so you'll be running this script as a user with high enough privileges to connect to the remote computer and get it to restart?


    Q1 - The message should be something like "Nightly reboot will start in (time)". if that is what you are asking.

    Q2/3 - Yes. the machines are pingable and part of the same domain. This is for a remote office. the script will be schedule from one of our servers.

    Thursday, June 03, 2010 5:15 PM
  • 1) I should be pretty simple simple

    > machines.txt | % { shutdown $_  /r /t 60 }  ## check help to see additional option shuch as "why this was rebooted"

    Taking a step at a time.... i'm truly a beginner here. :)

    "> machines.txt |" - This commands tells the script where to obtain the information from? If so, every machine needs to be in its own line correct?

    "% { shutdown $_  /r /t 60 }" I understand the shutdown command and have used it for DOS. but what does the % does in PS?

    btw.... I'm totally graceful for your reply. Thank you very much.

     

    Thursday, June 03, 2010 5:44 PM
  • 1) I should be pretty simple simple

    > machines.txt | % { shutdown $_  /r /t 60 }  ## check help to see additional option shuch as "why this was rebooted"

    I think I'm doing something wrong. I create a test file called machines.txt with the machine to reboot and place it in the same directory as the ps1 script. but I'm getting errors.

    is I enter "> machines.txt | % { shutdown $_  /r /t 60 }" ERROR: "The term '>' is not recognized as the name of a cmdlet, function....'

    if I remove the '>'. Error: "Cannot execute a document in the middle of a pipeline: %path%\machine.txt"

    Thursday, June 03, 2010 9:18 PM
  • In message
    <4a1b43a4-dc48-43a8-a86d-4f28d60f155e@communitybridge.codeplex.com>,
    Ncruz1980@?.?.invalid writes
    >
    > 1) I should be pretty simple simple
    >
    > > machines.txt | % { shutdown $_  /r /t 60 }  ## check help to see
    > additional option shuch as "why this was rebooted"
    >
    >Taking a step at a time.... i'm truly a beginner here. :)
    >"> machines.txt |" - This commands tells the script where to obtain the
    >information from? If so, every machine needs to be in its own line
    >correct?
     
    Yes - this pipes the machines.txt file into the pipeline, line at a
    time. Each line is in effect an object that will be sent downstream.
     
     
    >"% { shutdown $_  /r /t 60 }" I understand the shutdown command and
    >have used it for DOS. but what does the % does in PS?
     
    % is an alias for 'foreach'. Thus, for each machine in machines.txt, run
    the shutdown.
     
     
    >btw.... I'm totally graceful for your reply. Thank you very much.
     
    HTH
     

    Thomas Lee Doctordns@Gmail.Com
    Friday, June 04, 2010 9:46 AM
  • Thank you Thomas for breaking it down for me. Things are starting to make sense now.
    Friday, June 04, 2010 3:21 PM
  • "> machines.txt | % { shutdown $_  /r /t 60 }" ERROR: "The term '>' is not recognized as the name of a cmdlet, function....'

    if I remove the '>'. Error: "Cannot execute a document in the middle of a pipeline: %path%\machine.txt"


    Personally, I use Get-Contents rather than ">" or...

    Get-contents machines.txt | % { shutdown $_  /r /t 60 }

     

    HTH

     


    Thomas Lee Doctordns@Gmail.Com
    • Marked as answer by N. Cruz Friday, June 04, 2010 9:47 PM
    • Unmarked as answer by N. Cruz Friday, June 04, 2010 9:48 PM
    Friday, June 04, 2010 4:54 PM
  • "> machines.txt | % { shutdown $_  /r /t 60 }" ERROR: "The term '>' is not recognized as the name of a cmdlet, function....'

    if I remove the '>'. Error: "Cannot execute a document in the middle of a pipeline: %path%\machine.txt"


    Personally, I use Get-Contents rather than ">" or...

    Get-contents machines.txt | % { shutdown $_  /r /t 60 }

     

    HTH

     


    Thomas Lee Doctordns@Gmail.Com
    Here is what i entered: Get-Content C:\blp\machines.txt | % { Shutdown $1 /r /f /t 60 }
    Friday, June 04, 2010 9:50 PM
  • Personally, I use Get-Contents rather than ">" or...

    Get-contents machines.txt | % { shutdown $_  /r /t 60 }

    HTH


    Thomas Lee Doctordns@Gmail.Com
    Here is what i entered: Get-Content C:\blp\machines.txt | % { Shutdown $1 /r /f /t 60 }
    Friday, June 04, 2010 9:50 PM