Trying get this simple statement working, and I cant understand why it won't output the correct result.
$asset = "get-wmiobject -class win32_computersystem | select Name"
if ($asset.StartsWith("ME")) {
echo "Asset tag is ok" }
Else
{ echo "Asset tag needs updating" }
For some reason, despite the result from the WMI query being "ME12345" for example, the code outputs "Asset tag needs updating".
Do I need to use an /F
or something to make the IF
statement work with the result from the WMI statement?
Your command is wrapped in double-quotes, so you are passing a string of the command, rather than the actual returned value.
When you change it to a command and the value is returned, it is an object with the property Name
, and it does not contain a method "StartsWith", instead you can use a comparison operator, and below I've gone with regex that matches lines starting with "ME".
You probably want to use CIMInstance
, as it is the new version of WMIObject
.
$asset = Get-CimInstance -Class win32_computersystem | Select Name
if ($asset.Name -match "^ME") {
Write-Output "Asset tag is ok"
}
Else {
Write-Output "Asset tag needs updating"
}