KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

Calling out HM Variables in Powershell script

 
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting
View previous topic :: View next topic  
Author Message
bgorton



Joined: 03 May 2019
Posts: 6

PostPosted: Wed May 29, 2019 3:43 pm    Post subject: Calling out HM Variables in Powershell script Reply with quote

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%?
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12092
Location: USA

PostPosted: Wed May 29, 2019 4:52 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
bgorton



Joined: 03 May 2019
Posts: 6

PostPosted: Thu May 30, 2019 8:23 am    Post subject: Reply with quote

Something still isn't right.

My Start cmd line in the Script Manager is:
Code:
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:
\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:
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.
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12092
Location: USA

PostPosted: Thu May 30, 2019 10:32 am    Post subject: Reply with quote

Quote:
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%

Quote:
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
Back to top
View user's profile Send private message Visit poster's website
bgorton



Joined: 03 May 2019
Posts: 6

PostPosted: Thu May 30, 2019 2:12 pm    Post subject: Reply with quote

Ok, progress.

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

Code:
$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
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12092
Location: USA

PostPosted: Fri May 31, 2019 11:05 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
bgorton



Joined: 03 May 2019
Posts: 6

PostPosted: Mon Jun 03, 2019 9:00 am    Post subject: Reply with quote

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

$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:

Quote:
[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
----------


Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12092
Location: USA

PostPosted: Mon Jun 03, 2019 9:50 am    Post subject: Reply with quote

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

Regards
Alex
Back to top
View user's profile Send private message Visit poster's website
KS-Soft



Joined: 03 Apr 2002
Posts: 12092
Location: USA

PostPosted: Mon Jun 03, 2019 1:20 pm    Post subject: Reply with quote

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/mframe.htm#tests.htm#createshell_cmd

Regards
Alex
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Configuration, Maintenance, Troubleshooting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index