Calling out HM Variables in Powershell script
Calling out HM Variables in Powershell script
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%?
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%?
Something still isn't right.
My Start cmd line in the Script Manager is:
Then in my PS script I call out the $Args in the place where I want to place the %fvar_station%, like this:
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
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.
My Start cmd line in the Script Manager is:
Code: Select all
c:\hostmonitor\powershell.exe %fvar_station%
Code: Select all
\Logs\STAT$Args\Logfile.log
However, if I change my cmd line in Script Manager to
Code: Select all
c:\hostmonitor\powershell.exe %params%
No, please use %params% variable in Script Manager,My Start cmd line in the Script Manager is:
c:\hostmonitor\powershell.exe %fvar_station%
e.g. powershell.exe %script% %params%
You removed %script% variable.However, if I change my cmd line in Script Manager to
powershell.exe %params%
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
Ok, progress.
Now when I run the script, my first 2 lines look like this:
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
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
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
$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
Frustrating. When I specify my Input and Output files like this:
Then I Try this in HM, specifying 570 as my Params, I get this error:
Code: Select all
$inputfile = "I:\drs\LOGS\STAT" + $Args[0] + "\UnivMgr.log"
$outputfile = "C:\HostMonitor\UserLogTimes\STAT" + $Args[0] + "_UserLog.log"
[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
----------
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
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