· 9 years ago · Mar 14, 2017, 01:22 AM
1# Image deployment script
2
3# Get the ID and security principal of the current user account
4$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
5$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
6
7# Get the security principal for the Administrator role
8$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
9
10# Check privledges
11if ($myWindowsPrincipal.IsInRole($adminRole))
12 {
13 # Change window title and background colour to indicate admin rights
14 $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
15 $Host.UI.RawUI.BackgroundColor = "Red"
16 clear-host
17 }
18else
19 {
20 # If not currently running as an admin, restart with admin privledges
21
22 $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
23
24 $newProcess.Arguments = $myInvocation.MyCommand.Definition;
25
26 $newProcess.Verb = "runas";
27
28 [System.Diagnostics.Process]::Start($newProcess);
29
30 # Exit from the current, unelevated process
31 exit
32 }
33
34# Imaging script. Please modify for your own site
35
36# This section resets the WDSSERVER service and boot images
37[BOOLEAN]$global:xExitSession=$false
38import-module IntelvPro
39$amtcred = Read-AmtCredential
40CLS
41write-host "Starting service and disabling boot images" -ForegroundColor Green
42$swcount = "-N"
43net start wdsserver | Out-Null
44Disable-WdsBootImage -Architecture x64 -ImageName "Multicast WDS" | Out-Null
45Disable-WdsBootImage -Architecture x86 -Imagename "Image C and D Reboot" | Out-Null
46Disable-WDSBootImage -Architecture X86 -ImageName "Image C Reboot" | Out-Null
47Disable-WdsBootImage -Architecture X86 -ImageName "Image D Reboot" | Out-Null
48Disable-WdsBootImage -Architecture X86 -ImageName "Ghost32 (Capture)" | Out-Null
49Disable-WdsBootImage -Architecture X86 -ImageName "CleanDisks image c reboot" | Out-Null
50CLS
51
52# User input requested for image selection and lab choice
53write-host "Please select a ghost image" -ForegroundColor Yellow
54while ( $xMenuChoiceA -lt 1 -or $xMenuChoiceA -gt 8 ) {
55write-host "`t`t`t1. DWT Windows 8 & Office 2010" -Fore Cyan
56write-host "`t`t`t2. DWT Windows 8 & Office 2013" -ForegroundColor Cyan
57write-host "`t`t`t3. Windows 7 & Office 2010 (Dell)" -ForegroundColor Cyan
58write-host "`t`t`t4. Windows 8 & Office 2010 (Dell)" -Fore Cyan
59write-host "`t`t`t5. Windows 7 & Office 2010 (ThermalTake)" -ForegroundColor Cyan
60write-host "`t`t`t6. Windows 8 & Office 2010 (ThermalTake)" -ForegroundColor Cyan
61write-host "`t`t`t7. Generic image" -ForegroundColor Cyan
62write-host "`t`t`t8. MSFT test image" -ForegroundColor Cyan
63write-host " "
64[int] $xMenuChoiceA = read-host "Please select option 1 to 8..." }
65CLS
66
67# This next step can be removed if you want to set the number of computers as fixed. Make sure you update the ghost command line section below
68
69Write-Host " "
70$count = Read-Host "How many machines require ghosting?"
71CLS
72write-host "Please select a Lab" -ForegroundColor Yellow
73while ( $xMenuChoiceB -lt 1 -or $xMenuChoiceB -gt 4 ) {
74write-host "`t`t`t1. Lab 1.5" -Fore Cyan
75write-host "`t`t`t2. Lab 1.6 - High Spec Lab" -ForegroundColor Cyan
76write-host "`t`t`t3. Lab 1.7 - High Spec Lab" -ForegroundColor Cyan
77write-host "`t`t`t4. Lab 1.8" -Foregroundcolor Cyan
78write-host " "
79[int] $xMenuChoiceB = read-host "Please select option 1 to 4..."}
80# User inputs are injected into the commands below, to start the Ghostcast session
81Switch ( $xMenuChoiceA ) {
821{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
832{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
843{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
854{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
865{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
876{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
887{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
898{ Enable-WdsBootImage -Architecture x86 -Imagename "Image C and D Reboot"; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile.gho" 1 $swcount$count -C -LE; c:\ghost\GhostSrv.exe "\\server details\filepath\ghostfile d drive.gho" 2 $swcount$count -C -LE -FC:\Scripts\Logs\Ghost.log }
90
91}
92# Switches are changed to deployment vlan, computers are powered on and instructed to PXE boot. Script sleeps, disables WDSSERVER service, waits and then continues
93Switch ( $xMenuChoiceB ) {
941{ perl c:\scripts\perl\Lab15_Vlan4.pl; get-content c:\scripts\labs\lab15.txt | Invoke-AMTForceBoot -Device PXE -Operation PowerOn -Credential $amtcred; write-host "Imaging in progress..."; $a = get-process Ghostsrv; $a.WaitForExit(); write-host "Program Closed"; net stop wdsserver; write-host "WDS Service Stopped" }
952{ perl C:\Scripts\perl\Lab16_Vlan4.pl; get-content c:\scripts\labs\lab16.txt | Invoke-AMTForceBoot -Device PXE -Operation PowerOn -Credential $amtcred; write-host "Imaging in progress..."; $a = get-process Ghostsrv; $a.WaitForExit(); write-host "Program Closed"; net stop wdsserver; write-host "WDS Service Stopped" }
963{ perl C:\Scripts\Perl\Lab17_Vlan4.pl; get-content c:\scripts\labs\lab17.txt | Invoke-AMTForceBoot -Device PXE -Operation PowerOn -Credential $amtcred; write-host "Imaging in progress..."; $a = get-process Ghostsrv; $a.WaitForExit(); write-host "Program Closed"; net stop wdsserver; write-host "WDS Service Stopped" }
974{ perl C:\Scripts\perl\Lab18_Vlan4.pl; get-content c:\scripts\labs\lab18.txt | Invoke-AMTForceBoot -Device PXE -Operation PowerOn -Credential $amtcred; write-host "Imaging in progress..."; $a = get-process Ghostsrv; $a.WaitForExit(); write-host "Program Closed"; net stop wdsserver; write-host "WDS Service Stopped" }
98}
99
100# Email alerts are generated, and the ghost.log is added as an attachment. The content is also extracted and sent in the body of the email.
101$SMTPServer = "smtp.gmail.com"
102$SMTPPort = "587"
103$Username = "@gmail.com"
104$Password = ""
105$file = "C:\scripts\logs\ghost.log"
106$to = ""
107$subject = "Lab Imaging Complete"
108$body = get-content C:\Scripts\Logs\ghost.log
109$att = New-Object Net.Mail.Attachment ($file)
110
111$message = New-Object System.Net.Mail.MailMessage
112$message.subject = $subject
113$message.body = $body
114$message.to.add($to)
115$message.from = $username
116
117$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
118$smtp.EnableSSL = $true
119$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
120$message.Attachments.Add($att)
121$smtp.send($message)
122write-host "Mail Sent"
123
124# Android alert notifications are generated, and the ghost.log file is injected into the notification
125# Alternative services such as Pushover.net can accomodate devices other than android
126# param ($server, $shortname, $status, $datetime, $group, $prioritytext, $down, $message, $lastvalue)
127$server = get-content env:computername
128$datetime = get-date -DisplayHint DateTime -Format G
129$status = Get-Content C:\Scripts\Logs\ghost.log
130
131# This is the unique API Key associated with my account
132# $nmaurl = 'https://www.notifymyandroid.com/publicapi/notify'
133# $nmaapikey = 'api key goes here'
134
135# The priority can be changed from -2 (very low) up to 2 (emergency). 0 is Normal
136#$priority = [regex]::matches($prioritytext,"\*").Count
137#priority = $priority-0
138
139# This defines the message sent
140# $parameters = 'apikey=' + $nmaapikey + '&application=PRTG&event=' + $server + ' ' + $shortname + ' ' + $status + '&description=' + $datetime + ' ' + $group + ' Last value: ' + $lastvalue + '&priority=' + $priority
141#$http2 = New-Object -ComObject Msxml2.XMLHTTP
142#$http2.open('POST', $nmaurl, $false)
143#$http2.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
144#$http2.setRequestHeader("Content-length", $parameters.length)
145#$http2.setRequestHeader("Connection", "close")
146#$http2.send($parameters)
147#$http2.statusText
148
149#iphone section - this will send the ghost log in a notification
150import-module ps-pushover
151$token = 'token here'
152$user = 'api key here'
153Send-PushoverMessage -title "Deployment Complete" -message $body -sound "gamelan" -token $token -user $user
154
155# Ghost.log is renamed according to current date/time for future reference
156# C:\scripts\ps\Stamp.ps1 C:\scripts\logs\ghost.log
157# Starts a 30 minute delay, then changes the vlan back from deployment
158write-host "Beginning 30 minute wait prior to vlan reset"
159Switch ( $xMenuChoiceB ) {
1601{ start-sleep -s 1800; perl C:\Scripts\Perl\Lab15_Vlan5.pl }
1612{ start-sleep -s 1800; perl C:\Scripts\Perl\Lab16_Vlan6.pl }
1623{ Start-Sleep -s 1800; perl C:\Scripts\Perl\Lab17_Vlan7.pl }
1634{ Start-sleep -s 1800; perl C:\Scripts\perl\Lab18_Vlan8.pl }
164}