Powershell script file problem

I have sought an answer in another forum, made some progress, but still haven't managed to get this to work....

I have several Smart Switches controlled by Alexa, running automated routines. Trouble is, that sometimes the SM's become inaccessible to Alexa, so the routine doesn't then do anything - until I happen to check the devices in Alexa and notice the comms failure, then reset.

I have not found any way for Alexa to advise me of the failure, so I looked at other ways and a possible solution seems to be to ping the devices and report ping failures.

Nirsoft's PingInfoView is a likely candidate for the job, but doesn't include the essential ability to send an email to report failures, it is though supposed to be able to use a PowerShell script to send an email...

I have the PingInforView located in C:\Program Files\Nirsoft and the script file in that same folder.

This is the 'failed.ps1' script, with the details adjusted for my details in the first four lines of the script.

++++++++++++++++++++++++++++++++ $Username = "username"; $Password = "password"; $SendTo = "my email address"; $MailServer = "mail server"; $HostName = $args[0]; $IPAddress = $args[1]; $PingStatus = $args[2]; $FailedOn = $args[3];

$message = new-object Net.Mail.MailMessage; $message.From = $Username; $message.To.Add($SendTo); $message.Subject = "Failed Ping On $HostName" ; $message.Body = "Information about the failed ping: `r`nHost Name: $HostName`r`nIP Address: $IPAddress`r`nPing Status: $PingStatus`r`nPing Time: $FailedOn";

$smtp = new-object Net.Mail.SmtpClient($MailServer, "25"); $smtp.EnableSSL = $true; $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); $smtp.send($message);

++++++++++++++++++++++++++++++++++++

This line is run by PingInfoView when a failure to reply to ping is detected...

Powershell.exe -executionpolicy remotesigned -File C:\Program Files\Nirsoft\failed.ps1 "%HostName%" "%IPAddress%" "%LastPingStatus%" "%LastFailedOn%"

It is obvious that the line does get run when a deliberate failure is set up, because I see a brief flash of the blue PS Console screen, but no email is sent, or received.

If I open a PS Console and past the above line in, this is the response I get...

PS C:\WINDOWS\system32> Powershell.exe -executionpolicy remotesigned

-File C:\Program Files\Nirsoft\failed.ps1 "%HostName%" "%IPAddress%" "%LastPingStatus%" "%LastFailedOn%" Processing -File 'C:\Program' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again. Windows PowerShell

If I copy and paste the entire contents of the actual script into the PS Console, it runs as expected, an email is sent and received.

I also made sure that 'failed.ps1' was edited in Wordpad and saved as 'all files'. The file is also listed in Explorer as a Powershell Script, so why this complaint when trying to use the script.... 'Processing -File 'C:\Program' failed because the file does not have a '.ps1' extension.' ???

Reply to
Harry Bloomfield Esq
Loading thread data ...

What does a DIR in Powershell show ?

Also have you enabled all the plugins and are you running in admin ?

Reply to
Jethro_uk

I think you ned quotes round the C:\Program Files\Nirsoft\failed.ps1 bit, or otherwise it will assume the embedded space between Program and Files marks the end of the file name. So:

PS C:\WINDOWS\system32> Powershell.exe -executionpolicy remotesigned > -File "C:\Program Files\Nirsoft\failed.ps1" "%HostName%" "%IPAddress%" > "%LastPingStatus%" "%LastFailedOn%"

Reply to
John Rumm

enclose the C:\Program Files\Nirsoft\failed.ps1 in double quotes as it contains a space.

Reply to
Andy Burns

Thanks, but

I tried the quotes and it simply produces the same error as before..

Powershell.exe -executionpolicy remotesigned

Processing -File 'C:\Program' failed because the file does not have a '.ps1' extension. Specify a valid Windows PowerShell script file name, and then try again.

Reply to
Harry Bloomfield Esq

Nail, head I think...

PS C:\Program Files\Nirsoft> dir

Directory: C:\Program Files\Nirsoft

Mode LastWriteTime Length Name

---- ------------- ------ ----

-a---- 11/07/2022 21:30 733 failed.ps1.ps1

-a---- 10/07/2022 12:58 72097 GUNAuto_Shot beretta m12 9 mm (ID 0437)_BSB.mp3

-a---- 12/07/2022 13:11 2189 PingInfoView.cfg

-a---- 06/01/2022 22:11 17765 PingInfoView.chm

-a---- 06/01/2022 22:11 60792 PingInfoView.exe

-a---- 12/07/2022 13:11 358 PingInfoView_hosts.txt

-a---- 06/01/2022 22:11 17023 readme.txt

Thanks!

Reply to
Harry Bloomfield Esq

:)

The older I get, the less I like a lot of GUIs - they really are totally shit and getting worse. The crowning turd being the fetish for "active" web pages that it's impossible to search or download (possibly not by accident).

Give me ls, df, du, grep, awk and a few other basic CLI tools anyday.

Now when it comes to sysadmining, GUIs are a different beast. Done properly (note the caveat) they can enforce the underlying logic of a command or system without needing a doctorate. For example you want to enter a setting but the panel or box is greyed out because you need to check another. Much easier to find out what and how than a command line that can be seen from space.

Reply to
Jethro_uk

Well it failed where the space between program and files exists, so I's still say it's a quoting issue, how exactly did you run it?

I did an equivalent test, of an existing .ps1 file, in a folder with a space in the name, using double quotes and that made the difference between the error you're getting and no error but running my script.

Also, why are you starting a powershell session, then running a nested instance of powershell within it?

Reply to
Andy Burns

Andy Burns expressed precisely :

I was just testing it, to see why it wouldn't work called from my ping software.

As indicated above, there were two issues - the lack of quotes around the file to run and an issue with the file suffix - failed.ps1.ps1

Reply to
Harry Bloomfield Esq

The first setting to change on any windows account is the brain-dead "hide knows extensions" option ...

Reply to
Andy Burns

+1

Who in their right mind ever though that was a sensible default?

Reply to
John Rumm

Off-topic troll...

Reply to
Edward's Mother

Off-topic troll...

Reply to
Judge Dredd

Off-topic troll...

Reply to
Edward Hernandez Loves Porn

Off-topic troll...

Reply to
Judge Dredd

Off-topic troll...

Reply to
Edward Hernandez Loves Porn

HomeOwnersHub website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.