Debugging a simple fixlet with a PowerShell script

I’m sure I’m missing something simple here, but I can’t seem to get past this section of a createfile.

createfile until end

$scriptPath = 'C:\ta_fss'
$logfile = 'cleanup.log'
$dirs = @("C:\ta_fss", "C:\ta_staging")
$exts = @('*.log', '*.txt', '*.install*')
$psver = $PSVersionTable.PSVersion.Major

Start-Transcript "$scriptPath\$logfile" -Append -Force -NoClobber -Verbose

if ($psver -lt 5) {
    foreach ($dir in $dirs) {
        if (Test-Path -Path $dir -PathType Container) {
            Get-ChildItem -Path $dir -Exclude $exts -Recurse | Where-Object { ! $_.PSIsContainer } | Remove-Item -Recurse -Verbose
        }
    }
} else {
    foreach ($dir in $dirs) {
        if (Test-Path -Path $dir -PathType Container) {
            Get-ChildItem -Path $dir -Exclude $exts -File -Recurse | Remove-Item -Recurse -Verbose
        }
    } 
}

Stop-Transcript

end

I am attempting to create a simple PowerShell script, but keep getting an error on this line:
Get-ChildItem -Path $dir -Exclude $exts -Recurse | Where-Object { ! $_.PSIsContainer } | Remove-Item -Recurse -Verbose

I’ve tried escaping the curly brackets, but that doesn’t seem to work. What am I missing?

The curly-bracket escaping can be confusing, as to where escaping is needed. Have a read-through of the tip at Tip: Escaping curly brackets for substitutions in ActionScript and if that doesn’t help, please post the actionscript with the escaping here.

1 Like

Thanks for replying so quickly @JasonWalker.

It would seem that escaping the first set of curly braces should fix the issue, but when debugging the actionscript in the debugger, I keep getting an error. The issue seems to be with the nested if statements in the PowerShell script:

if ($psver -lt 5) {{
    foreach ($dir in $dirs) {
        if (Test-Path -Path $dir -PathType Container) {
	         Get-ChildItem -Path $dir -Exclude $exts -Recurse | Where-Object { ! $_.PSIsContainer } | Remove-Item -Recurse -Verbose
        }
    }
} else {
    foreach ($dir in $dirs) {
        if (Test-Path -Path $dir -PathType Container) {
            Get-ChildItem -Path $dir -Exclude $exts -File -Recurse | Remove-Item -Recurse -Verbose
        }
    } 
}

I’ve tried escaping the first if block, the foreach block, and the second if block, and none seem to work.

Ok, on this one it looks like none of the open curly brackets intends to start a relevance substitution, so every { should be changed to {{

1 Like

That didn’t seem to work either. I ended up just creating a physical script and prefetching it.

EDIT: A word.

I’m glad you got it working, but I still think we should have been able to create the PS script on the fly; I’m doing that in a number of my actions already. If you aren’t too frustrated with it already I’d like to see the actionscript version that failed.

Hi Jason,
Actually, what I posted in the initial post is what was failing.

I too would like to find a fix for this, because it should be working.

Thanks

The end in -append is triggering the createfile until end Try this

createfile until EOF

$scriptPath = 'C:\ta_fss'
$logfile = 'cleanup.log'
$dirs = @("C:\ta_fss", "C:\ta_staging")
$exts = @('*.log', '*.txt', '*.install*')
$psver = $PSVersionTable.PSVersion.Major

Start-Transcript "$scriptPath\$logfile" -Append -Force -NoClobber -Verbose

if ($psver -lt 5) {{
    foreach ($dir in $dirs) {{
        if (Test-Path -Path $dir -PathType Container) {{
            Get-ChildItem -Path $dir -Exclude $exts -Recurse | Where-Object {{ ! $_.PSIsContainer } | Remove-Item -Recurse -Verbose
        }
    }
} else {{
    foreach ($dir in $dirs) {{
        if (Test-Path -Path $dir -PathType Container) {{
            Get-ChildItem -Path $dir -Exclude $exts -File -Recurse | Remove-Item -Recurse -Verbose
        }
    } 
}

Stop-Transcript

EOF
1 Like

Your first post had none of the { symbols escaped as {{, your next post had only the first one escaped…can you post one with all the escapes…

NEVERMIND there’s also a totally different issue I see.

The createfile until end is too generic. It looks for the token ‘end’ anywhere. ‘end’ doesn’t have to be on its own line to match, and it doesn’t even have to match a whole word.

The ‘end’ in ‘Append’ here would match and try to close the ‘createfile’ statement. Try using something that won’t appear in the powershell script - I usually do something like ‘createfile until EOF_EOF’

3 Likes

Hi Jason,
That did it. I didn’t realize that the createfile until looks for just that word. Makes sense though.

I also didn’t realize that it doesn’t help to paste just the createfile section into the debugger. I tried that with the changes initially, and it was still giving me errors (in the debugger). As soon as I pasted the entire actionscript into the debugger, it passed with flying colors.

Thanks again.

1 Like