Page 1 of 1

Error with ExecQuery method

Posted: Wed Aug 31, 2011 7:51 am
by fr3288
Hi:
I'm trying to convert the following VBScript to PowerShell to avoid calling an external script from PowerShell:

Const wbemFlagReturnImmediately = 16Const wbemFlagForwardOnly = 32

strComputer = "."

lFlags = wbemFlagReturnImmediately + wbemFlagForwardOnlystrService = "winmgmts:{impersonationlevel=impersonate}//"
strNamespace = "/root/HP/InstrumentedBIOS"
strQuery = "select * from HP_BIOSSetting"

Set objWMIService = GetObject(strService & strComputer & strNamespace)Set colItems = objWMIService.ExecQuery(strQuery,,lFlags)

For Each objItem In colItems Value = objItem.Name If InStr(1,Value,"Tag",vbTextCompare) Then WScript.Echo Value & " = " & objItem.Value End IfNext


This is what I have been able to do but I get an error calling the ExecQuery method:


Set-Variable -Name -wbemFlagReturnImmediately -Value 16 -Option ConstantSet-Variable -Name -wbemFlagForwardOnly -Value 32 -Option Constant

$strComputer = "."

$lFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly$strService = "winmgmts:{impersonationlevel=impersonate}//"
$strNameSpace = "/root/HP/InstrumentedBIOS"
$strQuery = "select * from HP_BIOSSetting"

[Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")$objWMIService = [Microsoft.VisualBasic.Interaction]::GetObject($strService + $strComputer + $strNameSpace)
$colItems = $objWMIService.ExecQuery($strQuery,$Null,$lFlags)

foreach($objItem in $colItems){ $Value = $objItem.Name if($Value -like "*Tag*"){$Value + " = " + $objItem.Value}}


The error is:


GAC Version Location--- ------- --------True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.VisualBasic8.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualBasic.dllERROR: Exception calling "ExecQuery" with "3" argument(s): "Invalid parameter "ERROR: At C:Usersfr3288DocumentsSAPIENScriptsUntitled31.ps1:14 char:37ERROR: + $colItems = $objWMIService.ExecQuery <<<< ($strQuery,$Null,$lFlags)ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationExceptionERROR: + FullyQualifiedErrorId : ComMethodTargetInvocationAny ideas?

Thanks,

FR

Error with ExecQuery method

Posted: Wed Aug 31, 2011 7:51 am
by fr3288
Hi:
I'm trying to convert the following VBScript to PowerShell to avoid calling an external script from PowerShell:

Const wbemFlagReturnImmediately = 16Const wbemFlagForwardOnly = 32

strComputer = "."

lFlags = wbemFlagReturnImmediately + wbemFlagForwardOnlystrService = "winmgmts:{impersonationlevel=impersonate}//"
strNamespace = "/root/HP/InstrumentedBIOS"
strQuery = "select * from HP_BIOSSetting"

Set objWMIService = GetObject(strService & strComputer & strNamespace)Set colItems = objWMIService.ExecQuery(strQuery,,lFlags)

For Each objItem In colItems Value = objItem.Name If InStr(1,Value,"Tag",vbTextCompare) Then WScript.Echo Value & " = " & objItem.Value End IfNext


This is what I have been able to do but I get an error calling the ExecQuery method:


Set-Variable -Name -wbemFlagReturnImmediately -Value 16 -Option ConstantSet-Variable -Name -wbemFlagForwardOnly -Value 32 -Option Constant

$strComputer = "."

$lFlags = $wbemFlagReturnImmediately + $wbemFlagForwardOnly$strService = "winmgmts:{impersonationlevel=impersonate}//"
$strNameSpace = "/root/HP/InstrumentedBIOS"
$strQuery = "select * from HP_BIOSSetting"

[Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")$objWMIService = [Microsoft.VisualBasic.Interaction]::GetObject($strService + $strComputer + $strNameSpace)
$colItems = $objWMIService.ExecQuery($strQuery,$Null,$lFlags)

foreach($objItem in $colItems){ $Value = $objItem.Name if($Value -like "*Tag*"){$Value + " = " + $objItem.Value}}


The error is:


GAC Version Location--- ------- --------True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.VisualBasic8.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualBasic.dllERROR: Exception calling "ExecQuery" with "3" argument(s): "Invalid parameter "ERROR: At C:Usersfr3288DocumentsSAPIENScriptsUntitled31.ps1:14 char:37ERROR: + $colItems = $objWMIService.ExecQuery <<<< ($strQuery,$Null,$lFlags)ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationExceptionERROR: + FullyQualifiedErrorId : ComMethodTargetInvocationAny ideas?

Thanks,

FR

Error with ExecQuery method

Posted: Wed Aug 31, 2011 8:05 am
by jvierra
YOu don't have to or want to convert VBScruipt to PowerSHell.

Just use POwerShell directly. All of you lines of code can be summarized with one line:Get-WMIObject HP_BIOSSetting -namespace root/HP/InstrumentedBIOS

ORGet-WMIObject HP_BIOSSetting -namespace root/HP/InstrumentedBIOS |Format-Table -auto

Help Get-WMIObject -full

Error with ExecQuery method

Posted: Wed Aug 31, 2011 10:31 am
by jvierra
That will work however if you do this:
Get-WmiObject HP_BIOSSetting -namespace root/HP/InstrumentedBIOS | select name

It will let you see all of the names abailable.
Your method wotsk for the assettag as will:Get-WmiObject HP_BIOSSetting -namespace root/HP/InstrumentedBIOS -filter "name='assettag'"

Whish I guess you now know.
Good LUck,