2015-02-11 89 views
0

我得到了這個Powershell腳本,它獲取有關SQL Server是否安裝在主機上並獲取SQL Server版本和其他詳細信息的信息。使用SMO從SQL Server收集數據非常慢

它從txt文件獲取主機列表並將信息保存到DataTable

$data = New-Object ('System.Data.DataTable') 
$data.Columns.Add('Host name') | Out-Null 
$data.Columns.Add('Ip Address') | Out-Null 
$data.Columns.Add('SQL Server Product Name') | Out-Null 
$data.Columns.Add('SQL Server Edition') | Out-Null 
$data.Columns.Add('SQL Server Version') | Out-Null 
$data.Columns.Add('SQL Server Type') | Out-Null 
$data.Columns.Add('SQL Server Status') | Out-Null 

Get-Content .\servers.txt | ForEach { 
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null 
    $row = $data.NewRow() 
    $row['Host name'] = $_ 
    try { 
     $row['Ip Address'] = [Net.Dns]::GetHostEntry($_).AddressList.IpAddressToString 
    } 
    catch [System.Net.Sockets.SocketException] { 
     $row['Ip Address'] = 'Offline' 
    } 

    If ($row['Ip Address'] -eq 'Offline') { 
     $row['SQL Server Product Name'] = 'N/A' 
     $row['SQL Server Edition'] = 'N/A' 
     $row['SQL Server Version'] = 'N/A' 
     $row['SQL Server Type'] = 'N/A' 
     $row['SQL Server Status'] = 'N/A' 
    } 
    else { 
     $smo = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $_ 
     $row['SQL Server Product Name'] = $smo.Product + ' ' + $smo.ProductLevel 
     $row['SQL Server Edition'] = $smo.Edition 
     $row['SQL Server Version'] = $smo.VersionString 
     $row['SQL Server Type'] = $smo.ServerType 
     $row['SQL Server Status'] = $smo.Status 
    } 
    $smo.ConnectionContext.Disconnect() 
    $data.Rows.Add($row) 
} 

$data | Format-Table -AutoSize 

此腳本的問題是運行需要很長時間(超過一小時的113個服務器列表)。

有什麼方法可以加快進程?

回答

1

您可以使用後臺作業異步運行腳本(您將不得不使用3個cmdlet:start-job,get-jobreceive-job)。

About_remote_jobs

啓動遠程JOB返回到本地計算機的結果(ASJOB)

To start a background job on a remote computer that returns the command 
results to the local computer, use the AsJob parameter of a cmdlet such 
as the Invoke-Command cmdlet. 

When you use the AsJob parameter, the job object is actually created on 
the local computer even though the job runs on the remote computer. When 
the job is completed, the results are returned to the local computer. 

You can use the cmdlets that contain the Job noun (the Job cmdlets) to 
manage any job created by any cmdlet. Many of the cmdlets that have 
AsJob parameters do not use Windows PowerShell remoting, so 
you can use them even on computers that are not configured for 
remoting and that do not meet the requirements for remoting.