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
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
 
.NOTES
Tags: CIM
Author: Klaas Vandenberghe ( @PowerDBAKlaas )
 
dbatools PowerShell module (https://dbatools.io)
Copyright (C) 2016 Chrissy LeMaire
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
 
.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] [System.Management.Automation.CredentialAttribute()]$Credential
)
PROCESS
{
foreach ( $Computer in $ComputerName )
{
$reply = Resolve-DbaNetworkName -ComputerName $Computer -Credential $Credential -ErrorAction silentlycontinue

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

$computer = $reply.ComputerName
Write-Verbose "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-Verbose "Successfully retrieved ComputerSystem information on $Computer via CIM (WSMan)"
if ( $CompSys.PSobject.Properties.Name -contains "automaticmanagedpagefile" ) # pagefile exists on $computer
{
Write-Verbose "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-Warning "$computer Operating System too old. No Pagefile information available."
continue
}
}
else # we do not get computersystem class via WSMan, try via DCom
{
Write-Verbose "No WSMan connection to $Computer"
Write-Verbose "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-Verbose "Successfully retrieved ComputerSystem information on $Computer via CIM (DCOM)"
if ( $CompSys.PSobject.Properties.Name -contains "automaticmanagedpagefile" ) # pagefile exists on $computer
{
Write-Verbose "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-Warning "$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-Warning "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 ( $CIMsession ) { Remove-CimSession $CIMsession }
}
}
}