2

I am trying to append the date to a file name in my Powershell script but keep getting the following error (my code is below the error). Any help/direction would be greatly appreciated. Thank you.

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

Here is my powershell code:

$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"
2
  • 1
    Why are you doubling all backslashes? Are these UNC paths? Commented Jan 14, 2019 at 21:03
  • Yes they are UNC paths to a specific server on the network. Sorry for my ignorance. Thanks. Commented Jan 14, 2019 at 21:05

3 Answers 3

4

The first positional parameter of the Set-Content cmdlet is the -Path parameter. Because of the way you define the paths for the files, it is bound to have problems with that.

As I understand, these are UNC paths, so try this instead:

# 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

As you can see, I'm using the Join-Path cmdlet a lot to make sure my file paths get concatenated correctly.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Theo. Appreciate your time and help. I'll try it.
Thank you Theo. That got it!!
2

Why don't you use the format operator and Join-Path?
Edit even with only one format

$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))

1 Comment

Thanks for your time and help too. This worked for me as well.
0

you use set-content to modify the content of a file, not the filename:

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-content?view=powershell-6

2 Comments

Thanks Leon. Do you know how to modify the file name?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.