windows:powershell:insertinstance
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| windows:powershell:insertinstance [2013/10/15 03:13] – external edit 127.0.0.1 | windows: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' | + | 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' |
| 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 6: | Line 6: | ||
| <code powershell insertInstance.ps1> | <code powershell insertInstance.ps1> | ||
| - | function getSqlInstanceName($ComputerName) | + | function getSqlInstanceName($ComputerName) { |
| - | { | + | $SqlInstances = Get-Service -ComputerName $ComputerName | where {($_.Name -like |
| - | $SqlInstances = Get-Service -ComputerName $ComputerName | where {($_.Name -like | + | |
| ' | ' | ||
| - | $instanceNameArray | + | $instanceArray |
| if ($SqlInstances -ne $null) { | if ($SqlInstances -ne $null) { | ||
| $SqlInstances | foreach { | $SqlInstances | foreach { | ||
| - | if ($_.Name -eq ' | + | $sqlName = $_.Name |
| - | [void] | + | $service = Get-WmiObject win32_service -ComputerName $ComputerName | where {$_.Name -eq $sqlName} |
| - | $instanceNameArray.add(@{InstanceName=$ComputerName; | + | if ($sqlName |
| - | | + | $instanceArray = $instanceArray + @(@{InstanceName=$ComputerName; StartupAcct=$service.StartName; Status=$_.Status})} |
| else { | else { | ||
| - | | + | $instanceArray = $instanceArray + @(@{InstanceName=$ComputerName + " |
| - | | + | $sqlName.split(" |
| - | $_.Name.split(" | + | |
| - | } | + | |
| - | } | + | function insertInstanceSQL($i, |
| - | } | + | $InstanceName = $i.InstanceName |
| - | $instanceNameArray | + | $StartupAcct = $i.StartupAcct |
| - | } | + | if ($InstanceName -ne $null) { |
| + | | ||
| + | $IsActive = " | ||
| + | else {$IsActive = " | ||
| + | $sql = "EXEC Windows.Instance_Insert $HostID, ' | ||
| + | return $sql}} | ||
| $HostList = Invoke-Sqlcmd -ServerInstance " | $HostList = Invoke-Sqlcmd -ServerInstance " | ||
| - | Windows.Host_Select_HostID_HostName" | + | Windows.Host_Select_HostID_HostName" |
| $HostList | foreach { | $HostList | foreach { | ||
| $HostID = $_.HostID | $HostID = $_.HostID | ||
| - | Try { | + | Try {$SqlInstances = getSqlInstanceName($_.HostName)} |
| - | | + | Catch {Return} |
| - | Catch { | + | |
| - | | + | |
| $SqlInstances | foreach { | $SqlInstances | foreach { | ||
| - | | + | $sql = insertInstanceSQL |
| - | if ($InstanceName -ne $null) { | + | Invoke-Sqlcmd -Query $sql -ServerInstance " |
| - | if ($_.Status -eq ' | + | |
| - | $IsActive = " | + | |
| - | else { | + | |
| - | $IsActive = " | + | |
| - | $sql = "EXEC Windows.Instance_Insert | + | |
| - | ' | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | } | + | |
| - | } | + | |
| </ | </ | ||
windows/powershell/insertinstance.1381806784.txt.gz · Last modified: (external edit)
