从命令提示符调用ps1脚本时出现Powershell错误

时间:2017-07-07 06:29:52

标签: sql powershell

我使用简单的PowerShell文件在PowerShell和命令提示符下执行。

在PowerShell中,它正确执行所需的输出,但是当从命令提示符调用时,它无法连接到服务器并且无法找到网络。

filter.ps1

param([string]$servername)

$filePath1 = "D:\temp\result.txt"

try
{
    $SqlQuery = "select SUBSTRING(CONVERT(sysname, SERVERPROPERTY('ProductVersion')),0,CHARINDEX('.',convert(sysname,SERVERPROPERTY('ProductVersion')),0))"
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection

    $SqlConnection.ConnectionString = "Server = $Servername; Database = master; Integrated Security = SSPI;"
    #write-host $SqlConnection.ConnectionString 
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $SqlQuery
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $version = $DataSet.Tables[0].rows[0].Column1
    #Write-Host $version
    $DataSet.Tables[0] | out-file "C:\temp\version.csv"

    #switch case 
    switch -Exact($version)
    {
    '8'  {$version=80}
    '9'  {$version=90}
    '10' {$version=100}
    '11' {$version=110}
    '12' {$version=120}
    '13' {$version=130}
    }

    #Write-Host $version
    $filePath="C:\Program Files\Microsoft SQL Server\$version\Setup Bootstrap\Log\Summary.txt"

    $pattern1=Select-String -Pattern "Final result:" -CaseSensitive -Path $filePath |Tee-Object D:\temp\pattern.txt
    $pattern2=Select-String -Pattern "Final result:" -CaseSensitive -Path $filePath

    if($pattern2) 
    {            
        Write-Host "string is not empty"        
        $pattern3=Select-String -Pattern "Passed" -CaseSensitive -Path $filePath 

        if($pattern3) 
        {
        Write-Host "string is not empty"   
        Out-File -FilePath $filePath1
         'Success'| Out-File -FilePath $filePath1
        }
        else
        {       
         Out-File -FilePath $filePath1
         'failed'| Out-File -FilePath $filePath1 
        }
    } 
    else
    {
        Out-File -FilePath $filePath1
         'failed'| Out-File -FilePath $filePath1            
    }
}

catch
{
    Write-Host -BackgroundColor Red -ForegroundColor White "Fail" 
    $errText =  $Error[0].ToString() 

    if ($errText.Contains("network-related")) {Write-Host "Connection Error. Check server name, port, firewall."}   
    Write-Host $errText 
    continue 
}

PowerShell控制台的输出:

PS D:\sysdba> d:\sysdba\filter.ps1 cx-siscsqltest\sqlinst
1
string is not empty
PS D:\sysdba>

命令提示符输出:

H:\>powershell.exe -noexit "& ""d:\sysdba\filter.ps1"""
Fail
Connection Error. Check server name, port, firewall.
Exception calling "Fill" with "1" argument(s): "A network-related or instance-sp
ecific error occurred while establishing a connection to SQL Server. The server
was not found or was not accessible. Verify that the instance name is correct an
d that SQL Server is configured to allow remote connections. (provider: Named Pi
pes Provider, error: 40 - Could not open a connection to SQL Server)"
PS H:\> d:
PS D:\> powershell "d:\sysdba\filter.ps1"
Fail
Connection Error. Check server name, port, firewall.
Exception calling "Fill" with "1" argument(s): "A network-related or instance-sp
ecific error occurred while establishing a connection to SQL Server. The server
was not found or was not accessible. Verify that the instance name is correct an
d that SQL Server is configured to allow remote connections. (provider: Named Pi
pes Provider, error: 40 - Could not open a connection to SQL Server)"
PS D:\>

1 个答案:

答案 0 :(得分:3)

在命令行中没有参数。服务器名称为空