-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathGet-LenovoBIOSSettings.ps1
142 lines (139 loc) · 6.57 KB
/
Get-LenovoBIOSSettings.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
function Get-LenovoBIOSSettings {
<#
.SYNOPSIS
Gets current BIOS settings from Lenovo computer(s)
.DESCRIPTION
This functions allows you to retrieve all the BIOS settings from a Lenovo computer, including a switch to
return the 'options' for each setting. Note that retrieving the options makes this take longer
because we have to invoke a WMI method per setting.
.PARAMETER ComputerName
Optionally provide the computer you want the setting for
.PARAMETER WithOptions
Optionally performs the GetBiosSelections for each setting to return the possible options.
.PARAMETER Credential
Optionally provides credentials to perform the WMI queries and methods with
.EXAMPLE
C:\PS> Get-LenovoBiosSettings -ComputerName MAS86109 -WithOptions
Returns the current BIOS settings for MAS83109 including what options each setting has
.INPUTS
[Alias('Computer', 'HostName', 'ServerName', 'IPAddress')]
$ComputerName
.OUTPUTS
[pscustomobject[]]
.NOTES
May not work if Lenovo decides to change the format of the data stored in WMI
#>
[OutputType([pscustomobject[]])]
param
(
[parameter(Mandatory = $false, ValueFromPipelineByPropertyName)]
[Alias('Computer', 'HostName', 'ServerName', 'IPAddress')]
[string[]]$ComputerName,
[switch]$WithOptions,
[parameter(Mandatory = $false)]
[pscredential]$Credential
)
begin {
$CurrentSetting = [System.Collections.Generic.List[object]]::new()
if (-not $PSBoundParameters.ContainsKey('ComputerName')) {
$ComputerName = $env:COMPUTERNAME
}
}
process {
foreach ($Computer in $ComputerName) {
#region gather BIOS settings from 'Lenovo_BiosSetting' WMI class, and check for permissions
$getWmiObjectSplat = @{
ComputerName = $Computer
Namespace = 'root\wmi'
Class = 'Lenovo_BiosSetting'
ErrorAction = 'Stop'
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$getWmiObjectSplat.Credential = $Credential
}
if ($PSBoundParameters.ContainsKey('ComputerName')) {
$getWmiObjectSplat.ComputerName = $Computer
}
try {
$Settings = Get-WmiObject @getWmiObjectSplat | Select-Object -ExpandProperty CurrentSetting | Where-Object { $_ }
$Accessible = $true
}
catch [System.UnauthorizedAccessException] {
Write-Error -Message "Access denied to $Computer" -Category AuthenticationError -Exception $_.Exception
$Accessible = $false
}
catch [System.Runtime.InteropServices.COMException] {
if ($_.Exception.ErrorCode -eq 0x800706BA) {
# This is instead of the "RPC Server Unavailable" error
Write-Error -Message "The RPC Server Unavailable for $Computer. The machine is likely inaccessible over the network" -Category ConnectionError -Exception $_.Exception
$Accessible = $false
}
else {
$_
$Accessible = $false
}
}
#endregion gather BIOS settings from 'Lenovo_BiosSetting' WMI class, and check for permissions
if ($Accessible) {
#region test for the existance of 'Lenovo_GetBiosSelections' WMI class to determine if we can query for available settings
if ($WithOptions) {
$getWmiObjectSplat = @{
Namespace = 'root\wmi'
Class = 'Lenovo_GetBiosSelections'
ErrorAction = 'Stop'
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$getWmiObjectSplat.Credential = $Credential
}
if ($PSBoundParameters.ContainsKey('ComputerName')) {
$getWmiObjectSplat.ComputerName = $Computer
}
try {
$null = Get-WmiObject @getWmiObjectSplat
$CanGetOptions = $true
}
catch {
if ($_.CategoryInfo.Category -eq 'InvalidType') {
Write-Warning "Unable to query for BIOS Setting Options because 'Lenovo_GetBiosSelections' does not exist on $Computer"
$CanGetOptions = $false
}
}
}
#endregion test for the existance of 'Lenovo_GetBiosSelections' WMI class to determine if we can query for available settings
#region format settings appropriately and check for options if requested and available
foreach ($Setting in $Settings) {
$Setting = (($Setting -split ';', 2)[0] -split ',', 2)
$SettingName = $Setting[0]
$SettingValue = $Setting[1]
$HashTable = [ordered]@{
ComputerName = $Computer
SettingName = $SettingName
SettingValue = $SettingValue
}
if ($WithOptions -and $CanGetOptions) {
$getWmiObjectSplat = @{
Namespace = 'root\wmi'
Class = 'Lenovo_GetBiosSelections'
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$getWmiObjectSplat.Credential = $Credential
}
if ($PSBoundParameters.ContainsKey('ComputerName')) {
$getWmiObjectSplat.ComputerName = $Computer
}
$SettingOptions = ((Get-WmiObject @getWmiObjectSplat).GetBiosSelections($SettingName)) | Select-Object -ExpandProperty Selections
$HashTable.Add('SettingOptions', $SettingOptions)
}
elseif ($WithOptions) {
$HashTable.Add('SettingOptions', $null)
}
$CurrentSetting.Add([pscustomobject]$HashTable)
}
#endregion format settings appropriately and check for options if requested and available
}
}
}
end {
return $CurrentSetting
}
}