I just want to write a basic PowerShell script which compares two numbers. When I call my script via .\Compare-Numbers.ps1 -a 3 -b 3 nothing happens.
function Compare-Numbers {
param(
[Parameter(Mandatory=$True, HelpMessage="Please enter two numbers")]
[int]$a,
[int]$b
)
try {
if($a -eq $b) {
Write-Host "the given numbers are equal"
return (exit 0)
}
else {
Write-Host "the given numbers are not equal"
return (exit 1)
}
}
catch {
throw "an error occured!"
exit $LASTEXITCODE
}
}
I would expect that the write-host command writes the text in my command prompt, but - nothing happens.
compare-numbersthough i'd be interested as to know why your code isn't correctly working - when I'm running it it's refusing to ask for$bfor me and I'm unsure why.return (exit 1)will throw an error becauseexitis not known. Replacethrow "an error occured!"withthrow $_to see the error.