diff --git a/agent/managedInstances/fingerprint/hardwareInfo_windows.go b/agent/managedInstances/fingerprint/hardwareInfo_windows.go index b7bd740d2..6d4739513 100644 --- a/agent/managedInstances/fingerprint/hardwareInfo_windows.go +++ b/agent/managedInstances/fingerprint/hardwareInfo_windows.go @@ -20,10 +20,7 @@ package fingerprint import ( "fmt" - "os/exec" "path/filepath" - "strconv" - "strings" "time" "github.com/aws/amazon-ssm-agent/agent/appconfig" @@ -34,23 +31,12 @@ import ( "golang.org/x/sys/windows/svc/mgr" ) -type WMIInterface string - const ( hardwareID = "uuid" wmiServiceName = "Winmgmt" serviceRetryInterval = 15 // Seconds serviceRetry = 5 - - winVersionCommand = "Get-CimInstance -ClassName Win32_OperatingSystem | Format-List -Property Version" - - win2025MajorVersion = 10 - win2025MinorVersion = 0 - win2025BuildNumber = 26040 - - wmic WMIInterface = "WMIC" - cimInstance WMIInterface = "CIM Instance" ) func waitForService(log log.T, service *mgr.Service) error { @@ -105,162 +91,46 @@ var currentHwHash = func() (map[string]string, error) { log.Debug("WMI Service is ready to be queried....") - wmiInterface, _ := getWMIInterface(log) - - hardwareHash[hardwareID], _ = csproductUuid(log, wmiInterface) - hardwareHash["processor-hash"], _ = processorInfoHash(wmiInterface) - hardwareHash["memory-hash"], _ = memoryInfoHash(wmiInterface) - hardwareHash["bios-hash"], _ = biosInfoHash(wmiInterface) - hardwareHash["system-hash"], _ = systemInfoHash(wmiInterface) + hardwareHash[hardwareID], _ = csproductUuid(log) + hardwareHash["processor-hash"], _ = processorInfoHash() + hardwareHash["memory-hash"], _ = memoryInfoHash() + hardwareHash["bios-hash"], _ = biosInfoHash() + hardwareHash["system-hash"], _ = systemInfoHash() hardwareHash["hostname-info"], _ = hostnameInfo() hardwareHash[ipAddressID], _ = primaryIpInfo() hardwareHash["macaddr-info"], _ = macAddrInfo() - hardwareHash["disk-info"], _ = diskInfoHash(wmiInterface) + hardwareHash["disk-info"], _ = diskInfoHash() return hardwareHash, nil } -// get WMI interface which should be uses to retrieve the hardware info, if any error occur use WMIC by default -func getWMIInterface(logger log.T) (wmiInterface WMIInterface, err error) { - // get Windows version number via CIM Instance, as it works on all Windows versions - if outputBytes, err := exec.Command(appconfig.PowerShellPluginCommandName, winVersionCommand).Output(); err == nil { - outputStr := strings.Replace(strings.Replace(string(outputBytes), "\n", "", -1), "\r", "", -1) //removes any new lines from the output - logger.Debugf("Windows version property command output: %s", outputStr) //expected output format should be: Version : 10.0.26100 - outputStrElements := strings.Split(outputStr, ":") - if len(outputStrElements) != 2 { - logger.Warnf("Unexpected command output format for Windows version property: %s, setting WMIC as WMI interface", outputStr) - return wmic, fmt.Errorf("Error while parsing command output for windows version property. Got %v elements and was expecting 2.", - len(outputStrElements)) - } - - winVersion := strings.Split(strings.TrimSpace(outputStrElements[1]), ".") //secend element in the output is the actual Windows version value - if len(winVersion) != 3 { - logger.Warnf("Unexpected format for the Windows version property value: %s, setting WMIC as WMI interface", outputStr) - return wmic, fmt.Errorf("Error while parsing the Windows version property value. Got %v elements and was expecting 3.", len(winVersion)) - } - - winMajorVersion, err := strconv.Atoi(winVersion[0]) - if err != nil { - logger.Warnf("Bad format for the Windows major version value: %s, setting WMIC as WMI interface", winVersion[0]) - return wmic, fmt.Errorf("Error while parsing Windows major version: %v", err) - } - winMinorVersion, err := strconv.Atoi(winVersion[1]) - if err != nil { - logger.Warnf("Bad format for the Windows minor version value: %s, setting WMIC as WMI interface", winVersion[1]) - return wmic, fmt.Errorf("Error while parsing Windows minor version: %v", err) - } - winBuildNumber, err := strconv.Atoi(winVersion[2]) - if err != nil { - logger.Warnf("Bad format for the Windows build number value: %s, setting WMIC as WMI interface", winVersion[2]) - return wmic, fmt.Errorf("Error while parsing Windows build number: %v", err) - } - - // if it is Windows 2025 or later use CIM Instance, otherwise use WMIC - if winMajorVersion >= win2025MajorVersion && winMinorVersion >= win2025MinorVersion && winBuildNumber >= win2025BuildNumber { - return cimInstance, nil - } else { - return wmic, nil - } - } - - logger.Warnf("There was an issue while retrieving Windows version data: %s, setting WMIC as WMI interface", err) - return wmic, err -} - -func csproductUuid(logger log.T, wmiInterface WMIInterface) (encodedValue string, err error) { - var uuid string - switch wmiInterface { - case wmic: - encodedValue, uuid, err = commandOutputHash(wmicCommand, "csproduct", "get", "UUID") - case cimInstance: - encodedValue, uuid, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_ComputerSystemProduct", "UUID")) - default: - logger.Warnf("Unknown WMI interface: %v", wmiInterface) - } - +func csproductUuid(logger log.T) (encodedValue string, err error) { + encodedValue, uuid, err := commandOutputHash(wmicCommand, "csproduct", "get", "UUID") logger.Tracef("Current UUID value: /%v/", uuid) return } -func processorInfoHash(wmiInterface WMIInterface) (value string, err error) { - switch wmiInterface { - case wmic: - value, _, err = commandOutputHash(wmicCommand, "cpu", "list", "brief") - case cimInstance: - propertiesToRetrieve := []string{"Caption", "DeviceID", "Manufacturer", "MaxClockSpeed", "Name", "SocketDesignation"} - value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("WIN32_PROCESSOR", propertiesToRetrieve...)) - default: - logger.Warnf("Unknown WMI interface: %v", wmiInterface) - } - +func processorInfoHash() (value string, err error) { + value, _, err = commandOutputHash(wmicCommand, "cpu", "list", "brief") return } -func memoryInfoHash(wmiInterface WMIInterface) (value string, err error) { - switch wmiInterface { - case wmic: - value, _, err = commandOutputHash(wmicCommand, "memorychip", "list", "brief") - case cimInstance: - propertiesToRetrieve := []string{"Capacity", "DeviceLocator", "MemoryType", "Name", "Tag", "TotalWidth"} - value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_PhysicalMemory", propertiesToRetrieve...)) - default: - logger.Warnf("Unknown WMI interface: %v", wmiInterface) - } - +func memoryInfoHash() (value string, err error) { + value, _, err = commandOutputHash(wmicCommand, "memorychip", "list", "brief") return } -func biosInfoHash(wmiInterface WMIInterface) (value string, err error) { - switch wmiInterface { - case wmic: - value, _, err = commandOutputHash(wmicCommand, "bios", "list", "brief") - case cimInstance: - propertiesToRetrieve := []string{"Manufacturer", "Name", "SerialNumber", "SMBIOSBIOSVersion", "Version"} - value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_BIOS", propertiesToRetrieve...)) - default: - logger.Warnf("Unknown WMI interface: %v", wmiInterface) - } - +func biosInfoHash() (value string, err error) { + value, _, err = commandOutputHash(wmicCommand, "bios", "list", "brief") return } -func systemInfoHash(wmiInterface WMIInterface) (value string, err error) { - switch wmiInterface { - case wmic: - value, _, err = commandOutputHash(wmicCommand, "computersystem", "list", "brief") - case cimInstance: - propertiesToRetrieve := []string{"Domain", "Manufacturer", "Model", "Name", "PrimaryOwnerName", "TotalPhysicalMemory"} - value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_ComputerSystem", propertiesToRetrieve...)) - default: - logger.Warnf("Unknown WMI interface: %v", wmiInterface) - } - +func systemInfoHash() (value string, err error) { + value, _, err = commandOutputHash(wmicCommand, "computersystem", "list", "brief") return } -func diskInfoHash(wmiInterface WMIInterface) (value string, err error) { - switch wmiInterface { - case wmic: - value, _, err = commandOutputHash(wmicCommand, "diskdrive", "list", "brief") - case cimInstance: - propertiesToRetrieve := []string{"Caption", "DeviceID", "Model", "Partitions", "Size"} - value, _, err = commandOutputHash(appconfig.PowerShellPluginCommandName, getCimInstanceCmdArgs("Win32_DiskDrive", propertiesToRetrieve...)) - default: - logger.Warnf("Unknown WMI interface: %v", wmiInterface) - } - +func diskInfoHash() (value string, err error) { + value, _, err = commandOutputHash(wmicCommand, "diskdrive", "list", "brief") return } - -func getCimInstanceCmdArgs(className string, properties ...string) string { - var sb strings.Builder - sb.WriteString(fmt.Sprintf("Get-CimInstance -ClassName %s", className)) - if len(properties) > 0 { - sb.WriteString(" | Select-Object ") - for _, property := range properties { - sb.WriteString(fmt.Sprintf("%s, ", property)) - } - } - - return fmt.Sprintf("%s | ConvertTo-Json", strings.TrimRight(sb.String(), ", ")) -} diff --git a/agent/platform/platform_windows.go b/agent/platform/platform_windows.go index da91f8ec4..132fd3d24 100644 --- a/agent/platform/platform_windows.go +++ b/agent/platform/platform_windows.go @@ -21,6 +21,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "regexp" "strconv" "strings" @@ -29,11 +30,9 @@ import ( "github.com/aws/amazon-ssm-agent/agent/log" ) -const ( - caption = "Caption" - version = "Version" - sku = "OperatingSystemSKU" -) +const caption = "Caption" +const version = "Version" +const sku = "OperatingSystemSKU" // Win32_OperatingSystems https://msdn.microsoft.com/en-us/library/aa394239%28v=vs.85%29.aspx const ( @@ -110,8 +109,8 @@ func getPlatformDetails(property string, log log.T) (value string, err error) { log.Debugf(gettingPlatformDetailsMessage) value = notAvailableMessage - cmdName := appconfig.PowerShellPluginCommandName - cmdArgs := []string{fmt.Sprintf("Get-CimInstance -ClassName Win32_OperatingSystem | Format-List -Property %s", property)} + cmdName := "wmic" + cmdArgs := []string{"OS", "get", property, "/format:list"} var cmdOut []byte if cmdOut, err = exec.Command(cmdName, cmdArgs...).Output(); err != nil { log.Debugf("There was an error running %v %v, err:%v", cmdName, cmdArgs, err) @@ -122,7 +121,7 @@ func getPlatformDetails(property string, log log.T) (value string, err error) { value = strings.TrimSpace(string(cmdOut)) // Match whitespaces between property and = sign and remove whitespaces - rp := regexp.MustCompile(fmt.Sprintf("%v(\\s*)%v", property, ":")) + rp := regexp.MustCompile(fmt.Sprintf("%v(\\s*)%v", property, "=")) value = rp.ReplaceAllString(value, "") // Trim spaces again @@ -132,12 +131,14 @@ func getPlatformDetails(property string, log log.T) (value string, err error) { return } +var wmicCommand = filepath.Join(appconfig.EnvWinDir, "System32", "wbem", "wmic.exe") + // fullyQualifiedDomainName returns the Fully Qualified Domain Name of the instance, otherwise the hostname func fullyQualifiedDomainName(log log.T) string { hostName, _ := os.Hostname() - dnsHostName := getWMIComputerSystemValue("DNSHostName") - domainName := getWMIComputerSystemValue("Domain") + dnsHostName := getWMICComputerSystemValue("DNSHostName") + domainName := getWMICComputerSystemValue("Domain") if dnsHostName == "" || domainName == "" { return hostName @@ -146,12 +147,11 @@ func fullyQualifiedDomainName(log log.T) string { return dnsHostName + "." + domainName } -// getWMIComputerSystemValue return the specified attribute from WMI via powershell -func getWMIComputerSystemValue(attribute string) string { - cmdArgs := []string{fmt.Sprintf("Get-CimInstance -Class Win32_ComputerSystem | Format-List -Property %s", attribute)} - if contentBytes, err := exec.Command(appconfig.PowerShellPluginCommandName, cmdArgs...).Output(); err == nil { +// getWMICComputerSystemValue return the value part of the wmic computersystem command for the specified attribute +func getWMICComputerSystemValue(attribute string) string { + if contentBytes, err := exec.Command(wmicCommand, "computersystem", "get", attribute, "/value").Output(); err == nil { contents := string(contentBytes) - data := strings.Split(contents, ":") + data := strings.Split(contents, "=") if len(data) > 1 { return strings.TrimSpace(data[1]) } diff --git a/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect.go b/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect.go index 64c42b8f9..39c11a896 100644 --- a/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect.go +++ b/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect.go @@ -2,10 +2,11 @@ package windows import ( "fmt" + "os" "os/exec" + "path/filepath" "regexp" - "github.com/aws/amazon-ssm-agent/agent/appconfig" "github.com/aws/amazon-ssm-agent/agent/log" c "github.com/aws/amazon-ssm-agent/agent/plugins/configurepackage/envdetect/constants" @@ -61,8 +62,9 @@ func isWindowsNano(operatingSystemSKU string) bool { } func getWmiOSInfo() (string, error) { - cmdArgs := []string{"Get-CimInstance -ClassName Win32_OperatingSystem | Format-List -Property OperatingSystemSKU, Version"} - cmdOut, err := exec.Command(appconfig.PowerShellPluginCommandName, cmdArgs...).Output() + wmicCommand := filepath.Join(os.Getenv("WINDIR"), "System32", "wbem", "wmic.exe") + cmdArgs := []string{"OS", "get", "/format:list"} + cmdOut, err := exec.Command(wmicCommand, cmdArgs...).Output() if err != nil { return "", err } @@ -79,7 +81,7 @@ func parseOperatingSystemSKU(wmioutput string) (string, error) { } func parseProperty(wmioutput string, property string) (string, error) { - regex := fmt.Sprintf(`(?m)^\s*%s\s*:\s*(\S+)\s*$`, property) + regex := fmt.Sprintf(`(?m)^\s*%s\s*=\s*(\S+)\s*$`, property) re := regexp.MustCompile(regex) match := re.FindStringSubmatch(wmioutput) diff --git a/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect_test.go b/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect_test.go index bb7f96420..e22924886 100644 --- a/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect_test.go +++ b/agent/plugins/configurepackage/envdetect/osdetect/windows/osdetect_test.go @@ -36,69 +36,69 @@ func TestDetectPlatformDetails(t *testing.T) { "Microsoft(R) Windows(R) Server 2003 Datacenter x64 Edition", ` -BootDevice:\Device\HarddiskVolume1 -BuildNumber:3790 -BuildType:Multiprocessor Free -Caption:Microsoft(R) Windows(R) Server 2003 Datacenter x64 Edition -CodeSet:1252 -CountryCode:1 -CreationClassName:Win32_OperatingSystem -CSCreationClassName:Win32_ComputerSystem -CSDVersion:Service Pack 2 -CSName:AMAZON-901389D0 -CurrentTimeZone:0 -DataExecutionPrevention_32BitApplications:TRUE -DataExecutionPrevention_Available:TRUE -DataExecutionPrevention_Drivers:TRUE -DataExecutionPrevention_SupportPolicy:3 -Debug:FALSE -Description: -Distributed:FALSE -EncryptionLevel:256 -ForegroundApplicationBoost:0 -FreePhysicalMemory:15747592 -FreeSpaceInPagingFiles:518624 -FreeVirtualMemory:16201920 -InstallDate:20171019094403.000000+000 -LargeSystemCache:1 -LastBootUpTime:20171019094424.510250+000 -LocalDateTime:20171019114056.709000+000 -Locale:0409 -Manufacturer:Microsoft Corporation -MaxNumberOfProcesses:-1 -MaxProcessMemorySize:8589934464 -Name:Microsoft Windows Server 2003 R2 Datacenter x64 Edition|C:\WINDOWS|\Device\ +BootDevice=\Device\HarddiskVolume1 +BuildNumber=3790 +BuildType=Multiprocessor Free +Caption=Microsoft(R) Windows(R) Server 2003 Datacenter x64 Edition +CodeSet=1252 +CountryCode=1 +CreationClassName=Win32_OperatingSystem +CSCreationClassName=Win32_ComputerSystem +CSDVersion=Service Pack 2 +CSName=AMAZON-901389D0 +CurrentTimeZone=0 +DataExecutionPrevention_32BitApplications=TRUE +DataExecutionPrevention_Available=TRUE +DataExecutionPrevention_Drivers=TRUE +DataExecutionPrevention_SupportPolicy=3 +Debug=FALSE +Description= +Distributed=FALSE +EncryptionLevel=256 +ForegroundApplicationBoost=0 +FreePhysicalMemory=15747592 +FreeSpaceInPagingFiles=518624 +FreeVirtualMemory=16201920 +InstallDate=20171019094403.000000+000 +LargeSystemCache=1 +LastBootUpTime=20171019094424.510250+000 +LocalDateTime=20171019114056.709000+000 +Locale=0409 +Manufacturer=Microsoft Corporation +MaxNumberOfProcesses=-1 +MaxProcessMemorySize=8589934464 +Name=Microsoft Windows Server 2003 R2 Datacenter x64 Edition|C:\WINDOWS|\Device\ Harddisk0\Partition1 -NumberOfLicensedUsers:5 -NumberOfProcesses:45 -NumberOfUsers:3 -Organization:AMAZON -OSLanguage:1033 -OSProductSuite:402 -OSType:18 -OtherTypeDescription:R2 -PAEEnabled: -PlusProductID: -PlusVersionNumber: -Primary:TRUE -ProductType:3 -QuantumLength:0 -QuantumType:0 -RegisteredUser:AMAZON -SerialNumber:76871-644-6528304-50084 -ServicePackMajorVersion:2 -ServicePackMinorVersion:0 -SizeStoredInPagingFiles:524288 -Status:OK -SuiteMask:402 -SystemDevice:\Device\HarddiskVolume1 -SystemDirectory:C:\WINDOWS\system32 -SystemDrive:C: -TotalSwapSpaceSize: -TotalVirtualMemorySize:16691380 -TotalVisibleMemorySize:16776488 -Version:5.2.3790 -WindowsDirectory:C:\WINDOWS +NumberOfLicensedUsers=5 +NumberOfProcesses=45 +NumberOfUsers=3 +Organization=AMAZON +OSLanguage=1033 +OSProductSuite=402 +OSType=18 +OtherTypeDescription=R2 +PAEEnabled= +PlusProductID= +PlusVersionNumber= +Primary=TRUE +ProductType=3 +QuantumLength=0 +QuantumType=0 +RegisteredUser=AMAZON +SerialNumber=76871-644-6528304-50084 +ServicePackMajorVersion=2 +ServicePackMinorVersion=0 +SizeStoredInPagingFiles=524288 +Status=OK +SuiteMask=402 +SystemDevice=\Device\HarddiskVolume1 +SystemDirectory=C:\WINDOWS\system32 +SystemDrive=C: +TotalSwapSpaceSize= +TotalVirtualMemorySize=16691380 +TotalVisibleMemorySize=16776488 +Version=5.2.3790 +WindowsDirectory=C:\WINDOWS `, "5.2.3790", @@ -106,71 +106,71 @@ WindowsDirectory:C:\WINDOWS }, { "Microsoft Windows Server 2016 Datacenter Nano", - `BootDevice : \Device\HarddiskVolume1 -BuildNumber : 14393 -BuildType : Multiprocessor Free -Caption : Microsoft Windows Server 2016 Datacenter -CodeSet : 1252 -CountryCode : 1 -CreationClassName : Win32_OperatingSystem -CSCreationClassName : Win32_ComputerSystem -CSDVersion : -CurrentTimeZone : 0 -DataExecutionPrevention_32BitApplications: TRUE -DataExecutionPrevention_Available: TRUE -DataExecutionPrevention_Drivers: TRUE -DataExecutionPrevention_SupportPolicy: 2 -Debug : -Distributed : FALSE -EncryptionLevel : -FreeSpaceInPagingFiles : 1048316 -FreeVirtualMemory : 1885364 -InstallDate : 20171018143126.000000+000 -LargeSystemCache : -LocalDateTime : 20171019121946.074000+000 -Locale : 0409 -Manufacturer : Microsoft Corporation -MaxNumberOfProcesses : 4294967295 -MaxProcessMemorySize : 137438953344 -Name : Microsoft Windows Server 2016 Datacenter|C:\Windows|\Device\Harddisk0\Partition1 -NumberOfLicensedUsers : 0 -NumberOfProcesses : 21 -NumberOfUsers : 0 -OperatingSystemSKU : 143 -Organization : Amazon -OSArchitecture : 64-bit -OSLanguage : 1033 -OSProductSuite : 272 -OSType : 18 -OtherTypeDescription : -Primary : TRUE -ProductType : 3 -RegisteredUser : Amazon -SerialNumber : -ServicePackMinorVersion : 0 -SizeStoredInPagingFiles : 1048316 -Status : OK -SuiteMask : 272 -SystemDevice : \Device\HarddiskVolume1 -SystemDirectory : C:\Windows\system32 -SystemDrive : C: -TotalSwapSpaceSize : -TotalVisibleMemorySize : 1048176 -Version : 10.0.14393 -WindowsDirectory : C:\Windows + `BootDevice = \Device\HarddiskVolume1 +BuildNumber = 14393 +BuildType = Multiprocessor Free +Caption = Microsoft Windows Server 2016 Datacenter +CodeSet = 1252 +CountryCode = 1 +CreationClassName = Win32_OperatingSystem +CSCreationClassName = Win32_ComputerSystem +CSDVersion = +CurrentTimeZone = 0 +DataExecutionPrevention_32BitApplications= TRUE +DataExecutionPrevention_Available= TRUE +DataExecutionPrevention_Drivers= TRUE +DataExecutionPrevention_SupportPolicy= 2 +Debug = +Distributed = FALSE +EncryptionLevel = +FreeSpaceInPagingFiles = 1048316 +FreeVirtualMemory = 1885364 +InstallDate = 20171018143126.000000+000 +LargeSystemCache = +LocalDateTime = 20171019121946.074000+000 +Locale = 0409 +Manufacturer = Microsoft Corporation +MaxNumberOfProcesses = 4294967295 +MaxProcessMemorySize = 137438953344 +Name = Microsoft Windows Server 2016 Datacenter|C:\Windows|\Device\Harddisk0\Partition1 +NumberOfLicensedUsers = 0 +NumberOfProcesses = 21 +NumberOfUsers = 0 +OperatingSystemSKU = 143 +Organization = Amazon +OSArchitecture = 64-bit +OSLanguage = 1033 +OSProductSuite = 272 +OSType = 18 +OtherTypeDescription = +Primary = TRUE +ProductType = 3 +RegisteredUser = Amazon +SerialNumber = +ServicePackMinorVersion = 0 +SizeStoredInPagingFiles = 1048316 +Status = OK +SuiteMask = 272 +SystemDevice = \Device\HarddiskVolume1 +SystemDirectory = C:\Windows\system32 +SystemDrive = C: +TotalSwapSpaceSize = +TotalVisibleMemorySize = 1048176 +Version = 10.0.14393 +WindowsDirectory = C:\Windows `, "10.0.14393nano", false, }, { "missing OperatingSystemSKU", - "Version:10.0.14393", + "Version=10.0.14393", "10.0.14393", false, }, { "missing version", - "OperatingSystemSKU : 143", + "OperatingSystemSKU = 143", "", true, }, @@ -199,31 +199,31 @@ func TestParseVersion(t *testing.T) { }{ { "simple single line version", - "Version:10.0.14393", + "Version=10.0.14393", "10.0.14393", false, }, { "simple multiline line version", - "Version:10.0.14393\n", + "Version=10.0.14393\n", "10.0.14393", false, }, { "whitespace version", - " \t Version \t : \t 10.0.14393 \t", + " \t Version \t = \t 10.0.14393 \t", "10.0.14393", false, }, { "multiple version", - "CdVersion:342\nVersion:10.0.14393", + "CdVersion=342\nVersion=10.0.14393", "10.0.14393", false, }, { "windows newline", - "\r\nVersion:10.0.14393\r\n", + "\r\nVersion=10.0.14393\r\n", "10.0.14393", false, }, @@ -258,31 +258,31 @@ func TestParseOperatingSystemSKU(t *testing.T) { }{ { "simple single line SKU", - "OperatingSystemSKU:7", + "OperatingSystemSKU=7", "7", false, }, { "simple multiline line SKU", - "OperatingSystemSKU:7\n", + "OperatingSystemSKU=7\n", "7", false, }, { "whitespace SKU", - " \t OperatingSystemSKU \t : \t 7 \t", + " \t OperatingSystemSKU \t = \t 7 \t", "7", false, }, { "multiple SKU", - "SomeOtherOperatingSystemSKU:143\nOperatingSystemSKU:7", + "SomeOtherOperatingSystemSKU=143\nOperatingSystemSKU=7", "7", false, }, { "windows newline", - "\r\nOperatingSystemSKU:7\r\n", + "\r\nOperatingSystemSKU=7\r\n", "7", false, }, diff --git a/agent/updateutil/updateutil_windows.go b/agent/updateutil/updateutil_windows.go index 8604087e2..fd5e75f4f 100644 --- a/agent/updateutil/updateutil_windows.go +++ b/agent/updateutil/updateutil_windows.go @@ -22,6 +22,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "strings" "time" @@ -51,6 +52,7 @@ var ( backOffRetry = backoff.Retry deleteFile = fileutil.DeleteFile fileWrite = fileutil.WriteIntoFileWithPermissions + wmicCommand = filepath.Join(appconfig.EnvWinDir, "System32", "wbem", "wmic.exe") getVersionThroughRegistryKeyRef = getVersionThroughRegistryKey ) @@ -359,17 +361,16 @@ func getVersionThroughRegistryKey(log log.T) string { func getVersionThroughWMI(log log.T) string { version := "" - cmdArgs := []string{`Get-CimInstance -ClassName Win32_Product -Filter "Name like 'Amazon SSM Agent%'" | Format-List -Property @{name='Version';expr={$_.version}}`} - contentBytes, err := execCommand(appconfig.PowerShellPluginCommandName, cmdArgs...).Output() + contentBytes, err := execCommand(wmicCommand, "product", "where", "name like 'Amazon SSM Agent%'", "get", "version", "/Value").Output() if err != nil { - log.Warnf("Error getting version value from WMI: %v %v", string(contentBytes), err) + log.Warnf("Error getting version value from WMIC: %v %v", string(contentBytes), err) return version } contents := string(contentBytes) - log.Debugf("Version info from WMI: %v", contents) - data := strings.Split(contents, ":") + log.Debugf("Version info from WMIC: %v", contents) + data := strings.Split(contents, "=") if len(data) > 1 { version = strings.TrimSpace(data[1]) } - return version + return strings.TrimSpace(version) } diff --git a/common/identity/availableidentities/ec2/ec2detector/helper/helper.go b/common/identity/availableidentities/ec2/ec2detector/helper/helper.go index 12997545a..d7fbb41ee 100644 --- a/common/identity/availableidentities/ec2/ec2detector/helper/helper.go +++ b/common/identity/availableidentities/ec2/ec2detector/helper/helper.go @@ -44,7 +44,7 @@ type Detector interface { type DetectorHelper interface { // MatchUuid ensured string matches an uuid format and starts with ec2 MatchUuid(string) bool - // GetSystemInfo retrieves the system information based on platform, linux reads files while on Windows queries wmi + // GetSystemInfo retrieves the system information based on platform, linux reads files while on Windows queries wmic GetSystemInfo(string) string }