We help IT Professionals succeed at work.

PowerShell Script Request: Remote Windows 2016 Server Page File and Location.

PowerShell Script Request: Remote Windows 2016 Server Page File and Location.
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
Cobbled this together some time ago; it uses WMI and generates a detailed output; you can optionally ...
* use -FailFast to ping machines before trying to query them,
* pass a Credentials object,
* pass a file to directly export the results to
If no export file is given, the results will end up in the pipeline and can be processed further as usual.
Pipeline input is supported, so you can do something like
Get-ADComputer -Filter "Name -like 'foo*'" | .\Get-PageFileLocationByWmi.ps1 -FailFast | Format-Table -AutoSize

Open in new window


[CmdletBinding()]
Param(
	[Parameter(ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True, Position=0)]
	[Alias('ComputerName')][String[]]$Name = @($ENV:ComputerName),
	[Switch]$FailFast,
	[System.Management.Automation.PSCredential]$Credential,
	[String]$ExportCsv
)
Begin {
	$results = @()
	$resultProperties = 'Computername', 'SystemManaged', 'Name', 'InitialSize', 'MaximumSize', 'AllocatedBaseSize', 'CurrentUsage', 'PeakUsage', 'Exception'
	$wmiArgs = @{'Namespace' = 'root\cimv2'; 'ErrorAction' = 'Stop'}
	$collect = @{}
	$ping = New-Object -TypeName System.Net.NetworkInformation.Ping
	Function Set-ResultProperties($Hashtable, $Computername, $SystemManaged, $Name, $InitialSize, $MaximumSize, $AllocatedBaseSize, $CurrentUsage, $PeakUsage, $Exception) {
		If (-not $Hashtable.ContainsKey($Name)) {
			$Hashtable[$Name] = '' | Select-Object -Property $resultProperties
		}
		ForEach ($Property In ($PSBoundParameters.Keys | Where-Object {$_ -ne 'Hashtable'})) {
			$Hashtable[$Name].$Property = $PSBoundParameters[$Property]
		}
	}
}
Process {
	ForEach ($computer In $Name) {
		Try {
			$collect.Clear()
			Write-Host -Object "Processing $($computer) ... " -Foregroundcolor White -NoNewline
			If ($FailFast -and (($ping.Send($computer, 1000)).Status -ne 'Success') -and (($ping.Send($computer, 500)).Status -ne 'Success')) {
				Throw 'Offline'
			}
			$wmiArgs['ComputerName'] = $computer
			If ($Credential -and ($computer.Split('.')[0] -ne $ENV:ComputerName)) {
				$wmiArgs['Credential'] = $Credential
			}
			$systemManaged = (Get-WmiObject -Query 'Select AutomaticManagedPagefile From Win32_ComputerSystem' @wmiArgs).AutomaticManagedPagefile -eq $True
			Get-WmiObject -Query 'Select AllocatedBaseSize, CurrentUsage, Name, PeakUsage From Win32_PageFileUsage' @wmiArgs | ForEach-Object {
				Set-ResultProperties -Hashtable $collect -Name $_.Name -ComputerName $computer -SystemManaged $systemManaged -AllocatedBaseSize $_.AllocatedBaseSize -CurrentUsage $_.CurrentUsage -PeakUsage $_.PeakUsage
			}
			If (-not $systemManaged) {
				Get-WmiObject -Query 'Select InitialSize, MaximumSize, Name From Win32_PageFileSetting' @wmiArgs | ForEach-Object {
					Set-ResultProperties -Hashtable $collect -Name $_.Name -ComputerName $computer -SystemManaged $systemManaged -InitialSize $_.InitialSize -MaximumSize $_.MaximumSize
				}
			}
			Write-Host -Object 'OK' -Foregroundcolor Green
		} Catch {
			Set-ResultProperties -Hashtable $collect -Name '' -ComputerName $computer -Exception $_.Exception.Message
			Write-Host -Object "ERROR: $($_.Exception.Message)" -Foregroundcolor Red
		}
		$results += $collect.Values
	}
}
End {
	If ([string]::IsNullOrEmpty($ExportCsv)) {
		$results | Sort-Object -Property ComputerName, Name
	} Else {
		$results | Sort-Object -Property ComputerName, Name | Export-Csv -Path $ExportCsv -NoTypeInformation
	}
}

Open in new window

Author

Commented: