0

I have a PowerShell script that logs certain information to a file via the Out-File command:

"text to be logged" | Out-File $logFile -Append

When the PowerShell script is run directly, the log file is created/updated without any problems. This PowerShell script is called through a batch script (due to a tool I am utilizing that only accepts batch scripts as input) through the following command:

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%powerShellScriptPath%"""" """"%argOne%"""" """"%argTwo%""""' -Verb RunAs}";

The command I am implementing above is based on the implementation found in this answer.

When called through the batch script, the log file never gets created/updated. The PowerShell script performs all of its duties behind the scenes without issues, it simply fails to create the log file or populate it with data...

ITSUUUUUH
  • 189
  • 1
  • 3
  • 18
  • Do you run the script as a different user? – iRon Jul 31 '19 at 11:26
  • @iRon This would be run as the same user. – ITSUUUUUH Jul 31 '19 at 11:35
  • 2
    Your script started with runas will presumably have a current folder of `C:\Windows\System32` If $logFile isn't fully qualified it may be trying to write there. –  Jul 31 '19 at 11:44
  • @LotPings You're right, its been appending at this location... – ITSUUUUUH Jul 31 '19 at 11:49
  • I want to use the path that i was specifying in the $logFile variable; however. I see what you are saying due to the way i am concatenating my final path: `$logPath = "$($mainPath)\sample.log"` - where $mainPath is Get-Location. This would need to be modified to be a hardcoded location to place it in to expected location – ITSUUUUUH Jul 31 '19 at 12:06
  • 2
    It's unclear *where* you set the variable. If the var contains a drive and path there is no ambiguity. In case the path to write to is the same as the script location you can use `Set-Location $PSScriptRoot` in the script –  Jul 31 '19 at 12:06

0 Answers0