Error with ExecQuery method

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 12 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
fr3288
Posts: 23
Last visit: Fri Sep 18, 2020 1:20 pm

Error with ExecQuery method

Post 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
User avatar
fr3288
Posts: 23
Last visit: Fri Sep 18, 2020 1:20 pm

Error with ExecQuery method

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Error with ExecQuery method

Post 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Error with ExecQuery method

Post 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,

This topic is 12 years and 6 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked