Deploy custom OS image – add PowerShell script to Windows setup

I’ve been working with a customer who requested to prepare custom OS image with below requirements:

  • installed operating system: Windows Server 2012 R2 Standard
  • installed all latest OS updates (on time when image was build)
  • installed both SCOM and SCCM agents (the SCCM agent is connected to the customer primary site and SCOM agent connection based on Active Directory assignment)
  • pagefile located on P drive (size: 1.5 x memory value)
  • all DATA disks formatted with partitions and disk letters D: / E:

That image was used to provision new virtual machines using System Center Virtual Machine Manager. Customer has informed me that he doesn’t want to use service templates and each virtual machine needs to be provisioned as normal virtual machine template, not as service deployment. I couldn’t use Application Profile and its feature – Script Application as it doesn’t work with virtual machine templates, only service templates. To meet all requirements  I need to inject PowerShell code during first virtual machine boot. To do it I’ve used SetupComplete.cmd file (https://technet.microsoft.com/en-us/library/cc766314(v=ws.10).aspx). That file enables you to run custom scripts and make modifications to the system before a user logs on. My SetupComplete.cmd file was written in a way to start PowerShell script located in C:\Temp directory. Below I’m attaching both script:

SetupComplete.cmd

@echo off
@powershell -NonInteractive -NoProfile -ExecutionPolicy Unrestricted -Command “& {Start-Process PowerShell -ArgumentList ‘-NoProfile -ExecutionPolicy Bypass -File C:\Temp\PostDeployment.ps1’ -Verb RunAs}”
exit /B %errorlevel%

 

PostDeployment.ps1

[cmdletbinding()]

#region Prepare PageFile
#—————————
#Set disk for PageFile
$PageFileDisk = Get-Disk -ErrorAction Continue | Where-Object {$_.Number -eq “1” -and $_.PartitionStyle -eq “RAW”} -ErrorAction Continue | Initialize-Disk -PartitionStyle GPT -PassThru -ErrorAction Continue | New-Partition -DriveLetter “P” -UseMaximumSize -ErrorAction Continue | Format-Volume -FileSystem NTFS -NewFileSystemLabel “PageFile” -Confirm:$False -ErrorAction Continue

If ($PageFileDisk.DriveLetter -eq “P”) {
#Remove old PageFile
Set-CimInstance -Query “SELECT * FROM Win32_ComputerSystem” -Property @{AutomaticManagedPageFile=”False”}
$OldPageFile = Get-WmiObject -Query “SELECT * FROM Win32_PageFileSetting WHERE Name=’C:\\pagefile.sys'”
$OldPageFile.Delete()

#Create new PageFile
Set-WMIInstance -class Win32_PageFileSetting -Arguments @{name=”P:\pagefile.sys”;MaximumSize=0}
}
#endregion

#Set DATA disks
Get-Disk | Where-Object {$_.PartitionStyle -eq “RAW”} -ErrorAction Continue | Initialize-Disk -PartitionStyle GPT -PassThru -ErrorAction Continue | New-Partition -AssignDriveLetter -UseMaximumSize -ErrorAction Continue | Format-Volume -FileSystem NTFS -Confirm:$False -ErrorAction Continue

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.