This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
windows:powershell:insertinstance [2013/10/14 22:19] haidong Got the right PowerShell code this time. |
windows:powershell:insertinstance [2013/11/04 21:20] haidong Change SysMetrics to JiMetrics |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Here is a PowerShell script to discover SQL Server instances installed on the server hosts in the Host table. 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. | + | Here is a PowerShell script to discover SQL Server instances installed on the server hosts in the Host table. 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. |
This information should be fairly static. Depending on your situation, scheduling this to run weekly, monthly, or quarterly, or whatever. You can always manually kick off this job if needed. | This information should be fairly static. Depending on your situation, scheduling this to run weekly, monthly, or quarterly, or whatever. You can always manually kick off this job if needed. | ||
Line 5: | Line 5: | ||
** Remember to change "sql1" to your host instance name ** | ** Remember to change "sql1" to your host instance name ** | ||
- | <code powershell updateInstance.ps1> | + | <code powershell insertInstance.ps1> |
- | $InstanceList = Invoke-Sqlcmd -Query "exec Windows.Instance_Select_InstanceID_InstanceName" -ServerInstance "sql1" -Database "SysMetrics" | + | function getSqlInstanceName($ComputerName) |
- | $InstanceList | ForEach-Object { | + | { |
- | + | $SqlInstances = Get-Service -ComputerName $ComputerName | where {($_.Name -like | |
- | $InstanceName = $_.InstanceName | + | 'mssql$*') -or ($_.Name -eq 'mssqlserver')} |
- | $InstanceID = $_.InstanceID | + | $instanceNameArray = New-Object System.Collections.ArrayList |
- | + | if ($SqlInstances -ne $null) { | |
- | Try { | + | $SqlInstances | foreach { |
- | $result = Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT SERVERPROPERTY('Edition')" | + | if ($_.Name -eq 'mssqlserver') { |
- | $InstanceEdition = $result.column1 | + | [void] |
- | $result = Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT | + | $instanceNameArray.add(@{InstanceName=$ComputerName;Status=$_.Status}) |
- | SERVERPROPERTY('EditionID')" | + | } |
- | $InstanceEditionID = $result.column1 | + | else { |
- | $result = Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT SERVERPROPERTY('ProductVersion')" | + | [void] |
- | $InstanceVersion = $result.column1 | + | $instanceNameArray.add(@{InstanceName=$ComputerName + "\" + |
- | $result = Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT SERVERPROPERTY('ProductLevel')" | + | $_.Name.split("$")[1];Status=$_.Status}) |
- | $InstanceServicePack = $result.column1 | + | } |
- | + | } | |
- | $sql = "EXEC Windows.Instance_Update $InstanceID, | + | } |
- | '$InstanceEdition', '$InstanceEditionID', '$InstanceVersion', '$InstanceServicePack'" | + | $instanceNameArray |
- | Invoke-Sqlcmd -Query $sql -ServerInstance "sql1" -Database "SysMetrics" | + | } |
+ | $HostList = Invoke-Sqlcmd -ServerInstance "sql1" -Query "EXEC | ||
+ | Windows.Host_Select_HostID_HostName" -Database "JiMetrics" | ||
+ | $HostList | foreach { | ||
+ | $HostID = $_.HostID | ||
+ | Try { | ||
+ | $SqlInstances = getSqlInstanceName($_.HostName) } | ||
+ | Catch { | ||
+ | Return } | ||
+ | $SqlInstances | foreach { | ||
+ | $InstanceName = $_.InstanceName | ||
+ | if ($InstanceName -ne $null) { | ||
+ | if ($_.Status -eq 'running') { | ||
+ | $IsActive = "Y"} | ||
+ | else { | ||
+ | $IsActive = "N"} | ||
+ | $sql = "EXEC Windows.Instance_Insert $HostID, '$InstanceName', | ||
+ | '$IsActive'" | ||
+ | Invoke-Sqlcmd -Query $sql -ServerInstance "sql1" -Database ` | ||
+ | "JiMetrics" | ||
} | } | ||
- | Catch { Return } | + | } |
} | } | ||
</code> | </code> |