Save-LenovoBIOSSetting

 


[CmdletBinding()]
Param ()

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

# Start here

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

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

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

            [Parameter(Mandatory=$false)]
            [Alias('Password')]
                [string]$Credential,

            [Parameter(Mandatory=$false)]
            [ValidateSet('ascii','scancode')]
            [Alias('Encoding')]
                [string]$PasswordEncoding = 'ascii',

            [Parameter(Mandatory=$false)]
            [ValidateSet('us','fr','gr')]
                [string]$KeyboardLanguage = 'us',

            [Parameter(Mandatory=$false)]
                [switch]$Commit = $false
        )

        Begin
            {
                if($Credential)
                    {
                        if($PasswordEncoding -eq 'ascii')
                            {
                                [string]$AuthenticationBits = $Credential + ',' + $PasswordEncoding + ',' + $KeyboardLanguage
                            }
                        else
                            {
                                [string]$AuthenticationBits = $Credential + ',' + $PasswordEncoding
                            }

                        Write-Host "AuthenticationBits [$AuthenticationBits]"
                    }
            }

        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-Host "ERROR: NO DNS RECORD FOR [$Computer]: $_" -ForegroundColor Red
                                                Continue
                                            }

                                        default
                                            {
                                                Write-Host "ERROR: AN UNKNOWN ERROR OCCURRED TRYING TO CONNECT TO MACHINE [$Computer]: $_" -ForegroundColor Red
                                                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-Host "WARNING: Unable to connect to machine [$Computer] because it is offline: $_" -ForegroundColor Gray
                                                        Continue
                                                    }

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

                        if($PSCmdlet.ShouldProcess("Saving BIOS Settings on [$Computer]"))
                            {
                                if($Commit -eq $True)
                                    {
                                        $OriginalEAP = $ErrorActionPreference
                                        $ErrorActionPreference = 'Stop'

                                        Try
                                            {
                                                if($Credential) { $SaveResult = (gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi -ComputerName $Computer -ErrorAction Stop).SaveBiosSettings($AuthenticationBits) }
                                                else { $SaveResult = (gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi -ComputerName $Computer -ErrorAction Stop).SaveBiosSettings() }
                                            }
                                        Catch { throw "ERROR 135: FAILED TO SAVE BIOS SETTINGS ON [$Computer]: $_" }

                                        $ErrorActionPreference = $OriginalEAP

                                        Switch($SaveResult.return)
                                            {
                                                'Success' { Write-Output "$($SaveResult.Return)fully saved BIOS settings" }
                                                default { throw "ERROR 140: FAILED TO SAVE BIOS SETTINGS ON [$Computer]: [$($SaveResult.Return)]: $SaveResult" }
                                            }
                                    }
                                Else { Write-Output "WARNING: Commit is false [$Commit]; BIOS Changes NOT saved!" }
                            }
                    }
            }

        End { }
    }
#endregion Function Save-LenovoBIOSSetting

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

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

#Save-LenovoBIOSSetting

#Save-LenovoBIOSSetting -Commit

#Save-LenovoBIOSSetting -Setting 'WakeOnLan' -Value 'ACOnly' -Commit -Credential Passw0rd -PasswordEncoding ascii -KeyboardLanguage us

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