@@ -3981,7 +3981,25 @@ def get_windows_nic_attribute(
39813981 :param target: which nic attribute you want to get.
39823982
39833983 """
3984- cmd = 'wmic %s where %s="%s" get %s' % (global_switch , key , value , target )
3984+ if utils_misc .is_wmic_available (session ):
3985+ cmd = 'wmic %s where %s="%s" get %s' % (global_switch , key , value , target )
3986+ else :
3987+ value_ps = value
3988+ if value and key and key .lower () in {"macaddress" , "mac" }:
3989+ value_ps = value .replace (":" , "-" ).upper ()
3990+ if global_switch == "nic" :
3991+ wmi_class = "Win32_NetworkAdapter"
3992+ elif global_switch == "nicconfig" :
3993+ wmi_class = "Win32_NetworkAdapterConfiguration"
3994+ else :
3995+ raise exceptions .TestError (
3996+ "Unsupported global_switch '%s' for PowerShell fallback" % global_switch
3997+ )
3998+ cmd = (
3999+ "PowerShell -NoProfile -Command "
4000+ '"(Get-CimInstance {wmi_class} | '
4001+ "Where-Object {{$_.{key} -eq '{value}'}}).{target}\" "
4002+ ).format (wmi_class = wmi_class , key = key , value = value_ps , target = target )
39854003 status , out = session .cmd_status_output (cmd , timeout = timeout )
39864004 if status != 0 :
39874005 err_msg = "Execute guest shell command('%s') " "failed with error: '%s'" % (
@@ -3990,8 +4008,9 @@ def get_windows_nic_attribute(
39904008 )
39914009 raise exceptions .TestError (err_msg )
39924010 lines = [l .strip () for l in out .splitlines () if l .strip ()]
3993- # First line is header, return second line
3994- return lines [1 ]
4011+ # WMIC output has a header (value is in lines[1])
4012+ # PowerShell output has no header, so return out.strip()
4013+ return lines [1 ] if len (lines ) > 1 else out .strip ()
39954014
39964015
39974016def set_win_guest_nic_status (session , connection_id , status , timeout = 240 ):
0 commit comments