LabTech Support Forums

PowerShell as Admin returns 'OK' rather than result

This forum is for discussion of script related issues.

Moderator: jware.connectwise

PowerShell as Admin returns 'OK' rather than result

Postby shayes.msinetworks » Wed Nov 02, 2016 1:30 pm

I have a LT script that runs a handful of 'Powershell as Admin' commands. The parameter for each is 'Get-WmiObject' with different 'Where' conditions.

I've tested on 4 separate servers (on my way to testing about 10 or so), each running Windows Server 2012 R2. 1 of them, so far, is returning 'OK' rather than the result (or blank).

Code: Select all
Powershell as Admin Get-WmiObject -Namespace root\ITLocal -Class Warranty_Info | Where {$_.ServiceLevelDescription -eq "4 Hour 7x24 Onsite"} | Sort-Object EndDate -Descending | Select-Object -First 1 | Select -ExpandProperty EndDate
SET: [EXTRAFIELD 4 Hour 7x24 Onsite] = %powershellresult%


That same command, when run on the server, itself:
Code: Select all
03/14/2016


That same command, when run on the CMD tab:
Code: Select all
At line:1 char:102
+ ...  warranty_info | Where {$_.ServiceLevelDescription -eq 4 Hour 7x24 On ...
+                                                              ~~~~
Unexpected token 'Hour' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx
   ception
    + FullyQualifiedErrorId : UnexpectedToken


Mind is aching.

*UPDATE*

I also tried running the commands as a separate script, which gets downloaded and ran (versus using the PS.exe wrapper), and saved the output to .txt files, which I set as the variables in labtech to which my EDF's are set.

Blank .txt files should've made blank variables. But EDFs that were set to variables made from blank .txt files have 'OK' as their value.

Where is 'OK' coming from?
shayes.msinetworks
Newbie
Newbie
 
Posts: 13
Joined: Tue Oct 22, 2013 5:07 pm

Re: PowerShell as Admin returns 'OK' rather than result

Postby teamits » Wed Nov 02, 2016 6:39 pm

Outside of powershell, which we don't often use, I have long suspected "OK" is some sort of default return code since it shows up a lot. It also seems to be used for "did the agent start my command successfully" but is easily confused for "the agent ran this command and returned 'OK' to me" and does not necessarily mean the actual command completed successfully (as opposed to started successfully and got an error).

Perhaps add a test for "if variable=OK then set variable to blank"?
Steve
teamits
Hero Member
Hero Member
 
Posts: 1577
Joined: Wed Dec 31, 1969 8:00 pm

Re: PowerShell as Admin returns 'OK' rather than result

Postby shayes.msinetworks » Mon Nov 07, 2016 9:15 am

teamits wrote:Outside of powershell, which we don't often use, I have long suspected "OK" is some sort of default return code since it shows up a lot. It also seems to be used for "did the agent start my command successfully" but is easily confused for "the agent ran this command and returned 'OK' to me" and does not necessarily mean the actual command completed successfully (as opposed to started successfully and got an error).

Perhaps add a test for "if variable=OK then set variable to blank"?


I may wind up doing that, however, I can't help shake the feeling this is part of something that, if I don't determine, is going to make rolling this script out a living nightmare.

Out of 6 test servers, only 3 ran without issue (ours), the other 3 (clients') all had a string of unrelated errors, out of the gate. On one, the REG ADD to set the ExecutionPolicy to Restricted didn't work. On another, I'm getting 'Failed to Login' (even though I've confirmed the logon credentials for both the 'Company > Location's Deployment Defaults' and for the Dell API. So, not sure what that's all about, and on another, well...it's like I'm not even running scripts on it.

All of the test servers are BDR's, all of them are Windows Server 2012 R2, and all of them are current on Windows Updates (and, as part of the script, they all have .NET Framework 4.5+, and Powershell v5), I've also verified that UAC is off, and that AV products are not interfering.

I don't get how it could run in what should be the same environment, and get, ultimately 4 different results on 6 machines. And, if there's one thing I do know, it's that I'm not well-versed enough in programming/scripting to effectively write my way around it.
shayes.msinetworks
Newbie
Newbie
 
Posts: 13
Joined: Tue Oct 22, 2013 5:07 pm

Re: PowerShell as Admin returns 'OK' rather than result

Postby dan.cymorthit » Sun Apr 02, 2017 3:11 pm

I ran into the same issue on win2k12r2. Finally found that the one server didn't have powershell 2.0 engine installed. As soon as I installed it, I no longer received the "OK" results and it returned the results I was expected.
dan.cymorthit
Newbie
Newbie
 
Posts: 1
Joined: Fri Jan 02, 2015 1:04 pm

Re: PowerShell as Admin returns 'OK' rather than result

Postby dwhite.raymorgan » Tue Jul 11, 2017 5:10 am

FYI, If your script returns an error (Outputs to STDERR) or outputs on any unexpected IO channel (Write-Host) then LabTech will return "OK" instead of the actual results.

Fixes:
  • Use Write-Output instead of Write-Host.
  • Capture any CMDLet error output (Use -ErrorAction SilentlyContinue, or try {} catch {}).
  • Catch stray output ($Result=Do-Something -Here; instead of: Do-Something -Here;)
  • Catch error output for DOS Commands called from PowerShell. (dosomething.exe Here 2>'' This is two single quotes, not a double quote)
dwhite.raymorgan
Newbie
Newbie
 
Posts: 7
Joined: Tue Jun 10, 2014 4:44 pm


Return to Scripts

Who is online

Users browsing this forum: No registered users and 2 guests