从命令提示符调用 ps1 脚本时出现电源外壳错误



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

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

过滤器.ps1

param([string]$servername)
$filePath1 = "D:tempresult.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:tempversion.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 FilesMicrosoft SQL Server$versionSetup BootstrapLogSummary.txt"
$pattern1=Select-String -Pattern "Final result:" -CaseSensitive -Path $filePath |Tee-Object D:temppattern.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:sysdbafilter.ps1 cx-siscsqltestsqlinst
1
string is not empty
PS D:sysdba>

命令提示符的输出:

H:>powershell.exe -noexit "& ""d:sysdbafilter.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:sysdbafilter.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:>

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

最新更新