Error with ExecQuery method
Posted: Wed Aug 31, 2011 7:51 am
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
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