Powershell - Add page file Disk and set the page file on a VM inSCVMM 2012
Hi
I was looking at writing a script to add a separate disk to a VM and mount it , format it and set it as Page file on your VM . This script will do the Maths and select the page file 2 times as your VM memory and format it as 1.95 times leaving some room for NTFS partition. It will let you remote onto the VM using local administrator credentials and formats the drive and set it as page file drive as well .
Copy and paste the below code and I hope you will like it
$JobGroupID = [Guid]::NewGuid().ToString()
$vmname = Read-Host " Enter the VMNAME"
$username = $vmname + "\Administrator"
$cre = Get-Credential -UserName $username -Message " Enter the local administrator"
$memory = Get-SCVirtualMachine -name $vmname | select memory | Format-Table -HideTableHeaders
$VM = Get-SCVirtualMachine -Name $vmname
refresh-VM -VM $vm
Shutdown-VM -vm $VM
$memory = @()
$memory += Get-SCVirtualMachine -name $vmname | select memory
$memory = [string]$memory
$memory.getType().Fullname
$memory = $memory.trimstart('@{Memory=')
$memory = $memory.replace('}', '')
[String[]]$memory = $memory
$memory
$memory.getType().Fullname
$memory = [string]$memory
$pagefilesize = 2 *"$memory"
$pagefilesize
$memory.getType().Fullname
$filename= $vmname +"_Pagefile"
New-SCVirtualDiskDrive -IDE -Bus 1 -LUN 1 -JobGroup $jobGroupID -VirtualHardDiskSizeMB $pagefilesize -Dynamic -Filename $filename -VolumeType None -VirtualHardDiskFormatType VHD
Set-SCVirtualMachine -VM $VM -Name $vmname -Description "" -JobGroup $jobGroupID -RunAsynchronously
sleep -Seconds 20
Start-SCVirtualMachine -VM $VM
Write-host " Waiting for VM to power On . Disk initializing will start in a moment " -BackgroundColor Magenta
sleep -Seconds 180
###################### Initialize the Disks##############################################
$pagefilesize =[system.math]:: Round( 1.95* "$memory")
$pagefilesize = [int] $pagefilesize
$pagefilesize
Write-host " now the drive is getting monuted,formated and pagefile getting assigned" -BackgroundColor Magenta
$session = New-PSSession -ComputerName $vmname -Credential $cre
Invoke-Command -Session $session -ScriptBlock { Param($pagefilesize)
get-disk | where number -EQ 0 | set-disk -IsOffline $false
Get-Disk | Where number -eq 0 | Initialize-Disk -PartitionStyle MBR -PassThru | New-Partition -DriveLetter P -UseMaximumSize | Format-Volume -FileSystem NTFS -NewFileSystemLabel "Pagefile" -Confirm:$false
$ComputerSystem = Get-WmiObject -Class Win32_ComputerSystem -EnableAllPrivileges
if($ComputerSystem.AutomaticManagedPagefile)
{$ComputerSystem.AutomaticManagedPagefile = $false
$ComputerSystem.Put() }
$CurrentPageFile = gwmi -Query "select * from Win32_PageFileSetting where name='c:\\pagefile.sys'" -EnableAllPrivileges
# Delete current paging file on drive C
If($CurrentPageFile){$CurrentPageFile.Delete()}
swmi win32_pageFileSetting -Arguments @{Name='P:\pagefile.sys';}
$pagefilesize
$pagefile = Get-WmiObject win32_pagefilesetting
$pagefile.initialsize = [int]$pagefilesize
$pagefile.MaximumSize = [int]$pagefilesize
$pagefile.put()
}-ArgumentList $pagefilesize