This shows you the differences between two versions of the page.
windows:powershell:updatehost [2013/10/14 22:13] 127.0.0.1 external edit |
windows:powershell:updatehost [2018/05/14 22:00] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Here is a PowerShell script to gather Windows host info such as OS version, hardware model, memory size, CPU type, core count. Please create a SQL Server Agent job for this. The domain account that this job runs under needs to in the local administrator's group on the host whose metrics it is trying to get. | ||
- | Server host information normally don't change that often, so it doesn't need to be run daily. Depending on your environment, schedule this to run monthly or even quarterly. You can always manually kick off this job if needed. | ||
- | |||
- | ** Remember to change "sql1" to your host instance name ** | ||
- | |||
- | <code powershell updateHost.ps1> | ||
- | $HostList = Invoke-Sqlcmd -Query "exec Windows.Host_Select_HostID_HostName" -ServerInstance "sql1" -Database "SysMetrics" | ||
- | $HostList | ForEach-Object { | ||
- | $HostName = $_.HostName | ||
- | $HostID = $_.HostID | ||
- | |||
- | try { | ||
- | $WmiResults = get-wmiobject -computername $HostName -Class Win32_ComputerSystem | ||
- | } | ||
- | catch [Exception] { | ||
- | continue | ||
- | } | ||
- | |||
- | $Domain = $WmiResults.Domain | ||
- | $HardwareVendor = $WmiResults.Manufacturer | ||
- | $HardwareModel = $WmiResults.Model | ||
- | $MemorySizeGB = ($WmiResults.TotalPhysicalMemory / 1gb) + 1 | ||
- | |||
- | try { | ||
- | $WmiResults = get-wmiobject -computername $HostName -Class Win32_Processor | select -first 1 | ||
- | } | ||
- | catch [Exception] { | ||
- | continue | ||
- | } | ||
- | |||
- | $CPUType = $WmiResults.name | ||
- | $CoreCount = $WmiResults.NumberOfCores | ||
- | |||
- | try { | ||
- | $WmiResults = get-wmiobject -computername $HostName -Class Win32_OperatingSystem | ||
- | } | ||
- | catch [Exception] { | ||
- | continue | ||
- | } | ||
- | |||
- | $OS = $WmiResults.Caption | ||
- | $OSArchitecture = $WmiResults.OSArchitecture | ||
- | $OSServicePack = $WmiResults.CSDVersion | ||
- | $OSVersionNumber = $WmiResults.Version | ||
- | |||
- | $sql = "EXEC Windows.Host_Update $HostID, '$Domain', '$OS', '$OSArchitecture', '$OSServicePack', '$OSVersionNumber', '$HardwareModel', '$HardwareVendor', $MemorySizeGB, '$CPUType', $CoreCount" | ||
- | Invoke-Sqlcmd -Query $sql -ServerInstance "sql1" -Database "SysMetrics" | ||
- | } | ||
- | </code> |