powershell脚本在ISE中工作正常,但在powershell命令提示符下失败?

powershell脚本在ISE中工作正常,但在powershell命令提示符下失败?,powershell,Powershell,嗯,这个错误说得够多了。它无法识别“Get SqlInstances”函数 也许是ISE主机配置文件中的某些内容为该功能导入了适当的模块 你必须先检查一下。在ISE主机中运行时,执行以下命令: Get-SqlInstances : The term 'Get-SqlInstances' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling

嗯,这个错误说得够多了。它无法识别“Get SqlInstances”函数

也许是ISE主机配置文件中的某些内容为该功能导入了适当的模块

你必须先检查一下。在ISE主机中运行时,执行以下命令:

Get-SqlInstances : The term 'Get-SqlInstances' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\Users\amitk.yadav\desktop\BLDiscover_check_SQLInvtInfo.ps1:88 char:1
+ Get-SqlInstances $server >> $OUTPUT_DIR\InstanceList.txt  # listing all sql inst ...
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-SqlInstances:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Get-Content : Cannot find path 'C:\Temp\InstanceList.txt' because it does not exist.
At C:\Users\amitk.yadav\desktop\BLDiscover_check_SQLInvtInfo.ps1:110 char:23
+ ForEach ($instance in Get-Content $OUTPUT_DIR\InstanceList.txt)
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


我也有类似的问题。我的Powershell脚本执行快速检查以测试与SQL Server数据库的连接。它使用invokesqlcmd。它定期在命令提示符下工作。事实上,它刚刚在命令提示符下工作了3次,然后就停止了。没有任何更改,代码参考如下。但它在ISE中仍然有效我不明白它为什么停止了。欢迎提出任何建议。-

Powershell脚本的代码非常简单。 #================================================================================== #==脚本代码为:

Get-Content $($profile.CurrentUserCurrentHost)
#================================================================================== #==当我使用Powershell ISE时,脚本工作:

param(
 [Parameter(Mandatory=$true)][string]$csvFileName
,[Parameter(Mandatory=$true)][string]$SQLServer
,[Parameter(Mandatory=$true)][string]$SQLDBName
,[Parameter(Mandatory=$true)][string]$Username
,[Parameter(Mandatory=$true)][String]$Password
)
$ConnectTest = 'SELECT DBName = DB_NAME()'

Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Username $Username -Password $Password -Query $ConnectTest -Verbose

可能只发布两条以上的错误消息
param(
 [Parameter(Mandatory=$true)][string]$csvFileName
,[Parameter(Mandatory=$true)][string]$SQLServer
,[Parameter(Mandatory=$true)][string]$SQLDBName
,[Parameter(Mandatory=$true)][string]$Username
,[Parameter(Mandatory=$true)][String]$Password
)
$ConnectTest = 'SELECT DBName = DB_NAME()'

Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Username $Username -Password $Password -Query $ConnectTest -Verbose
.\ArgTest01.ps1 -csvFileName 'test.csv' -SQLServer '<MyServer>' -SQLDBName 'test' -Username 'test' -Password 'T35t3r_MCT3st3rFac3'

DBName
------
test
powershell.exe -File ArgTest01.ps1 -csvFileName 'test.csv' -SQLServer '<MyServer>' -SQLDBName 'test' -Username 'test' -Password 'T35t3r_MCT3st3rFac3'
Invoke-Sqlcmd : A network-related or instance-specific 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 and that SQL Server is configured to allow remote connections.
(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

At C:\Apps\Dropbox\Code\Powershell\CsvSqlimport\ArgTest.ps1:22 char:1
+ Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Userna ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Invoke-Sqlcmd], SqlException
    + FullyQualifiedErrorId : SqlExectionError,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand

Invoke-Sqlcmd :
At C:\Apps\Dropbox\Code\Powershell\CsvSqlimport\ArgTest.ps1:22 char:1
+ Invoke-Sqlcmd -ServerInstance $SQLServer -Database $SQLDBName -Userna ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Sqlcmd], ParserException
    + FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand