Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在powershell中找不到位置参数错误_Powershell - Fatal编程技术网

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

在powershell中找不到位置参数错误,powershell,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}\\"

我正在尝试将日期附加到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"

您可以使用“设置内容”来修改文件的内容,而不是文件名:


设置内容
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来确保正确连接文件路径。

为什么不使用格式运算符和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))

你为什么把所有的反斜杠都加倍?这些是UNC路径吗?是的,它们是网络上特定服务器的UNC路径。对不起我的无知。谢谢,谢谢,里昂。您知道如何修改文件名吗?重命名项目:谢谢Theo。感谢您的时间和帮助。我试试,谢谢你,提奥。明白了!!谢谢你的时间和帮助。这对我也很管用。