You are not logged in.

#1 15 Jun 2018 01:18

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

New-Item not seeming to work...

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:
PSNew_Item.png

Offline

#2 15 Jun 2018 07:30

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: New-Item not seeming to work...

The $PSScriptRoot variable doesn’t exist in PowerShell 2 and the log file is being written to C:\test.log

Offline

#3 15 Jun 2018 11:43

Simon Sheppard
Admin
Registered: 27 Aug 2005
Posts: 1,130
Website

Re: New-Item not seeming to work...

Your script works for me in PowerShell 5.1

Offline

#4 15 Jun 2018 15:03

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Re: New-Item not seeming to work...

Pyprohly wrote:

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.

Simon Sheppard wrote:

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

#5 15 Jun 2018 16:55

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: New-Item not seeming to work...

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

#6 15 Jun 2018 18:33

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Re: New-Item not seeming to work...

Pyprohly wrote:

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. big_smile

Offline

#7 18 Jun 2018 17:16

kingtermite
Member
Registered: 28 Oct 2016
Posts: 18

Re: New-Item not seeming to work...

OK....thank you Pyprohly. You nailed it. It was the brackets in the directory name that was killing it.

Offline

#8 19 Jul 2018 06:59

Pyprohly
Member
Registered: 26 Nov 2014
Posts: 37

Re: New-Item not seeming to work...

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

Board footer

Powered by