Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Parse a line and report if unusual in Powershell

$
0
0

Hi guys,

We have about 800 Servers. On each Server, there is a file that may or may not be wrong. We are expecting one line that contains the word "shutdown.exe" in there - then it would be fine. If there is MORE than this line, we want to extract that supplemental line(s) and add it to another file.

I wrote this code, which seemed to work fine at first:

gc ($Env:temp + "\reboot\Reboot.bat") | ? {$_ -notlike "shutdown.exe *"} | % {$_| Out-File ($Env:temp + "\StopSRV\stop01.cmd") -Append}

So I guessed this would read any line that does not contain "shutdown.exe" in file "reboot.bat", then append this text to "stop01.cmd". Background is that we used the wrong reboot file for years, and want to correct that now. However, we do not know if anybody added Tasks to the existing reboot script.

So when I played around with this, I noticed that it would eventually mess up the Destination file "stop01.cmd". As it had text in there before, it would suddently print the existing text in all weird characters.

Also I added two test lines to the src file reboot.bat:

that is on the second line

shutdown.exe -r -f -t 5

that is on the sixth line

This is how it looks like when it's appended to the stop01.cmd:

@echo off
REM Add shutdown commands here. For example:
REM net stop "Service Name" /y
t h a t   i s   o n   t h e   s e c o n d   l i n e 
t h a t   i s   o n   t h e   s i x t h   l i n e 
That spacing is very irritiating, and when opening with Notepad, it looks like the two lines are on the same line as the second REMed line.

What disturbed me a bit as well is that we don't know which Servers contain a changed reboot file after that. So by now I would be happy if I could run the script and it would Report back to a central Folder the Server Name where the file is not as expected. I am not expecting more than 15 Servers or so to be altered, so with reporting that, I could also document which Server is different. Any ideas how to Change that script to accomplish that?

Thanks!

Best regards,

Cap'


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>