2017-07-07 49 views
-1

我正在使用在PowerShell和命令提示符下執行的簡單PowerShell文件。從命令提示符調用ps1腳本時發生Powershell錯誤

在PowerShell中它正在執行正確的給出所需的輸出,但是當從命令提示符調用時它無法連接到服務器並且找不到網絡。

filter.ps1:從PowerShell控制檯

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 
} 

輸出:從命令提示符

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:\> 

回答

3

在你的命令行,沒有參數。服務器名稱爲空

+0

非常感謝我的感謝 – deepti

相關問題