Help with PowerShell Script - Newbie

Hello!

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.

AS,
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 
$Result.count
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}
  else
  {
$result = 0}

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

PS C:\Users\SA-CRBRVRBEDM01> 

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: