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

时间:2016-08-04 11:17:14

标签: powershell

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)
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2 个答案:

答案 0 :(得分:1)

好吧,错误说够了。它无法识别" Get-SqlInstances"功能。

也许您的ISE主机配置文件中的某些内容导入了该功能的正确模块。

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

Get-Content $($profile.AllUsersCurrentHost)

Get-Content $($profile.CurrentUserCurrentHost)

答案 1 :(得分:0)

我有类似的问题。我的 Powershell 脚本会快速检查以测试与 SQL Server 数据库的连接。它使用 Invoke-sqlcmd。它定期在命令提示符下工作。事实上,它刚刚在命令提示符下工作了 3 次,然后它就停止了。没有任何变化,下面引用了代码。但它仍然适用于 ISE。 我不明白为什么它刚刚停止。欢迎提出任何建议。-

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

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

#============================================== ==================================== #== 当我使用 Powershell ISE 时,脚本有效:

.\ArgTest01.ps1 -csvFileName 'test.csv' -SQLServer '<MyServer>' -SQLDBName 'test' -Username 'test' -Password 'T35t3r_MCT3st3rFac3'

DBName
------
test

#============================================== ==================================== #== 但是,当我在命令 shell 中使用以下内容时,出现一组错误:

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