[英]Composing a script in Powershell V3
我想知道如何使用Powershell v3創建腳本來獲取所有SQL實例的名稱。 例如,環境23Dev將具有兩個虛擬框。 ASNAV-DEVSQL-23和ASNAV-DEVWEB-23。 我想一直將信息ASNVA-DEVSQL-23一直返回到環境50?
有人可以協助嗎?
問候,
喬
這是一個列出SQLSERVER1和SQLSERVER2的SQL實例的示例:
"SQLSERVER1","SQLSERVER2" | % {
invoke-command -computername $_ -scriptblock{
"SERVER $env:computername"
(get-itemproperty 'HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server').InstalledInstances
}
}
這個單行代碼將以PowerShell對象的形式列出您的所有本地實例:
[System.Data.Sql.SqlDataSourceEnumerator]::Instance.GetDataSources() | Format-Table -Auto
摘自: http : //tangodude.wordpress.com/2014/02/07/list-local-sql-server-instances-with-powershell/
編輯:
此代碼段應比上一個代碼更好地工作:
$Current = Get-Location;
Import-Module SQLPS -DisableNameChecking;
Set-Location $Current;
[Microsoft.SqlServer.Management.Smo.SmoApplication]::EnumAvailableSqlServers()
編輯:
這是使用WMI查詢目標計算機上的服務。
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlWmiManagement") | Out-Null;
$MC = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer("COMPUTER_NAME_HERE");
foreach ($Instance in ($MC.Services | Where-Object { $_.Type -Eq "SqlServer" }))
{
Write-Host $Instance.Name;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.