【问题标题】:Why do I receive the error "Args didn't Validate"?为什么我会收到错误“Args 未验证”?
【发布时间】:2016-03-18 16:28:22
【问题描述】:

我目前正在尝试编写一个 powershell 脚本,它将:

1.) 从指定目录中获取文件夹名称。

2.) 将该文件夹名称存储到变量 $i.Name

3.) 使用参数将该变量放入我的 cmdline 序列中并运行 .exe。

这是我开始编写的脚本:

Set-ExecutionPolicy unrestricted -Force
#copy directories and files to new destination    
#cpi C:\localApps\AutomationTest\Analysis\2016\*  C:\localApps\AutomationTest\Loading -Recurse
Get-ChildItem C:\localApps\AutomationTest\Loading | where {$_.PSIsContainer} | Write-Host
foreach($i in Get-ChildItem C:\localApps\AutomationTest\Loading)
{
   if ($i.PSisContainer) {$i.Name}

   $CMD = 'C:\Program Files (x86)\Analysis 4.2\LoadAnalysis4.exe'
   $arg1 = '-database:"Data Source=ABC-SQL-DB123\MSSQLSERVER_DB;Initial Catalog=Analysis4;Automated Security=True;MultipleActiveResultsSets=True;Application Name=LoadAnalysis4.exe"'
   $arg2 = '-directory:"C:\localApps\AutomationTest\Loading\$i"'
   $arg3 = '-wsdir:"C:\Automation\AnalysisWSFolder"'
   $arg4 = '-tempdir:"C:\tmp"'
   $arg5 = '-minws'
   $arg6 = '-name:"$i"'
   $arg7 = '-config:"C:\Program Files (x86)\Analysis 4.2\AnalysisLoadConfigs\stnd-with-abc-testing.xml"'

   & $CMD $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7

}

以下是我收到的结果:

TestFolder1  
TestFolder1
Args didn't Validate
Error: The following unknown parameters were specified: 'Source=ABC-SQL-DB123\MSSQLSERVER_DB;Initial';
'Catalog=Analysis4;Automated'; 'Security=True;MultipleActiveResultsSets=True;Application';
'Name=LoadAnalysis4.exe'; 'Files'; '(x86)\Analysis'; '4.2\AnalysisLoadConfigs\stnd-with-abc-testing.xml'

LoadAnalysis4.exe completed, elapsed: 00:00:00.123456

似乎我的 Args 中有间距问题。寻找一种让这些 Args Validated 和脚本正常工作的好方法。

我想结束这个问题。因此,对于像我这样很少使用 PowerShell 的新手来说,这最终是如何工作的......

 foreach($i in Get-ChildItem C:\localApps\AutomationTest\Loading)
 {
    if ($i.PSisContainer) {$i.Name}

    $CMD = 'C:\Program Files (x86)\Analysis 4.2\LoadAnalysis4.exe'
    $arg1 = "-database:Data Source=ABC-SQL-DB123\MSSQLSERVER_DB;InitialCatalog=Analysis4;Automated Security=True;MultipleActiveResultsSets=True;Application Name=LoadAnalysis4.exe"
    $arg2 = "-directory:C:\localApps\AutomationTest\Loading\$i"
    $arg3 = "-wsdir:C:\Automation\AnalysisWSFolder"
    $arg4 = "-tempdir:C:\tmp"
    $arg5 = "-minws"
    $arg6 = "-name:$i"
    $arg7 = "-config:C:\Program Files (x86)\Analysis  4.2\AnalysisLoadConfigs\stnd-with-abc-testing.xml"


    & $CMD $arg1 $arg2 $arg3 $arg4 $arg5 $arg6 $arg7

 }

我将"" 放在所有参数周围,将'' 放在$CMD exe 周围。

【问题讨论】:

  • 这似乎是相当于linux shell“分词”的powershell外部命令。嵌入空间被视为参数分割空间,而不是字符串的一部分。
  • @PetSerAl 我输入了get-host|Select-Object version,上面写着4.0。
  • & $CMD '--%' $arg1 ...;单引号字符串也不会扩展变量。
  • 好的,我正在研究一个引用// The Call Operator & 的示例我发现这篇文章PowerShell Call Operator 也许这个链接会对我有所帮助。
  • 如果您想使用参数启动进程,请尝试使用Start-Process $cmd -ArgumentList $args

标签: powershell


【解决方案1】:

尝试将参数作为数组传递:

foreach($i in Get-ChildItem -Path 'C:\localApps\AutomationTest\Loading')
{
    if($i.PSisContainer){$i.Name}

    $CMD = 'C:\Program Files (x86)\Analysis 4.2\LoadAnalysis4.exe'
    $Arguments = @(
        '-database:Data Source=ABC-SQL-DB123\MSSQLSERVER_DB;InitialCatalog=Analysis4;Automated Security=True;MultipleActiveResultsSets=True;Application Name=LoadAnalysis4.exe',
        "-directory:C:\localApps\AutomationTest\Loading\$i",
        '-wsdir:C:\Automation\AnalysisWSFolder',
        '-tempdir:C:\tmp',
        '-minws',
        "-name:$i",
        '-config:C:\Program Files (x86)\Analysis  4.2\AnalysisLoadConfigs\stnd-with-abc-testing.xml'
    )

    & $CMD $Arguments
}

参考:

【讨论】:

  • 我将尝试将参数作为数组并查看该链接。
  • @Twdeveloper 你应该先尝试回答并接受它,如果它以后工作;)。
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 2013-01-05
  • 2012-01-30
  • 2020-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-11
相关资源
最近更新 更多