Get-LenovoBIOSSetting

 



##*=============================================
##* VARIABLE DECLARATION
##*=============================================
#region VariableDeclaration

# Start here

#endregion VariableDeclaration
##*=============================================
##* END VARIABLE DECLARATION
##*=============================================

##*=============================================
##* FUNCTION LISTINGS
##*=============================================
#region FunctionListings

#region Function Get-LenovoBIOSSetting
Function Get-LenovoBIOSSetting
    {
        [cmdletbinding(SupportsShouldProcess=$True)]
        Param
            (
                [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName)]
                    [string[]]$ComputerName = $env:computername,

                [Parameter(Mandatory=$false)]
                    [string[]]$Setting
            )

        Begin { [System.Collections.ArrayList]$tmpBIOSSetting = @() }

        Process
            {
                foreach($Computer in $ComputerName)
                    {
                        Remove-Variable DNSLookupError,PingResult -ErrorAction SilentlyContinue

                        if(!(Resolve-DnsName -Name $Computer -ErrorAction SilentlyContinue -ErrorVariable DNSLookupError))
                            {
                                Switch -Wildcard ($DNSLookupError)
                                    {
                                        '*DNS name does not exist'
                                            {
                                                Write-Error "NO DNS RECORD FOR [$Computer]: $_"
                                                Continue
                                            }

                                        default
                                            {
                                                Write-Error "AN UNKNOWN ERROR OCCURRED TRYING TO CONNECT TO MACHINE [$Computer]: $_"
                                                Continue
                                            }
                                    }
                                continue
                            }
                        else
                            {
                                $PingResult = (New-Object System.Net.NetworkInformation.Ping).Send($Computer,1000).Status.ToString()
                                if($PingResult -ne 'Success')
                                    {
                                        Switch($PingResult)
                                            {
                                                'Success'
                                                    {
                                                        # Machine is online - good
                                                    }

                                                'TimedOut'
                                                    {
                                                        Write-Warning "Unable to connect to machine [$Computer] because it is offline: $_"
                                                        Continue
                                                    }

                                                default
                                                    {
                                                        Write-Error "AN UNKNOWN ERROR OCCURRED TRYING TO CONNECT TO MACHINE [$Computer]: $_"
                                                        Continue
                                                    }
                                            }
                                        continue
                                    }
                            }

                        if($Setting)
                            {
                                Foreach($Item in $Setting)
                                    {
                                        Try
                                            {
                                                $arrCurrentBIOSSetting = gwmi -class Lenovo_BiosSetting -namespace root\wmi -Filter "CurrentSetting like '$Item,%'" -ComputerName $Computer -ErrorAction Stop | ? { $_.CurrentSetting -ne "" } | Select -ExpandProperty CurrentSetting
                                                Foreach($CurrentBIOSSetting in $arrCurrentBIOSSetting)
                                                    {
                                                        if($CurrentBIOSSetting -like '*Status:ShowOnly*')
                                                            {
                                                                [bool]$ShowOnly = $true
                                                                # Strip Status:ShowOnly
                                                                $CurrentBIOSSetting = $CurrentBIOSSetting.Replace('Status:ShowOnly','')
                                                            }
                                                        else
                                                            {
                                                                [bool]$ShowOnly = $false
                                                            }

                                                        [string]$CurrentItem = $CurrentBIOSSetting.SubString(0,$($CurrentBIOSSetting.IndexOf(',')))

                                                        if($CurrentBIOSSetting.IndexOf(';') -gt 0)
                                                            {
                                                                [string]$CurrentValue = $CurrentBIOSSetting.SubString($($CurrentBIOSSetting.IndexOf(',')+1),$CurrentBIOSSetting.IndexOf(';')-$($CurrentBIOSSetting.IndexOf(',')+1))
                                                            }
                                                        else
                                                            {
                                                                [string]$CurrentValue = $CurrentBIOSSetting.SubString($($CurrentBIOSSetting.IndexOf(',')+1))
                                                            }

                                                        if($CurrentBIOSSetting.IndexOf(';') -gt 0)
                                                            {
                                                                [string]$ValidValues = $CurrentBIOSSetting.SubString($($CurrentBIOSSetting.IndexOf(';')+1))
                                                                [string]$ValidValues = $ValidValues.Replace('[','').Replace(']','').Replace('Optional:','').Replace('Excluded from boot order:','')
                                                            }
                                                        Else
                                                            {
                                                                # We //could// test to confirm the class exists but that takes a little while to process
                                                                #if(Get-WmiObject -Namespace 'root\wmi' -List | ? { $_.Name -eq "Lenovo_GetBiosSelections" }) {  }

                                                                # But I think it's just faster to rely on Try/Catch
                                                                Try
                                                                    {
                                                                        [string]$ValidValues = (Get-WmiObject -Class Lenovo_GetBiosSelections -Namespace root\wmi -ComputerName $Computer -ErrorAction Stop).GetBiosSelections($CurrentItem) | select -ExpandProperty Selections
                                                                    }
                                                                catch
                                                                    {
                                                                        [string]$ValidValues = 'N/A'
                                                                    }
                                                            }

                                                        if($ShowOnly -eq $true)
                                                            {
                                                                # This means we cannot change it via script 😦
                                                                [string]$ValidValues = 'ShowOnly: ' + $ValidValues
                                                            }

                                                        $tmpBIOSSetting += [pscustomobject]@{ComputerName=$Computer;Setting=$CurrentItem;CurrentValue=$CurrentValue;ValidValues=$ValidValues;}

                                                        Remove-Variable CurrentBIOSSetting,ShowOnly,CurrentItem,CurrentValue,ValidValues -ErrorAction SilentlyContinue -WhatIf:$false
                                                    }
                                                Remove-Variable arrCurrentBIOSSetting -ErrorAction SilentlyContinue -WhatIf:$false
                                            }
                                        Catch
                                            {
                                                Exit-Script -ExitCode 100 -MSG "UNABLE TO QUERY THE BIOS VIA CLASS [Lenovo_BiosSeting] ON [$Computer] - POSSIBLE INVALID SETTING SPECIFIED [$Item][$CurrentItem]: $_"
                                            }
                                    }
                            }
                        Else
                            {
                                Try
                                    {
                                        $arrCurrentBIOSSetting = gwmi -class Lenovo_BiosSetting -namespace root\wmi -ComputerName $Computer -ErrorAction Stop | ? { $_.CurrentSetting -ne "" } | Select -ExpandProperty CurrentSetting
                                        Foreach($CurrentBIOSSetting in $arrCurrentBIOSSetting)
                                            {
                                                if($CurrentBIOSSetting -like '*Status:ShowOnly*')
                                                    {
                                                        [bool]$ShowOnly = $true
                                                        # Strip Status:ShowOnly
                                                        $CurrentBIOSSetting = $CurrentBIOSSetting.Replace('Status:ShowOnly','')
                                                    }
                                                else
                                                    {
                                                        [bool]$ShowOnly = $false
                                                    }

                                                [string]$CurrentItem = $CurrentBIOSSetting.SubString(0,$($CurrentBIOSSetting.IndexOf(',')))

                                                if($CurrentBIOSSetting.IndexOf(';') -gt 0)
                                                    {
                                                        [string]$CurrentValue = $CurrentBIOSSetting.SubString($($CurrentBIOSSetting.IndexOf(',')+1),$CurrentBIOSSetting.IndexOf(';')-$($CurrentBIOSSetting.IndexOf(',')+1))
                                                    }
                                                else
                                                    {
                                                        [string]$CurrentValue = $CurrentBIOSSetting.SubString($($CurrentBIOSSetting.IndexOf(',')+1))
                                                    }

                                                if($CurrentBIOSSetting.IndexOf(';') -gt 0)
                                                    {
                                                        [string]$ValidValues = $CurrentBIOSSetting.SubString($($CurrentBIOSSetting.IndexOf(';')+1))
                                                        [string]$ValidValues = $ValidValues.Replace('[','').Replace(']','').Replace('Optional:','').Replace('Excluded from boot order:','')
                                                    }
                                                Else
                                                    {
                                                        # We //could// test to confirm the class exists but that takes a little while to process
                                                        #if(Get-WmiObject -Namespace 'root\wmi' -List | ? { $_.Name -eq "Lenovo_GetBiosSelections" }) {  }

                                                        # But I think it's just faster to rely on Try/Catch
                                                        Try
                                                            {
                                                                [string]$ValidValues = (gwmi –class Lenovo_GetBiosSelections –namespace root\wmi -ComputerName $Computer -ErrorAction Stop).GetBiosSelections($CurrentItem) | select -ExpandProperty Selections
                                                            }
                                                        catch
                                                            {
                                                                [string]$ValidValues = 'N/A'
                                                            }
                                                    }

                                                if($ShowOnly -eq $true)
                                                    {
                                                        # This means we cannot change it via script 😦
                                                        [string]$ValidValues = 'ShowOnly: ' + $ValidValues
                                                    }

                                                $tmpBIOSSetting += [pscustomobject]@{ComputerName=$Computer;Setting=$CurrentItem;CurrentValue=$CurrentValue;ValidValues=$ValidValues;}

                                                Remove-Variable CurrentBIOSSetting,ShowOnly,CurrentItem,CurrentValue,ValidValues -ErrorAction SilentlyContinue -WhatIf:$false
                                            }
                                        Remove-Variable arrCurrentBIOSSetting -ErrorAction SilentlyContinue -WhatIf:$false
                                    }
                                Catch
                                    {
                                        Exit-Script -ExitCode 105 -MSG "UNABLE TO QUERY THE BIOS VIA CLASS [Lenovo_BiosSeting] ON [$Computer]: $_"
                                    }
                            }
                    }
            }

        End
            {
                $BIOSSetting = $tmpBIOSSetting | Sort-Object -Property ComputerName,Setting
                return $BIOSSetting
            }
    }
#endregion Function Get-LenovoBIOSSetting

#endregion FunctionListings
##*=============================================
##* END FUNCTION LISTINGS
##*=============================================

##*=============================================
##* SCRIPT BODY
##*=============================================
#region ScriptBody

#Get-LenovoBIOSSetting

#endregion ScriptBody
##*=============================================
##* END SCRIPT BODY
##*=============================================
Advertisements