gpt4 book ai didi

powershell - powershell 中找不到位置参数错误

转载 作者:行者123 更新时间:2023-12-01 19:36:15 25 4
gpt4 key购买 nike

我试图将日期附加到我的 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"

最佳答案

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 来确保我的文件路径正确连接。

关于powershell - powershell 中找不到位置参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54189025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com