functions/Get-DbaPageFileSetting.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function Get-DbaPageFileSetting {
    <#
.SYNOPSIS
Returns information about the network connection of the target computer including NetBIOS name, IP Address, domain name and fully qualified domain name (FQDN).
 
.DESCRIPTION
   WMI class Win32_ComputerSystem tells us if Page File is managed automatically.
   If TRUE all other properties do not exist.
   If FALSE classes Win32_PageFile, Win32_PageFileSetting en Win32_PageFileUsage are examined.
   CIM is used, first via WinRM, and if not successful, via DCOM.
   This function needs to be executed as a user with local admin rights on the target computer(s).
 
.PARAMETER ComputerName
The Server that you're connecting to.
This can be the name of a computer, a SMO object, an IP address or a SQL Instance.
 
.PARAMETER Credential
Credential object used to connect to the Computer as a different user
 
.PARAMETER EnableException
        By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
        This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
        Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
.NOTES
Tags: CIM
Author: Klaas Vandenberghe ( @PowerDBAKlaas )
 
dbatools PowerShell module (https://dbatools.io)
Copyright (C) 2016 Chrissy LeMaire
License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
.LINK
 https://dbatools.io/Get-DbaPageFileSetting
 
.EXAMPLE
Get-DbaPageFileSetting -ComputerName ServerA,ServerB
 
Returns a custom object displaying ComputerName, AutoPageFile, FileName, Status, LastModified, LastAccessed, AllocatedBaseSize, InitialSize, MaximumSize, PeakUsage, CurrentUsage for ServerA and ServerB
 
.EXAMPLE
'ServerA' | Get-DbaPageFileSetting
 
Returns a custom object displaying ComputerName, AutoPageFile, FileName, Status, LastModified, LastAccessed, AllocatedBaseSize, InitialSize, MaximumSize, PeakUsage, CurrentUsage for ServerA
 
#>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false, Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [Alias("cn", "host", "ServerInstance", "Server", "SqlServer")]
        [object]$ComputerName,
        [PSCredential] $Credential,
        [switch][Alias('Silent')]$EnableException
    )
    PROCESS {
        foreach ( $Computer in $ComputerName ) {
            $reply = Resolve-DbaNetworkName -ComputerName $Computer -Credential $Credential -ErrorAction silentlycontinue

            if ( !$reply.FullComputerName ) {
                # we can reach $computer
                Write-Message -Level Warning -Message "$Computer is not available."
                continue
            }

            $computer = $reply.FullComputerName
            Write-Message -Level Verbose -Message "Getting computer information from $Computer via CIM (WSMan)"
            $CompSys = Get-CimInstance -ComputerName $Computer -Query "SELECT * FROM win32_computersystem" -ErrorAction SilentlyContinue

            if ( $CompSys ) {
                # we have computersystem class via WSMan
                Write-Message -Level Verbose -Message "Successfully retrieved ComputerSystem information on $Computer via CIM (WSMan)"
                if ( $CompSys.PSobject.Properties.Name -contains "automaticmanagedpagefile" ) {
                    # pagefile exists on $computer
                    Write-Message -Level Verbose -Message "Successfully retrieved PageFile information on $Computer via CIM (WSMan)"
                    if ( $CompSys.automaticmanagedpagefile -eq $False ) {
                        # pagefile is not automatically managed, so get settings via WSMan
                        $PF = Get-CimInstance -ComputerName $Computer -Query "SELECT * FROM win32_pagefile" # deprecated !
                        $PFU = Get-CimInstance -ComputerName $Computer -Query "SELECT * FROM win32_pagefileUsage"
                        $PFS = Get-CimInstance -ComputerName $Computer -Query "SELECT * FROM win32_pagefileSetting"
                    }
                }
                else {
                    # pagefile does not exist on $computer, warn and try next computer
                    Write-Message -Level Verbose -Message "$computer Operating System too old. No Pagefile information available."
                    continue
                }
            }
            else {
                # we do not get computersystem class via WSMan, try via DCom
                Write-Message -Level Verbose -Message "No WSMan connection to $Computer"
                Write-Message -Level Verbose -Message "Getting computer information from $Computer via CIM (DCOM)"

                $sessionoption = New-CimSessionOption -Protocol DCOM
                $CIMsession = New-CimSession -ComputerName $Computer -SessionOption $sessionoption -Credential $Credential -ErrorAction SilentlyContinue
                $CompSys = Get-CimInstance -CimSession $CIMsession -Query "SELECT * FROM win32_computersystem" -ErrorVariable $MyErr -ErrorAction SilentlyContinue

                if ( $CompSys ) {
                    # we have computersystem class via DCom
                    Write-Message -Level Verbose -Message "Successfully retrieved ComputerSystem information on $Computer via CIM (DCOM)"
                    if ( $CompSys.PSobject.Properties.Name -contains "automaticmanagedpagefile" ) {
                        # pagefile exists on $computer
                        Write-Message -Level Verbose -Message "Successfully retrieved PageFile information on $Computer via CIM (DCOM)"
                        if ( $CompSys.automaticmanagedpagefile -eq $False ) {
                            # pagefile is not automatically managed, so get settings via DCom CimSession
                            $PF = Get-CimInstance -CimSession $CIMsession -Query "SELECT * FROM win32_pagefile" # deprecated !
                            $PFU = Get-CimInstance -CimSession $CIMsession -Query "SELECT * FROM win32_pagefileUsage"
                            $PFS = Get-CimInstance -CimSession $CIMsession -Query "SELECT * FROM win32_pagefileSetting"
                        }
                    }
                    else {
                        # pagefile does not exist on $computer, warn and try next computer
                        Write-Message -Level Warning -Message "$computer Operating System too old. No Pagefile information available."
                        continue
                    }
                }
                else {
                    # we don't get computersystem, not wia WSMan nor via DCom, warn and try next computer
                    Write-Message -Level Warning -Message "No WSMan nor DCom connection to $Computer. If you're not local admin on $Computer, you need to run this command as a different user."
                    continue
                }
            }
            if ( $CompSys.automaticmanagedpagefile -eq $False ) {
                # pagefile is not automatic managed, so return settings
                [PSCustomObject]@{
                    ComputerName      = $Computer
                    AutoPageFile      = $CompSys.automaticmanagedpagefile
                    FileName          = $PF.name # deprecated !
                    Status            = $PF.status # deprecated !
                    LastModified      = $PF.LastModified
                    LastAccessed      = $PF.LastAccessed
                    AllocatedBaseSize = $PFU.AllocatedBaseSize # in MB, between Initial and Maximum Size
                    InitialSize       = $PFS.InitialSize # in MB
                    MaximumSize       = $PFS.MaximumSize # in MB
                    PeakUsage         = $PFU.peakusage # in MB
                    CurrentUsage      = $PFU.currentusage # in MB
                }
            }
            else {
                # pagefile is automatic managed, so there are no settings
                [PSCustomObject]@{
                    ComputerName      = $Computer
                    AutoPageFile      = $CompSys.automaticmanagedpagefile
                    FileName          = $null
                    Status            = $null
                    LastModified      = $null
                    LastAccessed      = $null
                    AllocatedBaseSize = $null
                    InitialSize       = $null
                    MaximumSize       = $null
                    PeakUsage         = $null
                    CurrentUsage      = $null
                } | Select-DefaultView -Property ComputerName, AutoPageFile
            }
            if ( [void]$CIMsession.TestConnection() ) {
                Remove-CimSession $CIMsession
            }
        }
    }
}