Get-Content出错

时间:2014-06-16 22:54:13

标签: powershell

我有以下部分代码:

$fileList | foreach {
Write-Host ". . . adding $_ to script"
$myCreateScript += Get-Content "$ScriptRoot\$_" | Out-String }

在执行结束时产生以下内容:

  

ItemNotFound,Microsoft.PowerShell.Commands.GetContentCommand   System.Exception:指定路径E:\ Scripts \ users [[FILE_NAME_LOWER]] _ administrator.sql中的对象不存在,或者已被-Include或-Exclude参数过滤。

[[FILE_NAME_LOWER]] _ administrator.sql是文件的名称,它确实存在于指定的目录中。如果这是一个文件名包含括号([])的问题,我需要知道如何在foreach块中解析它,因为我无法轻易更改文件名(它们在源代码控制中的代码中的其他地方使用)并且由于脚本中的先前代码,需要保存在$ fileList中。我怀疑括号是个问题,因为$ fileList有超过200行,并且$ myCreateScript到目前为止填充了正确的数据。

1 个答案:

答案 0 :(得分:9)

您需要使用Get-Content的{​​{3}}参数:

$myCreateScript += Get-Content -LiteralPath "$ScriptRoot\$_" | Out-String }

否则,[[FILE_NAME_LOWER]]将被解释为-LiteralPath,它告诉PowerShell匹配每端两个方括号所包含的任何字符。