windows:powershell:insertinstance
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| windows:powershell:insertinstance [2013/10/15 03:19] – Got the right PowerShell code this time. haidong | 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 5: | Line 5: | ||
| ** Remember to change " | ** Remember to change " | ||
| - | <code powershell | + | <code powershell |
| - | $InstanceList | + | function getSqlInstanceName($ComputerName) { |
| - | $InstanceList | + | $SqlInstances |
| + | ' | ||
| + | $instanceArray = @() | ||
| + | if ($SqlInstances -ne $null) { | ||
| + | $SqlInstances | ||
| + | $sqlName = $_.Name | ||
| + | $service = Get-WmiObject win32_service -ComputerName $ComputerName | where {$_.Name -eq $sqlName} | ||
| + | if ($sqlName -eq ' | ||
| + | $instanceArray = $instanceArray + @(@{InstanceName=$ComputerName; | ||
| + | else { | ||
| + | $instanceArray = $instanceArray + @(@{InstanceName=$ComputerName + " | ||
| + | $sqlName.split(" | ||
| + | return $instanceArray} | ||
| - | | + | function insertInstanceSQL($i, |
| - | $InstanceID | + | |
| + | $StartupAcct | ||
| + | if ($InstanceName -ne $null) { | ||
| + | if ($i.Status -eq ' | ||
| + | $IsActive = " | ||
| + | else {$IsActive = " | ||
| + | $sql = "EXEC Windows.Instance_Insert $HostID, ' | ||
| + | return $sql}} | ||
| - | Try { | + | $HostList |
| - | $result | + | Windows.Host_Select_HostID_HostName" |
| - | $InstanceEdition = $result.column1 | + | $HostList | foreach { |
| - | | + | $HostID |
| - | SERVERPROPERTY(' | + | Try {$SqlInstances |
| - | | + | Catch {Return} |
| - | | + | |
| - | | + | $sql = insertInstanceSQL |
| - | | + | Invoke-Sqlcmd -Query $sql -ServerInstance " |
| - | $InstanceServicePack = $result.column1 | + | |
| - | + | ||
| - | | + | |
| - | '$InstanceEdition', | + | |
| - | | + | |
| - | | + | |
| - | Catch { Return } | + | |
| - | } | + | |
| </ | </ | ||
windows/powershell/insertinstance.1381807196.txt.gz · Last modified: (external edit)
