PS script doesn't rename file.

JRV 551 Reputation points
2022-05-19T22:06:00.03+00:00

Background: A PS script running on each workstation drops a log file (a text file) in a shared folder. If the log has any error conditions, the file has a *.error file extension.

Another script runs on the file server via Scheduled Task, and examines the *.error files for further processing and reporting. Most *.error files are left alone, but if specific text is found in a file, it renames the extension to *.RestartNeeded or *.IconUnreadable.

Line 3 renames the *.Error file to *.RestartNeeded and works perfectly.

Line 7 renames the *.Error file to *.IconUnreadable and it does not work at all.

But, save for the file extension, they are the exact same line of code!

$IgnoreErrors = Select-String -Path "$LogFolder\*.Error" -Pattern "ErrorArray=\|\|\|\|" |`
    Select-String -Path { $_.Path } -Pattern 'StatusText=0'
$IgnoreErrors | Rename-Item -NewName { $_.Path -replace '.Error', '.RestartNeeded' }

$ErrorArray = "ErrorArray=\|\$mSysTray_Handle - System tray item not found: 0x0000000000000000\|\$mSysTray_Handle_Hidden - System tray overflow notification icon not found: 0x0000000000000000\|\$mSysTray_ButCount - No items found in system tray: 0x0000000000000000\|\$mSysTray_ButCount_hidden - No items found in overflow system tray: 0x0000000000000000"
$IgnoreErrors = Select-String -Path "$LogFolder\*.Error" -Pattern $ErrorArray
$IgnoreErrors | Rename-Item -NewName { $_.Path -replace '.Error', '.IconUnreadable' }

When I run the script, I get output that shows the correct log files are matching the filters, and I do not get any errors.

The run-as account has NTFS & share permissions to rename files, and in fact, is able to do so in Line 3. And, again, I don't get any errors when I run the script.

The script processes these files correctly on one WS2019 system. The exact same script has this problem on 2 others running WS2012 & WS2012 R2. OS probably doesn't matter but who knows?

What am I missing?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2022-05-21T02:42:33.883+00:00

    This code (using single-quotes around the string loaded into $ErrorArray) works:

    $ErrorArray = 'ErrorArray=\|\$mSysTray_Handle - System tray item not found: 0x0000000000000000\|\$mSysTray_Handle_Hidden - System tray overflow notification icon not found: 0x0000000000000000\|\$mSysTray_ButCount - No items found in system tray: 0x0000000000000000\|\$mSysTray_ButCount_hidden - No items found in overflow system tray: 0x0000000000000000'
    
    $IgnoreErrors = Select-String -Path c:\junk\*.Error -Pattern $ErrorArray
    $IgnoreErrors.Path
    

    The value of the "Path" property was:

    C:\junk\Elog.Error

    Here's the content of the file I tested with:

    ErrorArray=|$mSysTray_Handle - System tray item not found: 0x0000000000000000|$mSysTray_Handle_Hidden - System tray overflow notification icon not found: 0x0000000000000000|$mSysTray_ButCount - No items found in system tray: 0x0000000000000000|$mSysTray_ButCount_hidden - No items found in overflow system tray: 0x0000000000000000
    
    1 person found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-05-20T02:54:38.613+00:00

    Your problem may be that you're using double quotes to surround your regex. Because of that, variable interpolation is enabled and the "$mSysTray_ButCount" is seen as a variable name and will be replace by whatever value is in that variable -- or an empty sting if there is no such variable.

    To fix that problem, use single quotes to surround the string. That will disable variable interpolation.

    You've correctly used "\" to escape the "$" character in the regex, but that has no effect on the variable interpolation.

    Also, in the Rename-Item cmdlets you haven't escaped the "." in the regex. It should be: $_.Path -replace '\.Error', '.IconUnreadable' . It probably isn't affecting anything unless you have the string "error" in the Path property somewhere other than in the file extension.

    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2022-05-24T22:45:35.72+00:00

    Rename-Item takes just a NAME as the argument to the -NewName parameter. For example:

    $IgnoreErrors | Rename-Item -NewName { $_.Name -replace '\.Error', '.IconUnreadable' }
    
    1 person found this answer helpful.
    0 comments No comments

  3. MotoX80 36,401 Reputation points
    2022-05-20T00:42:22.143+00:00

    Are all machines running the same version of Powershell?

    Do you have the scheduled task configured to capture both stdout and stderr? See my replies in this thread.

    https://learn.microsoft.com/en-us/answers/questions/592895/running-task-scheduler-as-administator.html

    Or you could use powershell to generate a transcript to a log file. (If you aren't already doing that.)

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.host/start-transcript?view=powershell-7.2

    Beyond that I would suggest adding some troubleshooting displays to see if you can narrow down where the problem is. Display the total number of files that the script sees in the folder. Then the number of ".error" files. Finally display the number of .error files that have the matching string.

    "Powershell version is {0}" -f $PSVersionTable.PSVersion   
    "I see {0} files in {1}" -f (Get-ChildItem -path $LogFolder -file).count, $LogFolder   
    "I see {0} .error files in {1}" -f (Get-ChildItem -path $LogFolder\*.error).count, $LogFolder   
    "IgnoreErrors has {0} entries." -f $IgnoreErrors.count   
    
    0 comments No comments

  4. JRV 551 Reputation points
    2022-05-20T15:49:25.587+00:00

    Rich & Moto, thanks for your replies.

    I've not only used Start-Transcript, but the transcript showed no errors. And as I say, I've run it interactively and nothing errors out. The first rename works, the 2nd doesn't. But no errors posted.

    The script displays the value of $IgnoreErrors as it runs, and it's finding the correct files. That's not to say the filter text couldn't be improved by single-quotes, and I will do that...but it is finding the right files regardless of quotes.

    The first Rename-Item is renaming the file(s). The second is not. Again, escaping the "." is an improvement I will make, but it's not stopping the first one from working.

    PS version in all cases will be what shipped with the OS. We've never updated. I'm at the WS2012 R2 site today; PS version is--

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    4      0      -1     -1
    

    May take a while for me to reply; the 2 sites where this isn't working are every-other-week visits, and one of those (where I am today) usually has 10000 things for me to do. But I will implement your suggestions when I can and try it out. Just might not be today! And know that I do appreciate the help.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.