Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created March 24, 2023 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnLBevan/71a88bb5e70dd345b5911ad1410a02b3 to your computer and use it in GitHub Desktop.
Save JohnLBevan/71a88bb5e70dd345b5911ad1410a02b3 to your computer and use it in GitHub Desktop.
After doing a lift & shift of VMs into Azue, their page file may still be on the C drive (or whatever its original location), rather than on the temporary storage available to the new AZ VM (giving better perfromance). This script scans all VMs to help flag such issues.
Login-AzAccount # opens browser for interactive login
$subscriptions = Get-AzSubscription |
Where-Object {$_.State -eq 'Enabled'} |
Select-Object -ExpandProperty 'Id'
$script = @'
$driveInfo = Get-PSDrive -PSProvider FileSystem | Select-Object Root, Description
$pageFileDrive = Get-WmiObject Win32_Pagefile | Select-Object -ExpandProperty Drive
([PSCustomObject]@{
DriveInfo = ($driveInfo | %{"$($_.Root) = $($_.Description)"}) -join '; '
PageFileDrive = $pageFileDrive
PageFileOnTemp = [bool]($driveInfo | ?{$_.Root -eq "$pageFileDrive\"} | ?{$_.Description -eq 'Temporary Storage'})
}) | ConvertTo-Json -Depth 3
'@
foreach ($subscription in $subscriptions) {
Set-AzContext -Subscription $subscription | Out-Null
$vms = Get-AzVM -Status |
Where-Object {$_.PowerState -eq 'VM running' } |
Where-Object {$_.StorageProfile.OSDisk.OSType -eq 'Windows'}
Write-Verbose "Found $($vms.Count) VMs in sub [$subscription]" -Verbose
$vms | ForEach-Object -Parallel {
$result = (Invoke-AzVMRunCommand -ResourceGroupName $_.ResourceGroupName -Name $_.Name -CommandId 'RunPowerShellScript' -ScriptString $using:script)
$result.value[0].message | ConvertFrom-Json -Depth 3 |
Add-Member -Type NoteProperty -Name 'Sub' -Value $using:subscription -PassThru |
Add-Member -Type NoteProperty -Name 'RG' -Value ($_.ResourceGroupName) -PassThru |
Add-Member -Type NoteProperty -Name 'VM' -Value ($_.Name) -PassThru
} | Export-Csv -Path ".\$($subscription)_VMInfo.csv"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment