Featch在多个服务器上安装SQL实例名称

时间:2017-06-02 11:47:19

标签: powershell

我写这个是为了在远程sql服务器上获取已安装实例的列表,但它失败了以下错误我将按如下方式实现此目的

  1. 从我的中央数据库服务器
  2. 中读取服务器名称列表
  3. 创建一个从step1中检索服务器的函数。
  4. 代码

    # -- Get the patch servers from central server  function
    get-patchservers {
        # -- Where is your central patching database located?
        $sql_inst   = Read-Host "SQL instance hosting Patching DB; default: Severname\SQL201201"
        # -- Default patching control server to "ServerName\SQL201201"
        if($sql_inst -eq "") {$sql_inst="Severname\SQL201201"}
        # -- What's the name of your central patching database?
        $sql_db     = Read-Host "Name of Patching DB; default:patchdb"
        # -- Default datatabase for patching db
        if($sql_db -eq "") {$sql_db="patchdb" }
        # -- T-SQL to feacth server name from cerntral db
        $Tsql = "SELECT svr_name  FROM server"
        $ServerList = Invoke-Sqlcmd -Query $Tsql -ServerInstance $sql_inst -Database $sql_db    #return $ServerList   
    
        foreach($Server in $ServerList) {
            ECHO $Server
            $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $Server)
            $regKey= $reg.OpenSubKey("SOFTWARE\\Microsoft\\MicrosoftSQL Server\\Instance Names\\SQL" )
            $instances = $regkey.GetValueNames()
            $instances
        }
    }    
    

1 个答案:

答案 0 :(得分:0)

我发现了这里造成的问题。它看起来与invoke-sqlcmd命令返回的标头有问题