commit b1a79790586dd98b94cc79e6701b84a12439b110 Author: jeirmeister Date: Mon Dec 9 18:05:41 2024 -0800 Add Manage-BiosSettings.ps1 diff --git a/Manage-BiosSettings.ps1 b/Manage-BiosSettings.ps1 new file mode 100644 index 0000000..c3fcec9 --- /dev/null +++ b/Manage-BiosSettings.ps1 @@ -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: $_" + } + } + } +} \ No newline at end of file