Savill's FAQs: Execute a PowerShell script in Windows PE

Three times a week, John Savill tackles your most pressing IT questions. Today learn about using your own images to create a VM using PowerShell, wiping a disk when using the Windows PE environment, and how to execute a PowerShell script in Windows PE.

John Savill

December 23, 2017

6 Min Read
John Savill'ss FAQs on IT Pro Today Hero

Three times a week (Monday/Wednesday/Friday), John Savill tackles your most pressing IT questions.

Read through the FAQ archives, or send him your questions via email.

Today: See how to create your own VMs on Azure from your uploaded images, wiping disks in the Windows PE environment, and the method to execute a PowerShell script in Windows PE.

Q. I uploaded my own image to Azure, how can I easily create a new VM from it using PowerShell?

A. The only difference when using a custom image compared to using an Azure marketplace image is that you provide the ID of the image you have created. Everything else is the same. For example, in the script below, I created a new virtual machine from my own custom image. This will result in a VM using managed disks. I created a storage account but this is only for the boot diagnostics to use, not the actual OS disk.

$location = "South Central US"
$rgname = "POCRG"
$vmName = "poc"
$computerName = "poc"
$vmSize = "Standard_DS1_v2"
$netRG = "RG_SCUS"
$vnetname = "RG_SCUS-vnet"
$subnetname = "default"
$stotype = 'Standard_LRS'
$stoname = "sasavpocdiag"

$user = "localadmin"
$password = 'Pa55word4242'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword)

$imageName = "WinPEImage"
$rgImgName = "RGSCUSPOC"
$image = get-azurermimage -ImageName $imageName -ResourceGroupName $rgImgName

$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $netrg -Name $vnetName

New-AzureRmResourceGroup -Name $rgname -Location $location

$ipName = "POCPip"
$pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location `
-AllocationMethod Dynamic

$nicName = "pocNIC"
$nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location `
-SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $pip.Id

$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
$vm = Set-AzureRmVMSourceImage -VM $vm -Id $image.Id

$vm = Set-AzureRmVMOSDisk -VM $vm -DiskSizeInGB 1024 `
-CreateOption FromImage -Caching ReadWrite

$vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $computerName `
-Credential $cred #-ProvisionVMAgent -EnableAutoUpdate

$vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id

New-AzureRmStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $location -Type $stotype
$vm = Set-AzureRmVMBootDiagnostics -VM $vm -Enable -ResourceGroupName $rgName -StorageAccountName $stoname

New-AzureRmVM -VM $vm -ResourceGroupName $rgName -Location $location

Q. I've booted a machine from Windows Preinstallation Environment (PE) and want to lay down an OS. But how can I wipe the disk if I'm booted from the PE running on it?

A. If you look closely at the process you use to create a Windows Preinstallation Environment, you do a few things:

  1. You copy a PE distribution to a folder

  2. You then mount the boot.wim found in that folder

  3. You add content via the mount

  4. You then dismount the boot.wim and make media from the folder structure

When you boot from Windows PE (which includes the boot.wim), the content of the .wim file is what you actually use and is copied to a writable RAM drive which surfaces as the X: drive. This means the actual physical disk that contained the PE can still be wiped and a new OS placed on it.

For example in my environment I do the following:

Diskpart /s x:parts.txt

where parts.txt contains the following to clean and create the required system and windows partition.

Select disk 0
Clean
create partition primary size=350
format quick fs=ntfs label="System"
assign letter="S"
active
create partition primary
format quick fs=ntfs label="Windows"
assign letter="W"

Then I simply apply an image and set the disk as bootable:

x:Imagex /apply "z:install.wim" 2 w:
W:WindowsSystem32bcdboot W:Windows /l en-US

 

Q. I'm trying to execute a PowerShell script inside Windows PE but its failing because of policy. How can I fix this?

A. If you have a Windows PE environment and are trying to launch a PowerShell script, you may see if fails because of execution policy. The best way to solve this is to launch the PowerShell using the following command:

powershell -executionpolicy bypass -nologo -noprofile -file z:unattendupdate.ps1

This tells it to launch PowerShell but bypass the execution policy.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like