Reputation: 15
Basically, I'm trying to read a BIOS setting and then with the if
condition, if it matches to execute another command.
It works fine with other variables, it is just with this one.
#This Script will check for VT and VTD on Lenovo machines and enable them
#if the current value is disabled.
#Run Set-ExecutionPolicy Unrestricted first.
#Run As administrator
#Check the current values
$VT= gwmi -class Lenovo_BiosSetting -namespace root\wmi |
Where-Object {$_.CurrentSetting.split(",",[StringSplitOptions]::RemoveEmptyEntries) -eq "VirtualizationTechnology"} |
Format-List CurrentSetting
$VTD= gwmi -class Lenovo_BiosSetting -namespace root\wmi |
Where-Object {$_.CurrentSetting.split(",",[StringSplitOptions]::RemoveEmptyEntries) -eq "VTdFeature"} |
Format-List CurrentSetting
#Modify the values
#$EnabledVT= (gwmi -class Lenovo_SetBiosSetting -namespace root\wmi).SetBiosSetting("VirtualizationTechnology,Enable")
#$EnableVTD= (gwmi -class Lenovo_SetBiosSetting -namespace root\wmi).SetBiosSetting("VTdFeature,Enable")
#$SaveBios=(gwmi -class Lenovo_SaveBiosSettings -namespace root\wmi).SaveBiosSettings()
#Check if VT is disabled and enable it if it is.
Echo "Virtualization current settings are below"
Write-output $VT
IF ($VT -like "*Disable*") {
"this is not working"
} else {
"Setting is already set to enabled, no changes made."
}
#Check if VTD is disabled and enable it if it is.
Write-output $VTD
IF ($VTD -like "*,Disable") {
this is not working
} else {
"Setting is already set to enabled, no changes made."
}
#Save bios settings.
$SaveBios
Write-host "Check completed, Please restart computer for changes to take effect if any changes were made. "
I have tried different conditions to match exactly or the like or the match but nothing seems to be able to find anything.
Upvotes: 1
Views: 172
Reputation: 174920
Instead of Format-List
, use Select-Object -ExpandProperty
:
$VT = gwmi -class Lenovo_BiosSetting -namespace root\wmi | Where-Object {$_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -eq “VirtualizationTechnology”} | Select-Object -ExpandProperty CurrentSetting
$VTD = gwmi -class Lenovo_BiosSetting -namespace root\wmi | Where-Object {$_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -eq “VTdFeature”} | Select-Object -ExpandProperty CurrentSetting
Upvotes: 3
Reputation: 36332
So, here's the issue that you are having. You are piping the output of your command to Format-List
, and storing that info in your variable. Don't do that. The Format-*
cmdlets should be used to format things for output to the console, not for storing data for use later. Instead remove that part, and then reference the CurrentSetting
property of the object.
$VT= gwmi -class Lenovo_BiosSetting -namespace root\wmi | Where-Object {$_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -eq “VirtualizationTechnology”}
$VTD= gwmi -class Lenovo_BiosSetting -namespace root\wmi | Where-Object {$_.CurrentSetting.split(“,”,[StringSplitOptions]::RemoveEmptyEntries) -eq “VTdFeature”}
IF ($VT.CurrentSetting -like "*Disable*") {"this is now working"}
else {"Setting is already set to enabled, no changes made."}
This will function as desired.
Upvotes: 4