我已经制作了一个PowerShell脚本来查找与某些BIOS设置匹配的内容然后进行更改。
唯一的问题是,如果我运行一次并重新启动它不会保存设置,虽然我有成功的回报。如果我运行两次,则保存设置。
我认为它与我的变量和foreach语句有关。谁能告诉我我做错了什么?
#param ([string]$lenovopass)
$lenovopass= "password"
#Search BIOS for Virtual or Items starting with V.
$biosvalues= gwmi -class Lenovo_BiosSetting -namespace root\wmi |
Where-Object {$_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -match “Virtual” -or $_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -like "V*”} |
Select-Object -ExpandProperty CurrentSetting
$biossplit= $biosvalues.split(",")[0,1]
#savebiossettings
$SaveBiosPass =(gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi).SaveBiosSettings("$lenovopass,ascii,us") | Out-Null
$SaveBios =(gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi).SaveBiosSettings() | Out-Null
$passcheck= (Get-WmiObject -namespace root\wmi -class Lenovo_BiosPasswordSettings | select -ExpandProperty PasswordState)
Function Lenovonopass{
foreach ($value in $biosvalues)
{If ($value -match "Enable")
{
Write-Host "Found $value"
Write-Host "Current Setting: $value"
Write-Host "No changes made, $value already enabled."
} else {
Write-Host "Found $value"
Write-Host "Current Settings: $value"
$value = $value.split(",")[0,1]
$valuea= $value[0]
$valueb= $value[1]
Write-Host "Changing $value state to Enable"
(gwmi -class Lenovo_SetBiosSetting -namespace root\wmi).SetBiosSetting(“$valuea,Enable”) | Out-Null
$SaveBios
$newvalue = gwmi -class Lenovo_BiosSetting -namespace root\wmi |
Where-Object {$_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -match "$valuea"} |
Select-Object -ExpandProperty CurrentSetting
write-host "New Settings: $newvalue"
}
}
}
Function LenovoPass{
foreach ($value in $biosvalues)
{If ($value -match "Enable")
{
Write-Host "Found $value"
Write-Host "Current Setting: $value"
Write-Host "No changes made, $value already enabled."
} else {
Write-Host "Found $value"
Write-Host "Current Settings: $value"
$value = $value.split(",")[0,1]
$valuea= $value[0]
$valueb= $value[1]
Write-Host "Changing $value state to Enable"
(gwmi -class Lenovo_SetBiosSetting -namespace root\wmi).SetBiosSetting(“$valuea,Enable,$lenovopass,ascii,us") | Out-Null
$SaveBiosPass
$newvalue = gwmi -class Lenovo_BiosSetting -namespace root\wmi |
Where-Object {$_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -match "$valuea"} |
Select-Object -ExpandProperty CurrentSetting
write-host "New Settings: $newvalue"
Write-Host ""
}
}
}
IF ($passcheck -eq "0")
{Lenovonopass
$SaveBios
Write-Host "BIOS Changes Saved"
} Else {
Lenovopass
$SaveBiosPass
Write-Host "BIOS Changes Saved"
}
答案 0 :(得分:0)
看起来你正试图通过调用变量来保存BIOS设置,就好像它是一个函数一样。
$SaveBios
Write-Host "BIOS Changes Saved"
当您为$SaveBios
分配值时,保存设置的唯一时间是在脚本的开头。
因此,通过运行脚本两次,您将在脚本开头保存设置,然后在之前的运行中更改了这些设置。
您可以改为为SaveBios创建一个函数。或者,当您确实要保存设置时,将值指定给$SaveBios
。
IF ($passcheck -eq "0")
{Lenovonopass
$SaveBios =(gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi).SaveBiosSettings() | Out-Null
Write-Host "BIOS Changes Saved"
} Else {
Lenovopass
$SaveBiosPass =(gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi).SaveBiosSettings("$lenovopass,ascii,us") | Out-Null
Write-Host "BIOS Changes Saved"
}