I am trying to use Out-File to have a date variable within the name for the file. But, getting an exception as below
$EndDate = get-date -UFormat "%Y-%m-%d %H:%M:%S"
Out-File -FilePath "C:\output\$host-$EndDate.json
Out-File : The given path's format is not supported.
At E:\sample.ps1:442 char:69
+ ... MaxValue) | Out-File -FilePath "E:\output\$serverHostName ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (:) [Out-File], NotSupportedException
+ FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
Instead of
Out-File -FilePath "C:\output\$host-$EndDate.json"
Make use of the format (-f) operator:
$EndDate = get-date -UFormat "%Y-%m-%d %H:%M:%S"
$myFileName = 'C:\output\{0}-{1}.json' -f $host, ($EndDate.ToString() -replace ':','') -replace '\s',''
Out-File -filepath $myFileName
Also, you may need to use the New-Item command instead of Out-File, if your directory doesn't already exist. Your error seems to indicate that the directory may not be available when creating the file.
New-Item -path $myFileName -Force
As Bill Stewart pointed out, $host
is a reserved word, so be careful using it.