Link to home
Start Free TrialLog in
Avatar of janhoedt
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
### 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
    ################################

Open in new window

Avatar of oBdA
oBdA

You're either pasting that directly into the console to test it, or you're running it in the ISE without saving it first.
You can only use $MyInvocation in this way if an actual script is executed.
ASKER CERTIFIED SOLUTION
Avatar of janhoedt
janhoedt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of janhoedt

ASKER

Works for me an no other input received on that what helped me.