Monday, August 22, 2011

OS Deploy script for scheduling reinstalls

Specops Deploy / OS has the ability to reinstall one or multiple computers in a AD, fully automated. And luckily there is full Powershell support in the product so it's also possible to schedule reinstallation to happen on off hours, in the middle of the night when both the users and admins (hopefully) are sleeping.

I'm far from a Powershell guru, but here is a first version of a script that can be used to reinstall one or multiple PC's from for example Windows Task Scheduler.
Depending on customer demand and wishes there will be updated versions with more features.


###############################################################################
#
# NAME: Specops Deploy /OS Reinstall
#
# AUTHOR:  Markus Lassfolk, Specops Software
#
# VERSION HISTORY:
# 1.0 2011-06-18 - Initial release with support for ComputerNames
#
# TODO:
# Error Handling
# Support for -OU
# Support for -Group
# WOL Support
#
# Pre-Requisits:
# AD Permissions to Reinstall Computers with Specops Deploy / OS.
# Specops Deploy / OS Admin Tools Installed
# Specops GPUpdate Pro Installed
# PowerShell 2.0
# Clients need to have GUID pre-populated in the AD
# (for example via PreStageTool.Exe)
#
# Description:
# A Script for Specops Deploy / OS to reinstall Clients in an AD via script.
# The Script will first list the PC's that will be reinstalled and count down
# for 30 seconds, before any actions will be carried out.
#
# Syntax:
# SpecopsOSDeployReinstall.PS ComputerName
# ComputerName can be one or multiple computernames separated by Space
#
# Example 1
# SpecopsOSDeployReinstall.PS COMPUTERNAME1 COMPUTERNAME2 COMPUTERNAME3
# Will Reinstall all three computers
#
# Example 2
# SpecopsOSDeployReinstall.PS CLIENT*
# Will Reinstall all AD Computers starting with CLIENT*
#
# Example 3
# SpecopsOSDeployReinstall.PS *02*
# Will reinstall all AD Computers with a name containing 02.
#
###############################################################################



# Script Start
Add-PSSnapIn Specops.Adx, Specops.Gpupdate, Specops.Gpupdatepro –Erroraction:0

#"Arguments: $($args.count)"
#Write-Host Running script with arguments; $args

function SetComputerForInstall([string]$computerName)
{
    $computer = [ADSI] "LDAP://$computerDn"
    Write-Host
 Write-Host Processing $computerDn
    Write-Host
 
 if ($computer.Path -ne $null)
    {
        $computer.netbootMachineFilePath = "\BOOT\x86\PXEBOOT.N12"
        $computer.SetInfo()
       
        $specopsOsDeploy = [ADSI] "LDAP://CN=Specops OS Deploy,$computerDn"
        if ($specopsOsDeploy.Path -eq $null)
        {
            $specopsOsDeploy = $computer.Create("classStore", "CN=Specops OS Deploy")
            $specopsOsDeploy.SetInfo()
        }
       
        $specopsOsDeploy.description = "Reinstall"
        $specopsOsDeploy.SetInfo()
        $specopsOsDeploy.PutEx(1, "displayName", $null)
        $specopsOsDeploy.SetInfo()
        $specopsOsDeploy.url = "$_"
        $specopsOsDeploy.SetInfo()
    }
    else
    {
        Write-Host "No computer found"
    }
}

function CountDown() {
    param(
    [int]$hours=0,
    [int]$minutes=0,
    [int]$seconds=0,
    [switch]$help)
 
    if ($help -or (!$hours -and !$minutes -and !$seconds)){
        write-host $HelpInfo
        return
        }
    $startTime = get-date
    $endTime = $startTime.addHours($hours)
    $endTime = $endTime.addMinutes($minutes)
    $endTime = $endTime.addSeconds($seconds)
    $timeSpan = new-timespan $startTime $endTime
#    write-host $([string]::format("`nScript paused for {0:#0}:{1:00}:{2:00}",$hours,$minutes,$seconds)) -backgroundcolor black -foregroundcolor yellow
    while ($timeSpan -gt 0) {
        $timeSpan = new-timespan $(get-date) $endTime
        write-host "`r".padright(40," ") -nonewline
        write-host "`r" -nonewline
        write-host $([string]::Format("`rTime Remaining: {0:d2}:{1:d2}:{2:d2}", `
        $timeSpan.hours, `
        $timeSpan.minutes, `
        $timeSpan.seconds)) `
        -nonewline -backgroundcolor black -foregroundcolor yellow
        sleep 1
        }
    write-host ""
    }
 


Write-Host These computers will be triggered for reinstall in 30 seconds.
Write-Host
Get-SpecopsADComputer $args  ForEach-Object { Write-host $_.DistinguishedName }

Write-Host
Write-Host You have a chance to abort the script now without any changes.
countdown -seconds 30


Write-Host Executing Reinstall command.
# Trigger Clients for Reinstallation.
Get-SpecopsADComputer $args  ForEach-Object { $computerDN = $_.DistinguishedName
Write-Host $computerDN
SetComputerForInstall $computerDN
}

# Alert the user and Forcefully Restart client after 120 seconds.
Write-Host Alerting and Performing a remote reboot on all effected clients.
Get-SpecopsADComputer $args  Restart-SpecopsComputer -Message:"Your computer will automatically restart and reinstall in 2 minutes. Please save all your work!" -ForceCloseApplications -WarningTime:120 

# End of Script 


Save that script on a computer that fullfils the requirements stated above and create a new Scheduled Task.

Set it to start a Program: powershell.exe
With a commandline like this;
-Command " & 'C:\temp\PowerShell Script\SpecopsOSDReinstall_1.0.ps1' COMPUTERNAME1 COMPUTERNAME2 CLIENT*" -NoProfile -Noninteractive


And of course, make sure the account executing the task has the appropriate rights.

Tuesday, August 9, 2011

Using Specops Command Scripts to Restart Multiple Services

Specops Command is a great product bringing the power of PowerShell into Group Policy allowing admins to create pretty much any script and deploy it with Specops Command.
Here's a script to ensure all Specops services associated with the a demo server be started at all times and if they weren't then start them and report the status to the Specops Reporting Database.  Here's the result.

$computername = $env:computername
Get-Service -DisplayName "*Specops*"  | %{
if ($_.Status -eq "Stopped" ){
Start-Service -ServiceName $_.Name
}
}
send-feedback "Sucessfully started the Specops Services on $computername"


Can be used with Specops Command which is available for download from here.


Monday, August 8, 2011

Specops Password Reset 5.0 from an end user perspective.

How users reset their own Active Directory passwords using Specops Password Reset 5.0

Specops Password Reset 5.0 Demo from Specops Software on Vimeo.