It works as expected with Get-Help ./UPSMonitor.ps1
You appear to have written a custom -help parameter and that outputs the text you specified:
if ($help) {
write-host "UPSMonitor is a utility written and tested in Powershell script (v 7.1) that taps into the Microsoft OS Win32_Battery class in order to provide the following optional functions:"
Write-Host "`tEmail alerts"
Write-Host "`tAction script"
Write-Host "`n`r"
Write-Host "The most up-to-date version of this utility and documentation is available from https://github.com/chribonn/UPSMonitor"
}
This was failing. When I tried to run this from a command prompt I get the following:
Watch-Win32_UPS.ps1: Cannot process argument transformation on parameter âEmailSMTPUseSSLâ. Cannot convert value âSystem.Stringâ to type âSystem.Booleanâ. Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.
I took a look at your script and I have a question, likely a stupid one so be gentle.
What is the benefit of defining your variables using New-Variable? I just use $var = âsomethingâ and yes, I am aware of scoping. I have just never used New-Variable.
I am always looking at being better at PowerShell. Thanks in advance for your input.
I will admit that I donât know the answer. I date back to COBOL and my BSc had a module on ADA. Thatâs how dated I am :-).
I landed on PowerShell coincidentally. I have a Socomec UPS and could not find a driver to make it shutdown my computers if there is a power failure. After failing to find a solution I decided to give up and write my own code.
My research led me to PS and I decided to write it in this language. I tend to have a preference for verbose code that I find easier to understand. Also I prefer to have formal variable definition and typecasting (influenced by development tools such as C++, C#, Pascal, etc).
Thanks for your 2c. I believe I am similar to you in verbose code. I write for clarity and for the next guy down the road that inherits my mess
What I am hoping to understand is if Set-Variable will help clarify my code in the area of variable scope. I have one script that is very large and has global variables in the main body that I pass to functions in a Module as arguments. I believe I can also set these variables via my PowerShell data file but feedback from this site said to stick with passing as arguments.
I will play around with Set-Variable and see if it clarifies and or simplifies my code. Thanks again.
Thanks Olaf. So, I made an error. The question I posed to the forum some time back was the best method to pass parameters from the main body of the script to functions within a module. The advise I got was to stick with passing them as arguments to the functions within the module. Do you have any advise on that?
Iâd say the same. What I meant is not to use global variables inside the functions. Instead itâs better to pass them as arguments. So the functions do not depend on nothing from outside their scope.
I tend to prefer solutions in which all variable used within the module are either passed as parameters from the calling module, are declared within the module (and destroyed with the module) and returned back to the calling module in a return value.
With PowerScript I had a problem passing an object as a parameter. In my code the Battery is an object.