Add Manage-BiosSettings.ps1

This commit is contained in:
jeirmeister 2024-12-09 18:05:41 -08:00
commit b1a7979058

64
Manage-BiosSettings.ps1 Normal file
View File

@ -0,0 +1,64 @@
function Manage-BiosSettings {
param (
[Parameter(Mandatory=$true)]
[ValidateSet('export', 'import')]
[string]$Action,
[Parameter()]
[string]$FilePath = "bios_settings.txt"
)
# Ensure running as Administrator
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
throw "This script must be run as Administrator"
}
switch ($Action) {
'export' {
try {
$biosSettings = gwmi -class Lenovo_BiosSetting -namespace root\wmi |
Where-Object { $_.CurrentSetting -ne "" } |
ForEach-Object { $_.CurrentSetting.replace(",", " = ") }
$biosSettings | Out-File -FilePath $FilePath -Encoding UTF8
Write-Host "BIOS settings exported to $FilePath"
}
catch {
Write-Host "Error exporting BIOS settings: $_"
}
}
'import' {
try {
if (-not (Test-Path $FilePath)) {
throw "Settings file not found: $FilePath"
}
$biosSettings = Get-Content $FilePath | Where-Object { $_ -match "=" }
foreach ($setting in $biosSettings) {
$name, $value = $setting.Split("=").Trim()
$result = (Get-WmiObject -class Lenovo_SetBiosSetting -namespace root\wmi).SetBiosSetting("$name,$value")
if ($result.return -eq "Success") {
Write-Host "Set $name to $value"
}
else {
Write-Host "Failed to set $name to $value"
}
}
$save = (Get-WmiObject -class Lenovo_SaveBiosSettings -namespace root\wmi).SaveBiosSettings()
if ($save.return -eq "Success") {
Write-Host "All BIOS settings saved successfully"
}
else {
Write-Host "Failed to save BIOS settings"
}
}
catch {
Write-Host "Error importing BIOS settings: $_"
}
}
}
}