question

MarioonMicrosoft-1066 avatar image
0 Votes"
MarioonMicrosoft-1066 asked MarioonMicrosoft-1066 answered

Powershell script not working from device

I want to use a Powershell script to send me an customize email when different actions (events) occurs concerning my APC UPS device. When the script is run from Powershell is works as it should be. However, when the script is executed from the UPS, it does not work.

The problem has to do with Credentials. Unsafe credential script 1) works perfectly. But getting credential as with script 2) does not work when executed from UPS. The whole setup of Credential in windows, by using Credential Manager, is good because the script successfully send the email when run from PowerShell.

Can someone help me with this problem?


The script runs from an APC UPS connected to a Windows Server 2012 R2. All scripts are located on this server. The Powershell script is run from the UPS by using .bat file.

1) Create credentials. This WORKS

$username = "xxxxxx@yyyyyyyy"
$password = "zzzzzzzzzz"
$myPwd = ConvertTo-SecureString -string $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -argumentlist $username, $myPwd

2) Create credentials. This does NOT work.

$cred=Get-StoredCredential -target XXXXXX

Thanks
Mario


windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered

The Powershell script is run from the UPS by using .bat file.

What exactly does "from the UPS" mean?

Are you running Powershell.exe on the Windows server and referencing the UPS as a network drive? IE: you are reading the script "from the UPS" into the server's memory where powershell.exe is running?

 Powershell.exe \\UPS\Share\SendMail.ps1


Create credentials. This does NOT work.

What error do you get? What account is being used to run the script? Is this being called by a Windows service or the task scheduler?




5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarioonMicrosoft-1066 avatar image
0 Votes"
MarioonMicrosoft-1066 answered MotoX80 commented

ups = uninterruptible power supply.

Via the UPS application PowerChute Business Edition you can execute a script when an event, lets say current outage, occurs. In this case, a .bat file is executed. The .bat file executes the Powershell script

The .bat script
PowerShell.exe -ExecutionPolicy Unrestricted -noProfile -nonInteractive -file c:\xxxxx\PS\scripts\upsMailSelfTestPast.ps1

The system does not give any error.

I think maybe it has to do with permissions. It works ok from within Powershell but not when it is executed from the UPS.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It works ok from within Powershell


In that case, Powershell.exe is executing as your account.

In the Powerchute configuration where you define the .bat file name, do you have an option to tell it to execute the .bat file with a user id? If not, the script will likely be executing as the SYSTEM account.

Modify the .bat file to capture stdout and stderr so that you can see the messages the Powershell produces.

 PowerShell.exe -ExecutionPolicy Unrestricted -noProfile -nonInteractive -file c:\xxxxx\PS\scripts\upsMailSelfTestPast.ps1 1>>C:\xxxx\Logs\UpsMail.log 2>&1

0 Votes 0 ·
MarioonMicrosoft-1066 avatar image
0 Votes"
MarioonMicrosoft-1066 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.