簡體   English   中英

針對 PowerShell 中的 WID 遠程運行 sqlcmd

[英]Running sqlcmd against the WID in PowerShell remotely

我正在與 WSUS 戰斗並嘗試設置一個腳本來管理遠程站點中多台服務器上的 SUSDB 維護,我們有多個版本和多個操作系統的服務器,所以它變得有點復雜,較新的(2008R2 之后)工作沒有問題,但是,Server 2008s 有一個問題:

我在本地或交互式遠程 session 中運行它,它按預期工作,但如果我嵌套調用命令以在遠程 session 中運行它,如下所示,它會出錯並顯示以下內容:

HResult 0x2,級別 16,State 1
命名管道提供程序:無法打開與 SQL 服務器 [2] 的連接。
Sqlcmd:錯誤:Microsoft SQL Server Native Client 10.0:建立與 SQL Server 的連接時出現網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 檢查實例名稱是否正確,以及 SQL 服務器是否配置為允許遠程連接。 有關詳細信息,請參閱 SQL 服務器聯機叢書。

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 年的盒子,但是有一個利基產品供應商和更換盒子的一些預算問題。

在第 35 行,我看到以下內容

    Else {
        $conStr = 'np:\\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query'
    }

您可以嘗試在沒有np:的情況下運行它嗎?

    Else {
        $conStr = '\\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query'
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM