Powershell参数验证问题

时间:2012-09-13 17:38:30

标签: powershell parameters arguments

我从另一个脚本中调用了类似的PowerShell脚本。

function test_execute_sql_2_direct_query()
  {
    & C:\Server\Scripts\PowerShellDeploy\Execute-SQL-Command.ps1 -SERVER_NAME "myserver" -SQL_COMMAND_TYPE "input" -SQL_FILE ".\select.sql" -USER_NAME "username" -PASSWORD "password" -DATABASE "accounts"
  }

当我调用该函数时,我从Powershell收到一条错误消息,指出'无法验证参数'Query'的参数。参数为null或空....'

我不太理解为什么我收到这条消息? Execute-SQL-Command脚本没有名为“Query”的参数。很可能我错过了什么。

这是它的参数部分:

param(
[Parameter(
         HelpMessage="Server Name")]
[string]$SERVER_NAME = "",

[Parameter(
         HelpMessage="Database")]
[string]$DATABASE = "",

    [Parameter(
         HelpMessage="Path to .SQL File to run. Used in input file mode (not direct query mode)")]
[string]$SQL_FILE ="",  

    [Parameter(
         HelpMessage="Server Port (default = 1433)")]
[string]$SERVER_PORT = "1433",

    [Parameter(
         HelpMessage="Server Transport (default = tcp")]
[string]$TRANSPORT = "tcp", 

[Parameter(
         HelpMessage="Method of connecting to database <passthrough, credentialed>")]
[string]$CONNECT_METHOD = "passthrough",

[Parameter(
        HelpMessage="Username -- used only for credentialed login")]
[string]$USER_NAME = "username",

[Parameter(
        HelpMessage="Command type-- direct query or input file (.sql)")]
[string]$SQL_COMMAND_TYPE = "DIRECT_QUERY",

[Parameter(
        HelpMessage="SQL statement text type-- used in direct queries")]
[string]$SQL_STATEMENT = "",

[Parameter(
        HelpMessage="Password -- used only for credentialed login")]
[string]$PASSWORD = "pwd")

1 个答案:

答案 0 :(得分:0)

这个问题归结为用户错误 - 实际错误来自Execute-SQL-Command.ps1中调用的行 - 特别是Invoke-Sqlcmd cmdlet。