janhoedt
asked on
PS transcript logging in script not working "path is null"
Hi,
I cannot seem to make this transcript logging (which I want to add to my scripts running as scheduled taks, working).
It should create a logs folder (when not existing) and log to a folder where the script runs. Cleanup is also included.
Whatever I do, I always get "Split-Path : Cannot bind argument to parameter 'Path' because it is null."
Please advise.
J.
This is the code
I cannot seem to make this transcript logging (which I want to add to my scripts running as scheduled taks, working).
It should create a logs folder (when not existing) and log to a folder where the script runs. Cleanup is also included.
Whatever I do, I always get "Split-Path : Cannot bind argument to parameter 'Path' because it is null."
Please advise.
J.
This is the code
### START TRANSCRIPT LOGGING ###
Stop-Transcript -ErrorAction Ignore #if it runs, stop it
#$ScriptPath = (Split-Path ((Get-Variable MyInvocation).Value).MyCommand.Path)
$ScriptPath = (Split-Path $MyInvocation.MyCommand.Path) # + "\"
$Logfilename = $MyInvocation.MyCommand
$LogfileLocation = "$ScriptPath\Logging"
if (!$LogfileLocation) {New-Item -Type directory -Path $LogfileLocation -force -ea 0}
$files = Get-ChildItem $LogfileLocation | sort LastWriteTime
if($files.Count -gt 10){remove-item ($LogfileLocation + '\' + $files[0].Name) -Force}
Start-Transcript -Path "$LogfileLocation\$LogfileName-$([datetime]::Now.ToString('dd-MM-yyyy hh-mm')).log" -IncludeInvocationHeader
################################
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works for me an no other input received on that what helped me.
You can only use $MyInvocation in this way if an actual script is executed.