You are not logged in.
Pages: 1
I've scaled down the relevant pieces into one little test script. Essentially, I'm defining a log file path/name in "$LOGFILE" and then in the Initialize-DebugLog function, if it is found, I delete the old one first, then I create a new file to start as a log file. It is not creating the file or logging the message to it. I had a slight variation of this working before, but now that I'm refactoring it, I'm having some issues.
Any ideas why the file isn't being created?
(Please disregard all the Write-Host debugging lines).
Function Initialize-DebugLog
{
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "Logfile=[$LOGFILE]"
if (Test-Path -LiteralPath $LOGFILE -PathType leaf)
{
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "Log file already exists."
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "Deleting and initializing a new log file."
Remove-Item -LiteralPath $LOGFILE -Force -Verbose -ErrorVariable removeErr
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkMagenta" $removeErr
}
else
{
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "Log file does not exist."
}
$baseFile = Split-Path $LOGFILE -leaf
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "Log file name: $baseFile"
$logPath = Split-Path $LOGFILE
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "Log file Path: $logPath"
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "Creating: $LOGFILE"
new-item -Path $logPath -name $baseFile -itemType "File" -force -ErrorVariable addErr
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkMagenta" $addErr
Write-Host -ForegroundColor "Yellow" -BackgroundColor "DarkBlue" "==Debug Log Initialized=="
}
$debugscript=$true
$LOGFILE = "$PSSCRIPTROOT\test.log"
Initialize-DebugLog
$myString = "This is some text to log"
if (Test-Path -LiteralPath $LOGFILE -PathType leaf)
{
Add-Content $LOGFILE -value $myString
}
Here is the output I'm seeing on screen:
Offline
The $PSScriptRoot variable doesn’t exist in PowerShell 2 and the log file is being written to C:\test.log
Offline
Your script works for me in PowerShell 5.1
Offline
The $PSScriptRoot variable doesn’t exist in PowerShell 2 and the log file is being written to C:\test.log
Nope...you can see from the first debug print out that the directory is on the front of the file name. That being said...I double checked and there is no file created in c:\ root.
Your script works for me in PowerShell 5.1
There isn't anything version specific in there, is there? I just checked and I'm using PS V3.0 on this computer. Although the machines it will eventually run on seem to have v5.1. Maybe I should upgrade. Is PS a manual upgrade?
Offline
Oops. Sorry to lead you down the wrong path. I put two and two together noticing that the window style in the image is reminiscent of Windows 7 and seeing $PSScriptRoot.
On closer inspection, it seems that New-Item fails because the path given to the `-Path` parameter contains brackets. PowerShell can often be a little… unpredictable when paths contain brackets. Usually a `-LiteralPath` would solve this problem but New-Item doesn’t support a `-LiteralPath` parameter.
One workaround would be to just change the current working directory to the target folder before issuing the New-Item command and omit the `-Path` parameter. Alternatively, you can leverage a .Net call to create the file:
[System.IO.File]::Create($LOGFILE)
Last edited by Pyprohly (15 Jun 2018 16:57)
Offline
Oops. Sorry to lead you down the wrong path. I put two and two together noticing that the window style in the image is reminiscent of Windows 7 and seeing $PSScriptRoot.
On closer inspection, it seems that New-Item fails because the path given to the `-Path` parameter contains brackets. PowerShell can often be a little… unpredictable when paths contain brackets. Usually a `-LiteralPath` would solve this problem but New-Item doesn’t support a `-LiteralPath` parameter.
One workaround would be to just change the current working directory to the target folder before issuing the New-Item command and omit the `-Path` parameter. Alternatively, you can leverage a .Net call to create the file:
[System.IO.File]::Create($LOGFILE)
Thanks...that sounds logical. I'll play with it keeping the brackets in mind. I was beginning to wonder if it was somehow a Onedrive weirdness issue, but the brackets makes more sense. I can easily just rename the directory to find out.
Right now I'm in the middle of upgrading to PowerShell 5.1, so I can see if that does anything different too.
BTW...it IS Windows 7. We're stuck on Win7 because of issues with the version of Clearcase that we use or something like that. Hoping they'll change that in the future and let me get to Win10 like I use at home.
Offline
OK....thank you Pyprohly. You nailed it. It was the brackets in the directory name that was killing it.
Offline
Actually… if you include the file name in `-Path` and avoid using `-Path` and `-Name` together it should work:
new-item -Path $LOGFILE -itemType "File" -force -ErrorVariable addErr
;p
Offline
Pages: 1