User Tools

Site Tools


windows:powershell:insertinstance

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
windows:powershell:insertinstance [2013/10/15 03:19] – Got the right PowerShell code this time. haidongwindows:powershell:insertinstance [2018/05/15 03:00] (current) – external edit 127.0.0.1
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 
 +    'mssql$*'-or ($_.Name -eq 'mssqlserver')} 
 +    $instanceArray = @() 
 +    if ($SqlInstances -ne $null) { 
 +        $SqlInstances foreach { 
 + $sqlName = $_.Name 
 + $service = Get-WmiObject win32_service -ComputerName $ComputerName | where {$_.Name -eq $sqlName} 
 +            if ($sqlName -eq 'mssqlserver') { 
 +                $instanceArray = $instanceArray + @(@{InstanceName=$ComputerName; StartupAcct=$service.StartName; Status=$_.Status})} 
 +            else { 
 +                $instanceArray = $instanceArray + @(@{InstanceName=$ComputerName + "\"
 + $sqlName.split("$")[1];StartupAcct=$service.StartName; Status=$_.Status})}}} 
 +    return $instanceArray}
  
-    $InstanceName = $_.InstanceName +function insertInstanceSQL($i, $HostID) { 
-    $InstanceID = $_.InstanceID+    $InstanceName = $i.InstanceName 
 +    $StartupAcct = $i.StartupAcct 
 +    if ($InstanceName -ne $null) { 
 +        if ($i.Status -eq 'running') { 
 +            $IsActive = "Y"
 +        else {$IsActive = "N"
 +        $sql = "EXEC Windows.Instance_Insert $HostID, '$InstanceName', '$StartupAcct', '$IsActive'" 
 + return $sql}}
  
-        Try { +$HostList = Invoke-Sqlcmd -ServerInstance "sql1" -Query "EXEC 
-           $result = Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT SERVERPROPERTY('Edition')" +Windows.Host_Select_HostID_HostName" -Database "JiMetrics
-           $InstanceEdition = $result.column1 +$HostList | foreach { 
-           $result = Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT +    $HostID = $_.HostID 
-SERVERPROPERTY('EditionID')+    Try {$SqlInstances getSqlInstanceName($_.HostName)} 
-           $InstanceEditionID = $result.column1 +    Catch {Return} 
-           $result = Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT SERVERPROPERTY('ProductVersion')" +    $SqlInstances | foreach { 
-           $InstanceVersion = $result.column1 + $sql = insertInstanceSQL $$HostID 
-           $result Invoke-Sqlcmd -ServerInstance $InstanceName -Query "SELECT SERVERPROPERTY('ProductLevel')" +        Invoke-Sqlcmd -Query $sql -ServerInstance "sql1" -Database "JiMetrics"}}
-           $InstanceServicePack = $result.column1 +
- +
-           $sql = "EXEC Windows.Instance_Update $InstanceID, +
-'$InstanceEdition', '$InstanceEditionID', '$InstanceVersion', '$InstanceServicePack'" +
-           Invoke-Sqlcmd -Query $sql -ServerInstance "sql1" -Database "SysMetrics" +
-        } +
-    Catch { Return } +
-}+
 </code> </code>
windows/powershell/insertinstance.1381807196.txt.gz · Last modified: (external edit)