PowerShell: Reboot remote computers

PowerShell logo

It looks like it’s going to be a week of scripting blog posts.  Today’s script is used to reboot a list of remote computers.  Sometimes, when deploying security patches, the computers fails to reboot.  So I use this script to force reboots.  It would be a piece of cake to modify the script, so that it turns off the computers instead.

One of my customers should find that useful.  Out of 2000 computers, about 150 computers get left powered on over the weekend.

What the script does:

  • reads the list of computers to be reset from C:\computer_lists\reboot_target_list.txt
    the format of the list is COMPUTERNAME,Reboot Reason
  • “Pings” the computer to see if it’s on the network.
  • checks to see if someone is logged on.  If someone is, don’t reboot.
  • reboots the computer.

Check and reboot computers.ps1

Continue reading

PowerShell: count of files in a directory

PowerShell logoWith PowerShell, I needed to get a count of files in a particular directory.  This is what I came up with.

#
# Get a count of files in a directory.
#
$directory_file_count = "0"
$server_directory_string = "\\WISEFAQDC\c$\downloads"

# check that the directory exists.
$does_directory_exist = (Test-Path $server_directory_string)

# if it does, then continue
if ($does_directory_exist)
{
# file count does include directories but not a count of their contents.
$directory_file_count = (get-childitem $server_directory_string -name).count
Write-Host "Directory file count: $directory_file_count"
}
else
{
# directory doesn't exist
Write-Host "Directory $server_directory_string - DOES NOT EXIST"
}

The only problem with this code is that file count does include directories, but not their contents,  in it’s count of files.

Bookmark and Share

Reading the end of a log file with PowerShell

PowerShell logo I needed to read the last line of a log file.  PowerShell made it very, very, easy.

All you need to do is Get-Content the file, and then pipe it to Select-Object with the –last parameter set to 1.

Here is the code snippet which does that:

$failure_reason = (Get-Content $file_txt | Select-Object -last 1)

And here is a (simple) version of the PowerShell script I wrote to read though a list of computers, and dump the last line to the screen:

$windows2000_PCs = (Get-Content c:\temp\pc-list.txt)
foreach ($computer in $windows2000_PCs)
{
$file_txt = "\\"+ $computer + "\c$\log\install.txt"
$install_result = (Get-Content $file_txt | Select-Object -last 1)
Write-Host $computer has install result of: $install_result
}
Write-Host Done!

Which outputs:

BROOMFONDLE has install result of: Program installed successfully on 21 DEC 2010 – 1000hrs
MAGICTHIGHS1 has install result of: Program failed to install – Error code 1603 on 11 DEC 2010 - 1321hrs
Done!

Things to note:

  • c:\temp\pc-list.txt contains the list of computer names you wish to scan.
  • the script is not idiot-proofed error-trapped.
    ie. in a production version of this you would check that the computer being scanned is online, the install.txt file exists, and so on.
  • the Get-Content cmdlet will read though the entire file.  Select-Object will retain the –last x lines you asked for.  (for the technically minded, on my Windows 7 system, it opened the file with the following options “Desired Access: Generic Read, Disposition: Open, Options: Synchronous IO Non-Alert, Open No Recall, ShareMode: Read, Write”

Bookmark and Share