我有一个到远程 MS SQL服务器的连接字符串,并尝试使用Invoke-SqlCmd cmdlet查询此服务器。 这是脚本:
$ConnectionString = "data source=sqldev;initial catalog=test;User Id=dev;Password=mypassword;"
.\LoadSqlServerSMO.ps1 #this just loads SMO modules
$sqlConnection = new-object System.Data.SqlClient.SqlConnection($ConnectionString)
$connection = New-Object Microsoft.SqlServer.Management.Common.ServerConnection($sqlConnection)
$serverObject = New-Object Microsoft.SqlServer.Management.SMO.Server($connection)
$q1 = "select * from currency"
invoke-sqlcmd -Query $q1 `
-ServerInstance "sqldev" -Database "test" -Username "dev" -Password "mypassword" `
-ConnectionTimeout 3 -OutputSqlErrors $true -QueryTimeout 3 -SuppressProviderContextWarning -IgnoreProviderContext
invoke-sqlcmd -Query $q1 `
-ServerInstance $serverObject `
-ConnectionTimeout 3 -OutputSqlErrors $true -QueryTimeout 3 -SuppressProviderContextWarning -IgnoreProviderContext
invoke-sqlcmd : Value cannot be null.
Parameter name: ServerInstance
At Untitled3.ps1:22 char:14
+ $sqlResult = invoke-sqlcmd -Query $q1 `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Sqlcmd], ArgumentNullException
+ FullyQualifiedErrorId : CannotGetServerInstance,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
看起来,就像是PowerShell中的一个错误......或者我做错了什么?
以防万一,这里是$ PSVersionTable:
Name Value ---- ----- PSVersion 4.0 WSManStackVersion 3.0 SerializationVersion 1.1.0.1 CLRVersion 4.0.30319.42000 BuildVersion 6.3.9600.17090 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0} PSRemotingProtocolVersion 2.2
$ serverObject已填充,非null对象,连接存在,用户名/密码正常:
$ serverObject.Information | fl * 给出以下内容:
BuildClrVersionString : v4.0.30319 BuildNumber : 5058 Collation : SQL_Latin1_General_CP1_CI_AS CollationID : 872468488 ComparisonStyle : 196609 ComputerNamePhysicalNetBIOS : DEV-DB1 Edition : Enterprise Edition (64-bit) ErrorLogPath : C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Log FullyQualifiedNetName : dev-db1.dev.mydomain.local IsCaseSensitive : False IsClustered : False IsFullTextInstalled : False IsHadrEnabled : True IsSingleUser : False IsXTPSupported : False Language : English (United States) MasterDBLogPath : C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA MasterDBPath : C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA MaxPrecision : 38 NetName : DEV-DB1 OSVersion : 6.2 (9200) PhysicalMemory : 31250 Platform : NT x64 Processors : 4 Product : Microsoft SQL Server ProductLevel : SP2 ResourceLastUpdateDateTime : 5/14/2014 18:34:17 ResourceVersionString : 11.00.5058 RootDirectory : C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL SqlCharSet : 1 SqlCharSetName : iso_1 SqlSortOrder : 52 SqlSortOrderName : nocase_iso VersionMajor : 11 VersionMinor : 0 VersionString : 11.0.5058.0 Parent : [sqldev] Version : 11.0.5058 EngineEdition : EnterpriseOrDeveloper ResourceVersion : 11.0.5058 BuildClrVersion : 4.0.30319 Urn : Server[@Name='DEV-DB1']/Information Properties : {...properties here...} UserData : State : Existing
打开跟踪:
trace-command -Name ParameterBinding -PSHost -Expression {invoke-sqlcmd -Query $q1 -ServerInstance $serverObject -ConnectionTimeout 3 -OutputSqlErrors $true -QueryTimeout 3 -SuppressProviderContextWarning -IgnoreProviderContext}
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Invoke-Sqlcmd]
DEBUG: ParameterBinding Information: 0 : BIND arg [select * from currency] to parameter [Query]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : Executing VALIDATION metadata:
[System.Management.Automation.ValidateNotNullOrEmptyAttribute]
DEBUG: ParameterBinding Information: 0 : BIND arg [select * from currency] to param [Query] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [[sqldev]] to parameter [ServerInstance]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Management.Automation.PSObject]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [[sqldev]] to param [ServerInstance] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [3] to parameter [ConnectionTimeout]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Int32]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [3] to param [ConnectionTimeout] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to parameter [OutputSqlErrors]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Boolean]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to param [OutputSqlErrors] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [3] to parameter [QueryTimeout]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Int32]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [3] to param [QueryTimeout] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to parameter [SuppressProviderContextWarning]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Management.Automation.SwitchParameter]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to param [SuppressProviderContextWarning] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to parameter [IgnoreProviderContext]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Management.Automation.SwitchParameter]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : BIND arg [True] to param [IgnoreProviderContext] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Invoke-Sqlcmd]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Invoke-Sqlcmd]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Out-Default]
DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.Management.Automation.ErrorRecord]
DEBUG: ParameterBinding Information: 0 : RESTORING pipeline parameter's original values
DEBUG: ParameterBinding Information: 0 : Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [Value cannot be null.
Parameter name: ServerInstance] to parameter [InputObject]
DEBUG: ParameterBinding Information: 0 : BIND arg [Value cannot be null.
Parameter name: ServerInstance] to param [InputObject] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Out-Default]
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Out-LineOutput]
DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.Management.Automation.ErrorRecord]
DEBUG: ParameterBinding Information: 0 : RESTORING pipeline parameter's original values
DEBUG: ParameterBinding Information: 0 : Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [Value cannot be null.
Parameter name: ServerInstance] to parameter [InputObject]
DEBUG: ParameterBinding Information: 0 : BIND arg [Value cannot be null.
Parameter name: ServerInstance] to param [InputObject] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [out-lineoutput]
DEBUG: ParameterBinding Information: 0 : BIND PIPELINE object to parameters: [Format-Default]
DEBUG: ParameterBinding Information: 0 : PIPELINE object TYPE = [System.Management.Automation.ErrorRecord]
DEBUG: ParameterBinding Information: 0 : RESTORING pipeline parameter's original values
DEBUG: ParameterBinding Information: 0 : Parameter [InputObject] PIPELINE INPUT ValueFromPipeline NO COERCION
DEBUG: ParameterBinding Information: 0 : BIND arg [Value cannot be null.
Parameter name: ServerInstance] to parameter [InputObject]
DEBUG: ParameterBinding Information: 0 : BIND arg [Value cannot be null.
Parameter name: ServerInstance] to param [InputObject] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [format-default]
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : BIND arg [1] to parameter [Version]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ArgumentToVersionTransformationAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: 1.0
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Version]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : Executing VALIDATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ValidateVersionAttribute]
DEBUG: ParameterBinding Information: 0 : BIND arg [1.0] to param [Version] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : BIND arg [1] to parameter [Version]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ArgumentToVersionTransformationAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: 1.0
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Version]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : Executing VALIDATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ValidateVersionAttribute]
DEBUG: ParameterBinding Information: 0 : BIND arg [1.0] to param [Version] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : BIND arg [1] to parameter [Version]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ArgumentToVersionTransformationAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: 1.0
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Version]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : Executing VALIDATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ValidateVersionAttribute]
DEBUG: ParameterBinding Information: 0 : BIND arg [1.0] to param [Version] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : BIND arg [1] to parameter [Version]
DEBUG: ParameterBinding Information: 0 : Executing DATA GENERATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ArgumentToVersionTransformationAttribute]
DEBUG: ParameterBinding Information: 0 : result returned from DATA GENERATION: 1.0
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.Version]
DEBUG: ParameterBinding Information: 0 : Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 : Executing VALIDATION metadata:
[Microsoft.PowerShell.Commands.SetStrictModeCommand+ValidateVersionAttribute]
DEBUG: ParameterBinding Information: 0 : BIND arg [1.0] to param [Version] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Set-StrictMode]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
invoke-sqlcmd : Value cannot be null.
Parameter name: ServerInstance
At C:\code\platform\DrWorkerService\src\PowershellScripts\Untitled3.ps1:26 char:59
+ trace-command -Name ParameterBinding -PSHost -Expression {invoke-sqlcmd -Query $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Invoke-Sqlcmd], ArgumentNullException
+ FullyQualifiedErrorId : CannotGetServerInstance,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand