Help with PowerShell Script - Newbie


Very much a newbie to PowerShell but have experience writing shell scripts (unsure if commands can be linked to one another). I am trying to write a script where I want to view a log file and get a count of a number of expired rows. In shell it would look something like this

cd <directory>
cat logfile | grep -i expired | wc -l

Something along those lines should give me a count of all the expired rows. Would the same work in powershell? I am unsure on how we could write it.

Apologies for the basic question!

Any help would be greatly appreciated.

welcome to the forum. :wave:t4:

Could you please choose a little more descriptive subject next time? 99.9% of the questions asked here are questions about PowerShell. :wink:

In my experience it does not make that much sense to “migrate” or “translate” shell scripts into PowerShell one command by one because PowerShell uses rich and powerful objects instead of stupid text. :wink:

So of course your task is quite easily achievable with PowerShell as well. You could use

to find the desired strings or patterns in a given file.

$Result = Select-String -Path 'C:\sample\logfile.log' -Pattern 'expired' -SimpleMatch 
1 Like

Thank you so much Olaf! Apologies for the simple header I will ensure it is more suitable next time!

The log files I want to view are hosted on another server (Linux Server to be exact) I can view these via a URL or obviously logging into the server itself, what would you suggest be the best way? Also how would this affect the command?

Well as usual … it depends. What would you like to do with the information you get? It will probably run faster when you run it locally but the result will be the same.

Sadly no it will not be a local file, it is remote, so would get the count and push it out to a text file that I would create.

Apologies for the shell below but as I say that’s mainly what I know :smiley: & to try and give an idea what I am trying to do

ssh <server>

cd <path>

cat logfile | grep -i expired | wc -l > somefile.txt

Hmmm … I don’t have any experience with linux server but I’m sure there is something like a task scheduler. If I recall it right it’s been named cron job or cron daemon in the past. Why don’t you run this task locally on that remote server and only read the result remotely from the local machine?

Unfortunately it isn’t my server so can only read into it, like I said there is a URL to view the logs so I could read via the web browser if easier?

You change the conditions with every reply. Or … what’s just as bad as this … you don’t provide all information.

Apologies Olaf don’t mean to be confusing and misleading.

So I can’t write anything on the server as it isn’t my server so I have to write the script remotely. I can connect into the server e.g. with SSH to view the log files… Or I can connect through a web browser and view the log files that way (I have uploaded a screenshot).

So I want to write a script to connect to the server or webpage and get a count of how many have “Expired”

I hope I have cleared up any confusion!

You still could ask the owner of the server to cooperate. :wink:

I’ve read about the possibility to use SSH with PowerShell to connect to Linux server but I’m not familiar with it. :man_shrugging:t4: You may search for more info about this topic either here or the PowerShellGallery or maybe StackOverflow.

You could try to download the file via Invoke-WebRequest and process it locally.

Hey Olaf,

So I have used the invoke web request as you have suggested and getting a connection to the log file :slight_smile:

$response = Invoke-WebRequest -Uri 'http://scrbampdk008253:2111/log/amps/JseCertReport.out'  -TimeoutSec 60

As stated this allows me to read into the log file

PS C:\Users\SA-CRBRVRBEDM01> $response.RawContent
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Security-Policy: default-src 'self'; script-src 'unsafe-inline'
Accept-Ranges: bytes
Content-Length: 7189
Date: Tue, 27 Jul 2021 09:30:55 GMT
Last-Modified: Tue, 27 Jul 2021 01:11:18 GMT
Server: Jetty(9.4.12.v20180830)

Certificate Unknown:bunn_key.pem:No date/Invalid date:Certificate Date=:Run Date=2021-07-27:Expiry Days=():3
Certificate Unknown:cargosmart_key.pem:No date/Invalid date:Certificate Date=:Run Date=2021-07-27:Expiry Days=():3
Certificate Expired:croatia_cert_10062021.pem::Certificate Date=2021-06-10:Run Date=2021-07-27:Expiry Days=(-47 days):1
Certificate Expired:croatia_key_10062021.pem::Certificate Date=2021-06-10:Run Date=2021-07-27:Expiry Days=(-47 days):1
Certificate Unknown:DBH_key.pem:No date/Invalid date:Certificate Date=:Run Date=2021-07-27:Expiry Days=():3
Certificate Fine:denmark_24032023.pem::Certificate Date=2023-03-24:Run Date=2021-07-27:Expiry Days=(605 days):0

Then I tried to get it to print out just those with expired but unfortunately not getting any luck. Using an if command I am able to print out 1 if it finds expired which is a starting point but sadly not what I am after :smiley:

PS C:\Users\SA-CRBRVRBEDM01> $response = Invoke-WebRequest -Uri 'http://scrbampdk008253:2111/log/amps/JseCertReport.out'  -TimeoutSec 60
  if( $response.RawContent.Contains('expired'))
  $result = 1}
$result = 0}

PS C:\Users\SA-CRBRVRBEDM01> $response.count


I tried to remove the if statement but and run another line using the -Pattern command but at the moment no luck as unsure.

$response = Invoke-WebRequest -Uri 'http://scrbampdk008253:2111/log/amps/JseCertReport.out'  -TimeoutSec 60
$expired = ($response | where -Pattern 'expired' -SimpleMatch).count 

Use Select-String:

$response.RawContent -split '\n' | Select-String 'expired'
1 Like

Hi Matt,

Thanks so much for that :smiley: great help - as I only want to see this as a count I tried to change the .RawContent to .Count but no output, how would I show this as a count?

($response.RawContent -split '\n' | Select-String 'expired').Count

1 Like

Thank you so much Matt!

Thank you both Olef & Matt - can’t thank you enough :smiley: