在 PowerShell 中远程针对 WID 运行 sqlcmd

问题描述

我正在与 WSUS 进行斗争并尝试设置一个脚本来管理远程站点中多台服务器上的 SUSDB 维护,我们有多个版本和多个操作系统的服务器,所以它变得有点复杂,新的( 2008R2 后)工作没有问题,但是,Server 2008s 有问题:

我在本地或在交互式远程会话中运行它并且它按预期工作,但是如果我嵌套调用命令以在远程会话中运行它,如下所示,它会出错:

HResult 0x2,级别 16,状态 1
命名管道提供程序:无法打开与 sql Server [2] 的连接。
sqlcmd:错误:Microsoft sql Server Native Client 10.0:建立与 sql Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。检查实例名称是否正确以及 sql Server 是否配置为允许远程连接。有关详细信息,请参阅 sql Server 联机丛书..

sqlcmd:错误:Microsoft sql Server Native Client 10.0:登录超时

我已经搜索了几天并尝试了所有我能想到的排列方式,但这是我所拥有的:

$sqlPath = "C:\Users\<username>\Documents\sql Server Management Studio\"
$sqlfile = "SUSDB-ReIndex.sql"
$WsusSvrs = ("WSUS-Site1","WSUS-Site7","WSUS-Site13","WSUS-Site14","WSUS-Site15")
$ErrorActionPreference = "Stop"
$results = @()

Foreach ($WSUSSvr in $WSUSSvrs) {
    Try {
        $sess = New-PSSession -ComputerName $WSUSSvr -EnableNetworkAccess -ErrorAction Stop
    }
    Catch {
        continue;
    } 

    $output = Invoke-Command -Session $sess -ScriptBlock {
        If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {   
            $arguments = "& '" + $myinvocation.mycommand.deFinition + "'"
            start-process powershell -Verb runAs -ArgumentList $arguments
            Break
        } 

        If((Get-Command sqlcmd.exe -ErrorAction SilentlyContinue)-eq "") {
            $NosqlCmd = new-object System.IO.FileNotFoundException("sqlcmd is not Installed on this machine,please install the appropriate version and try again")
            Throw $NosqlCmd
        }

        $Output = New-Object psobject
        $OsverStr = (Gwmi win32_operatingsystem).version.split(".")
        [single]$OS = [convert]::ToSingle(($OsverStr[0],$OsverStr[1] -join "."))
        $Output | Add-Member -MemberType NoteProperty -Name Osver -Value $OS

        if ($OS -gt 6.1) {
            $conStr = "\\.\pipe\Microsoft##WID\tsql\query"
        }
        Else {
            $conStr = 'np:\\.\pipe\MSsql$MICROSOFT##SSEE\sql\query'
        }

        if(Test-Path "$using:sqlPath\$Using:sqlfile") {
            $cmd = "sqlcmd.exe -S $ConStr -i '$Using:sqlPath\$Using:sqlfile' -d 'SUSDB' -E -o 'C:\temp \$Using:WSUSSvr-reindexout.txt'"
            Invoke-Expression $cmd
            $output | Add-Member -MemberType NoteProperty -Name Message -Value "done"
        }
        else {
            $output | Add-Member -MemberType NoteProperty -Name Message -Value "Unable to find $Using:sqlfile"
        }
        $Output
    } -ArgumentList $sqlPath,$sqlfile,$WSUSSvr

    $results += $output

    if(test-path "\\$wsussvr\C$\temp\$WSUSSvr-reindexout.txt") {
        cp "\\$wsussvr\C$\temp\$WSUSSvr-reindexout.txt" "D:\wsus-reports\" -Force
    }

    If( $sess.State -eq "Opened" ) { Remove-PSSession $sess }
}

$results | ft

我知道现在我们应该会看到 2008 年的盒子,但是有一个小众产品供应商和一些更换盒子的预算问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)