Calling out HM Variables in Powershell script

All questions related to installations, configurations and maintenance of Advanced Host Monitor (including additional tools such as RMA for Windows, RMA Manager, Web Servie, RCC).
Post Reply
bgorton
Posts: 11
Joined: Fri May 03, 2019 8:33 am

Calling out HM Variables in Powershell script

Post by bgorton »

Within the Properties -> Variables of my sub-folders, I have a folder variable named %fvar_station%. This Station ID is a 3 digit number unique to each computer in my environment, and on the local workstation this 3 digit code is used when creating log files for an application, in a file format like this:
C:\Logs\STATXXX\Logs Where XXX is the Station ID

I have a Powershell script that looks for the $env:STATION_ID, and when I run the Powershell on a workstation manually, it pulls the Station ID from the Environment Variables and uses it multiple times down the string.

I would like to run this Powershell script on multiple workstations, so I have added it to my Script Manager.

But when I run it in HostMonitor, it returns an error "Invalid Result (Select-String: Cannot Find Path C:\Logs\STAT\Logs (notice, missing the XXX).

Short question: How do I reference a $variable to match at %fvar_variable%?
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

You may refer to command line parameters using $Args[] array.
E.g. $Args[0] is 1st parameter

Then start your script using %fvar_station% as parameter of the command line

Regards
Alex
bgorton
Posts: 11
Joined: Fri May 03, 2019 8:33 am

Post by bgorton »

Something still isn't right.

My Start cmd line in the Script Manager is:

Code: Select all

c:\hostmonitor\powershell.exe %fvar_station%
Then in my PS script I call out the $Args in the place where I want to place the %fvar_station%, like this:

Code: Select all

\Logs\STAT$Args\Logfile.log
And my response in HM is "Error: Invalid Result (%fvar_station%:The term%fvar_station% is not recognized as the name of a cmdlet...."

However, if I change my cmd line in Script Manager to

Code: Select all

c:\hostmonitor\powershell.exe %params%
Then in the Params section of my test properties I specify %fvar_station%, my HM Reply is "Error: Invalid Result (570)" *570 is what my %fvar_station% is set for this folder, so it at least recognizes that variable. But the ultimate output of this PS script is a datetime output.
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

My Start cmd line in the Script Manager is:
c:\hostmonitor\powershell.exe %fvar_station%
No, please use %params% variable in Script Manager,
e.g. powershell.exe %script% %params%
However, if I change my cmd line in Script Manager to
powershell.exe %params%
You removed %script% variable.
So HostMonitor starts c:\hostmonitor\powershell.exe 570
(powershell.exe %fvar_station%) command.
But 570 is not valid file name (script file), right?

If you store script in some external file, then provide filename in command line. E.g.
c:\hostmonitor\powershell.exe myscriptname.ps1 %params%
If you use Script Manager to store script then use %script% variable, e.g.
powershell.exe %script% %params%

There are several samples, just check what command line used in these PowerShell samples.

Regards
Alex
bgorton
Posts: 11
Joined: Fri May 03, 2019 8:33 am

Post by bgorton »

Ok, progress.

Now when I run the script, my first 2 lines look like this:

Code: Select all

$Output = Select-String -Path "I:\DRS\Logs\STAT$arg\UnivMgr.log" -Pattern "User($Login|$Logout)" | Select-Object -Last 1
$Output.line | Out-file "C:\HostMonitor\UserLogTimes\STAT$arg_UserLog.log" -Force
But when I go to look for the output file, it does not have the $arg listed in the file name, it shows as "Stat_UserLog.log" It should have the %fvar_station% ID (570) in the filename/path
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

I think you should specify parameter like
$Output.line | Out-file "C:\HostMonitor\UserLogTimes\STAT{$Args[0]}_UserLog.log" -Force
or
$Output.line | Out-file "C:\HostMonitor\UserLogTimes\STAT$($Args[0])_UserLog.log" -Force

or better
$filename = "C:\HostMonitor\UserLogTimes\STAT" + $Args[0] + "_UserLog.log"
$Output.line | Out-file $filename -Force

Regards
Alex
bgorton
Posts: 11
Joined: Fri May 03, 2019 8:33 am

Post by bgorton »

Frustrating. When I specify my Input and Output files like this:

Code: Select all

$inputfile = "I:\drs\LOGS\STAT" + $Args[0] + "\UnivMgr.log"
$outputfile = "C:\HostMonitor\UserLogTimes\STAT" + $Args[0] + "_UserLog.log"
Then I Try this in HM, specifying 570 as my Params, I get this error:
[9:00:24 AM] HostMonitor is going to execute "Powershell - Logged In Time" script ...
[9:00:24 AM] Script started, invalid result received:
----------
Select-String : Cannot find path 'I:\drs\LOGS\STAT\UnivMgr.log' because it
does not exist.
At C:\HostMonitor\Login_info2.ps1:8 char:11
+ $Output = Select-String -Path "$inputfile" -Pattern "User($Login|$Log ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (I:\drs\LOGS\STAT\UnivMgr.log:St
ring) [Select-String], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Selec
tStringCommand
----------

KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

Please send screen shots and/or config files to support@ks-soft.net

Regards
Alex
KS-Soft
Posts: 12869
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

As I see you are not using 1 script, you are using 2 scripts.
1st script started by HostMonitor using "powershell.exe %script% %params%" command line, then your 1st script starts 2nd script (login_info2.ps1).
The problem: your 1st script does not send any parameters to your 2nd script (login_info2.ps1).

I think there is no reason to use 2 scripts. Probably you need just one?

There are 3 possible solutions:
a) modify your 1st script: it should send parameters to 2nd script
b) use just 1 external script: start login_info2.ps1 using "Start cmd" field, keep "Script field" empty (because you are using external script file)
c) use just 1 internal script: copy text from login_info2.ps1 file into "Script" field and do not use external file at all

Also, script should provide some valid result to HostMonitor.
Please check the manual
https://www.ks-soft.net/hostmon.eng/mfr ... eshell_cmd

Regards
Alex
Post Reply