I want to run a script from commandline, which takes in a file directory as one of the parameters. The files are log files, which are postfixed by a date in following format: yyymmdd (Get-Date -Format filedate). I want to get the latest log file to be passed into the pwoershell script. e.g logfile-20210928.json
> e.g powershell.exe -NonInteractive -NoProfile -ExecutionPolicy
> RemoteSigned -File "D:\someapp\checkfile.ps1" -Path
> 'D:\logs\logfile-$(Get-Date -Format filedate).json'
I get the following error as powershell thinks -Format is a parameter.
A positional parameter cannot be found that accepts argument '-Format'.
CodePudding user response:
You are posting your 'Get-Date' part in single quotes, the variable is therefor plain text. try:
"D:\logs\logfile-$(Get-Date -Format filedate).json"
Since you are using cmd to open the file you can not use powershell's Get-Date function in the commandline, you can try:
D:\logs\logfile-%date:~-4,4%%date:~-7,2%%date:~-10,2%.json
So the .bat file will be:
powershell.exe -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -File "D:\someapp\checkfile.ps1" -Path "D:\logs\logfile-