在Powershell中找不到位置参数错误

时间:2019-01-14 20:53:00

标签: powershell

我正在尝试在Powershell脚本中将日期附加到文件名中,但始终出现以下错误(我的代码在错误下方)。任何帮助/方向将不胜感激。谢谢。

Set-Content : A positional parameter cannot be found that accepts 

argument '$null'.
At P:\CoverageVerifier\CombineTextFiles.ps1:8 char:50
+ ... thTrailer | Set-Content "${path}\\" + ${$dateStr} + "_CoverageVerifi ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Content], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetContentCommand

这是我的powershell代码:

$path = "\\xx\\apps\\CoverageVerifier\\"
$pathHeader = "\\xx\\apps\\CoverageVerifier\\Header.txt"
$pathData = "\\xx\\apps\\CoverageVerifier\\Data.txt"
$pathTrailer = "\\xx\\apps\\CoverageVerifier\\Trailer.txt"
$date = Get-Date
$dateStr = $date.ToString("yyyyMMdd")
#Write-Output $dateStr
Get-Content $pathHeader,$pathData,$pathTrailer | Set-Content "${path}\\cvgver." + ${dateStr} + ".0101"

3 个答案:

答案 0 :(得分:4)

Set-Content cmdlet的第一个位置参数是-Path参数。 由于定义文件路径的方式一定会导致问题。

据我了解,这些是UNC路径,因此请尝试以下操作:

# for LOCAL paths
# Set the driveletter to the actual drive you are using. For demo I'm using 'X:\'
# $path        = 'X:\apps\CoverageVerifier'
# for UNC paths
# change 'servername' to your actual servers name
$path        = '\\servername\apps\CoverageVerifier'

$pathHeader  = Join-Path -Path $path -ChildPath 'Header.txt'
$pathData    = Join-Path -Path $path -ChildPath 'Data.txt'
$pathTrailer = Join-Path -Path $path -ChildPath 'Trailer.txt'
$dateStr     = (Get-Date).ToString("yyyyMMdd")
$outFile     = Join-Path -Path $path -ChildPath ($dateStr + "_TestVerifier.txt")

Get-Content $pathHeader, $pathData, $pathTrailer | Set-Content $outFile

如您所见,我经常使用Join-Path cmdlet来确保我的文件路径正确连接。

答案 1 :(得分:2)

为什么不使用格式运算符和Join-Path?
即使仅使用一种格式,也可以编辑

$path =        "\\xx\apps\CoverageVerifier"
$pathHeader =  Join-Path $path "Header.txt"
$pathData =    Join-Path $path "Data.txt"
$pathTrailer = Join-Path $path "Trailer.txt"

Get-Content $pathHeader,$pathData,$pathTrailer | 
  Set-Content (Join-Path $path ("{0:yyyyMMdd}_TestVerifier.txt" -f (Get-Date))

答案 2 :(得分:0)