我很难让这个WMIC查询正常工作。
@echo off
setlocal
Call :wmic nicconfig where IPEnabled=TRUE and DefaultIPgateway is not null get ipaddress,macaddress,defaultipgateway /format:list
exit /b
:wmic
for /f "delims=" %%A in ('"wmic %*"') do for /f "delims=" %%B in ("%%A") do echo %%B
exit /b我错过了一些简单的东西,但我不知道是什么。我一直在说“和-无效的别名动词。”
提亚
哑光
发布于 2015-03-07 02:38:36
正如Bill_Stewart所指出的,DefaultIPGateway列包含一个数组数据类型,不能用WQL查询。但这并不意味着wmic无法处理复合where子句。您只需将参数用引号括起来给where。
wmic nicconfig where "IPEnabled=TRUE and IPConnectionMetric>0" get ipaddress,macaddress,defaultipgateway /format:list看见?整个IPEnabled=TRUE and IPConnectionMetric>0的事情是一个论点。您将得到一个错误,因为AND发生在wmic期望get、list或call等的地方。
顺便说一句,如果您需要查询字符串,则必须在双引号中使用单引号。与布尔和国家统计局不同,字符串必须被引用。
wmic nicconfig where "Description LIKE '%%NVIDIA%%'" list /format:list还有几个问题要考虑。在一个wmic循环中调用for时,需要引用相同的符号和逗号,或者用插入符号转义。另外,在for /f循环中,不需要围绕整个命令同时使用单引号和双引号。按以下方式更改您的脚本,它应该可以工作:
@echo off
setlocal
call :wmic nicconfig where "IPEnabled=TRUE and IPConnectionMetric>0" get ipaddress^^,macaddress^^,defaultipgateway /format:list
exit /b
:wmic
for /f "delims=" %%A in ('wmic %*') do for /f "delims=" %%B in ("%%A") do echo %%B
exit /b发布于 2015-03-06 22:27:41
WQL不支持查询数组数据类型(请参阅https://msdn.microsoft.com/en-us/library/aa392902.aspx)。在shell脚本(批处理)中实现这一点会很烦人,但在PowerShell中非常简单:
get-wmiobject Win32_NetworkAdapterConfiguration -filter "IPEnabled=TRUE" |
where-object { $_.DefaultIPGateway }发布于 2017-02-14 21:36:17
我在工作中遇到了同样的问题。我的尝试是使用VBS并使IpEnabled成为查询的一部分,并使DefaultIPGateway成为Sub的一部分,这在一定程度上要归功于WMICreator工具是如何格式化示例代码的。
这只应在两种情况下都适用时写出有效值:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=True",,48)
For Each objItem in colItems
DoWork
next
Sub DoWork()
If isNull(objItem.DefaultIPGateway) Then
exit sub
Else
Wscript.Echo "DefaultIPGateway: " & Join(objItem.DefaultIPGateway, ",")
End If
Wscript.Echo "DHCPServer: " & objItem.DHCPServer
Wscript.Echo "DNSDomain: " & objItem.DNSDomain
Wscript.Echo "DNSHostName: " & objItem.DNSHostName
If isNull(objItem.IPAddress) Then
Wscript.Echo "IPAddress: "
Else
Wscript.Echo "IPAddress: " & Join(objItem.IPAddress, ",")
End If
Wscript.Echo "IPConnectionMetric: " & objItem.IPConnectionMetric
Wscript.Echo "IPEnabled: " & objItem.IPEnabled
If isNull(objItem.IPSubnet) Then
Wscript.Echo "IPSubnet: "
Else
Wscript.Echo "IPSubnet: " & Join(objItem.IPSubnet, ",")
End If
end Subhttps://stackoverflow.com/questions/28908430
复制相似问题