How to resolve my PS issues?

Hello,
Trying to learn PS for automating tasks. Which then helps me learn!

Here is my simple code:


param (
     [string] $FileName = $null,
     [string] $Hash = $null
     )
(Get-FileHash $FileName).Hash -Algorithm SHA-256 -eq $Hash Write-Output

So my issues:

  1. When I run this and feed it the variables, I get an Explore window opening pointing to the .zip file. I want to leave an .exe or .zip file untouchedl

  2. I am not getting any output. I think I should an if/then loop so if a file hash matches, or doesn’t the user knows very easily. How do I get the STDOUT from inside a script?

My first post so be kind with criticism!
Thanks,
Old Geek

You need to break it down as you have so many syntax errors. Do each part by itself first. Starting with

Get-FileHash $FileName

Does this produce output? Ok great, what about

(Get-FileHash $FileName).Hash

OK cool, now let’s add more

(Get-FileHash $FileName).Hash -Algorithm SHA-256 

Holy moly, that’s not good. When you added ( ).Hash you completed the Get-FileHash command and output only the value of the Hash property. Effectively the command is

SomeLongHash -Algorithm SHA-256

Which is not valid powershell and clearly not what you’re attempting. So assuming you want to add -Algorithm to Get-FileHash, it must be inside the parenthesis

(Get-FileHash $FileName -Algorithm SHA-256).Hash

OK now there’s an error with -Algorithm argument. This is where I’m guessing you got this code from AI. If you simply type Get-FileHash $FileName -Algorithm and press tab, it will complete all the options available. This will reveal the value you are looking for is SHA256

OK so we fixed that

(Get-FileHash $FileName -Algorithm SHA256).Hash

This produces a SHA256 hash. OK let’s add more

(Get-FileHash $FileName -Algorithm SHA256).Hash -eq $Hash

Does that produce a true/false like you expect? OK add your last part

(Get-FileHash $FileName -Algorithm SHA256).Hash -eq $Hash Write-Output

OK now we have a syntax error again. I’m not sure what you’re trying to achieve by adding that, as the output will already output as you’ve seen. So writing out the output that’s already being output is unnecessary. I’d just leave it off.

I would recommend simplifying until you get a better understanding of powershell.

$filehash = Get-FileHash -Path $FileName -Algorithm SHA256

if($filehash){
    $filehash.hash -eq $Hash
}
else{
    Write-Host "No filehash was produced for $FileName" -ForeGroundColor Yellow
}
3 Likes

Hello Doug,

Some how some way I want to buy you a (insert fave drink here) for your reply. I have never gotten such a thorough answer. AND I want to learn more PS thanks to your detailed reply. But I digress:

I changed this based on your critique:

(Get-FileHash $FileName -Algorithm SHA256).Hash -eq $Hash

I ran the script like so:

powershell.exe .\HashChecker.ps1 …..\Downloads\Microsoft.WindowsTerminal_1.20.11381.0_x64.zip B417393110F805835CEAF2EAC56A6274762CEBACEFEABFD915C51441042FB59F
And voila:

True

AND I did not get a File Explorer window open against the zip file!! 2 birds dead with 1 change/stone!

Now that this issue is resolved, before I close this post out AS resolved, please share your fave newb PS learning resource please?

I want to really dig into PS now!

Thanks,
Dave

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.