«

»

Aug 23

Powershell Code: Get a Server’s Pagefile Configuration

The following Powershell code discovers and displays a server’s pagefile configuration.

    # Get information on the pagefile size and location and save to variable
    write-host “Attempting to read pagefile data for $computer.  Errors may occur if the pagefile is managed by the system.” `r
    $PageFile = Get-WmiObject -class “Win32_PageFileSetting” -computername $Computer
    $PageFileCount = $PageFile.Count

    IF ($PageFileCount -gt 1)
         { ## OPEN IF – There are multiple pagefiles
            write-Host “Gathering multiple PageFile data on $computer …”
            $PageFileCount–
            For ($PFCount = 0;$PFCount -le $PageFileCount;$PFCount++)
                {  ## OPEN Bracket FOR loop to total PageFile size
                $PageFileMaxSize = $PageFileMaxSize + $PageFile[$PFCount].MaximumSize
                $PageFileSizeArray += @($PageFileMaxSize)
                $PageFileCaption = $PageFile[$PFCount].Name
                $PageFileLoc = $PageFileCaption.Substring(0,2)
                $PageFileLocation += @($PageFileLoc)
                }  ## CLOSE Bracket FOR loop to total PageFile size
            $PageFileSize = $PageFileMaxSize
            IF (!$PageFile) {$SystemPageFile = “Yes”} ELSE {$SystemPageFile = “No”}
         } ## CLOSE IF – There are multiple pagefiles

     ELSE
         { ## OPEN IF – There is 1 pagefile       
      IF (!$PageFile)
        { ## OPEN IF – There is no data in the Pagefile variable
            write-Host “PageFile on $computer is managed by the system.  Gathering data now …”
            $PageFile = Get-WmiObject -computer $computer -Class Win32_PageFileUsage
            $PageFileSize = $PageFile.PeakUsage
            $PageFileName = $PageFile.Name
            $PageFileLocation = $PageFileName.Substring(0,2)
            IF (!$PageFile) {$SystemPageFile = “No”} ELSE {$SystemPageFile = “Yes”}
         } ## CLOSE IF – There is no data in the Pagefile variable
       ELSE
        { ## OPEN IF – There is data in the Pagefile variable
            write-Host “Gathering PageFile data on $computer …”
            # $PageFileInitSize = $PageFile.InitialSize
            $PageFileMaxSize = $PageFile.MaximumSize
            $PageFileSize = $PageFileMaxSize
            $PageFileName = $PageFile.Name
            $PageFileLocation = $PageFileName.Substring(0,2)
            IF (!$PageFile) {$SystemPageFile = “Yes”} ELSE {$SystemPageFile = “No”}
        } ## CLOSE IF – There is data in the Pagefile variable    
    } ## CLOSE IF – There is 1 pagefile