Created
June 15, 2016 13:00
-
-
Save KingBain/6666d8d2a3f785c46579ae6ca1ee6f37 to your computer and use it in GitHub Desktop.
Powershell script to configure BIOS Aut-On settings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[switch]$Clear, | |
[string]$Time, | |
[int]$Window, | |
[string[]]$Days | |
) | |
function Get-AutoOn(){ | |
$object = New-Object -TypeName PSObject | |
try { | |
$systemInfo = (gwmi -class win32_ComputerSystem -Namespace "root\cimv2" -ErrorAction stop).Manufacturer.Trim().ToLower() | |
if ($systemInfo -eq "hewlett-packard"){ | |
$enabledDays = New-Object System.Collections.ArrayList | |
$biosSettings = Get-WmiObject HP_BIOSSetting -Namespace "root\HP\InstrumentedBIOS" -ErrorAction stop | |
$powerOnTime = ($biosSettings | Where-Object{$_.name -like "*BIOS Power-On Time*" }).value | |
$powerOnEnabled = $false | |
foreach ($setting in $biosSettings){ | |
foreach ($day in 0..6) { | |
if ($setting.name -like "*$([dayofWeek]$day)*"){ | |
if (-not ($Setting.value -like "*,Enable")){$enabledDays.Add([dayofWeek]$day) | Out-Null} | |
} | |
} | |
} | |
if ($enabledDays.Count -gt 0){ | |
$powerOnEnabled = $true | |
} | |
} | |
if ($systemInfo -eq "dell inc."){ | |
$enabledDays = New-Object System.Collections.ArrayList | |
$biosSettings = gwmi -namespace "root\dcim\sysman" -class DCIM_BIOSEnumeration | |
$powerOnHour = ($biosSettings | Where-Object{$_.AttributeName -like "*Auto On Hour*" }).CurrentValue | |
$powerOnMin = ($biosSettings | Where-Object{$_.AttributeName -like "*Auto On Minute*" }).CurrentValue | |
$powerOnEnabled = $false | |
if ($powerOnMin -and $powerOnHour) { | |
$powerOnHour = "{0:D2}" -f [int]($powerOnHour[0]) | |
$powerOnMin = "{0:D2}" -f [int]($powerOnMin[0]) | |
$powerOnTime = "$($powerOnHour):$($powerOnMin)" | |
} | |
$powerOnEnabled = ($biosSettings | Where-Object{$_.AttributeName -like "Auto On" }).CurrentValue | |
if ($powerOnEnabled[0] -eq 4){$powerOnEnabled = $true} | |
foreach ($setting in $biosSettings){ | |
foreach ($day in 0..6) { | |
if ($setting.AttributeName -like "Auto on $([dayofWeek]$day)" ){ | |
if (($setting.CurrentValue)[0] -eq 1){ | |
$enabledDays.Add([dayofWeek]$day) | Out-Null | |
} | |
} | |
} | |
} | |
} | |
$object | Add-Member -MemberType NoteProperty –Name Enabled -Value $powerOnEnabled | |
$object | Add-Member -MemberType NoteProperty –Name Time -Value ([DateTime]$powerOnTime) | |
$object | Add-Member -MemberType NoteProperty –Name Days -Value ($enabledDays.ToArray()) | |
} | |
catch{ | |
Write-Warning $_.Exception.Message | |
exit 1 | |
} | |
return $object | |
} | |
function Set-AutoOn(){ | |
param([string[]]$days, | |
[Datetime]$time) | |
try { | |
$systemInfo = (gwmi -class win32_ComputerSystem -Namespace "root\cimv2" -ErrorAction stop).Manufacturer.Trim().ToLower() | |
if ($systemInfo -eq "hewlett-packard"){ | |
$results = (gwmi -class hp_biossettinginterface -Namespace "root\hp\instrumentedbios" -ErrorAction stop).SetBIOSSetting("BIOS Power-On Time (hh:mm)",$time.TimeOfDay,"") | |
if($results.Return -ne 0){throw "BIOS returned invalid command"} | |
foreach($dayofWeek in 0..6 ){ | |
$d = ([dayofWeek]$dayofWeek).ToString() | |
if ($days.Contains($d)){ | |
$results = (gwmi -class hp_biossettinginterface -Namespace "root\hp\instrumentedbios" -ErrorAction stop).SetBIOSSetting($d,"Enable","") | |
if($results.Return -ne 0){throw "BIOS returned invalid command"} | |
} | |
else{ | |
$results = (gwmi -class hp_biossettinginterface -Namespace "root\hp\instrumentedbios" -ErrorAction stop).SetBIOSSetting($d,"Disable","") | |
if($results.Return -ne 0){throw "BIOS returned invalid command"} | |
} | |
} | |
} | |
if ($systemInfo -eq "dell inc."){ | |
$dellBIOSSetting = gwmi DCIM_BIOSService -namespace root\dcim\sysman -ErrorAction stop | |
if ($days.Count -eq 0){$dellEnable = 1} | |
else {$dellEnable = 4} | |
$results = $dellBIOSSetting.SetBIOSAttributes($null,$null,"Auto On",$dellEnable) | |
if($results.ReturnValue -ne 0){throw "BIOS returned invalid command"} | |
$results = $dellBIOSSetting.SetBIOSAttributes($null,$null,"Auto On Hour",("{0:D2}" -f $time.Hour)) | |
if($results.ReturnValue -ne 0){throw "BIOS returned invalid command"} | |
$results = $dellBIOSSetting.SetBIOSAttributes($null,$null,"Auto On Minute",("{0:D2}" -f $time.Minute)) | |
if($results.ReturnValue -ne 0){throw "BIOS returned invalid command"} | |
foreach($dayofWeek in 0..6 ){ | |
$d = ([dayofWeek]$dayofWeek).ToString() | |
if ($days.Contains($d)){ | |
$results = $dellBIOSSetting.SetBIOSAttributes($null,$null,"Auto on $d",1) | |
if($results.ReturnValue -ne 0){throw "BIOS returned invalid command"} | |
} | |
else{ | |
$results = $dellBIOSSetting.SetBIOSAttributes($null,$null,"Auto on $d",2) | |
if($results.ReturnValue -ne 0){throw "BIOS returned invalid command"} | |
} | |
} | |
} | |
} | |
catch{ | |
Write-Warning $_.Exception.Message | |
return $false | |
} | |
return $true | |
} | |
if ($Clear){ | |
[datetime]$Time = "00:00" | |
$Days = @() | |
} | |
else { | |
try {[datetime]$Time = $Time} | |
catch{ | |
"Your time is invalid, please use the following format hh:mm" | |
exit 1 | |
} | |
if (-not $Days){ | |
"Your day value is missing, try (""mon,tues,wed,thur,fri,sat,sun""):" | |
exit 1 | |
} | |
if ($Window){ | |
$Time = $Time.AddMinutes((Get-Random -Min 0 -Max $Window)) | |
} | |
$cleanedDays = New-Object System.Collections.ArrayList | |
foreach ($Day in $Days) { | |
try { | |
[dayofWeek]$Day > $null | |
$cleanedDays.Add([dayofWeek]$Day) | Out-Null | |
} | |
catch { | |
"$Day is not a valid week of the day, try (""mon,tues,wed,thur,fri,sat,sun""):" | |
exit 1 | |
} | |
} | |
$Days = $cleanedDays.ToArray() | |
} | |
$currentSettings = Get-AutoOn | |
if ($currentSettings.Time -eq $time -and (diff $Days $currentSettings.Days -PassThru).count -eq 0){ | |
"No changes were needed" | |
exit 0 | |
} | |
$results = Set-AutoOn -Days $Days -time $Time | |
if ($results){ | |
"BIOS has been configured" | |
exit 0 | |
} | |
else { | |
Write-warning "BIOS configuration has failed" | |
exit 1 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment