View previous topic :: View next topic |
Author |
Message |
bgorton
Joined: 03 May 2019 Posts: 11
|
Posted: Mon Nov 25, 2019 5:35 pm Post subject: Call out Variables in Powershell |
|
|
I have some custom variables (ex. fvar_hostname = 'hostname) that I used in HostMonitor. I have a Powershell script I am running thru Script Manager. In my script I call out my parameters from HM that I want to use (typically is is fvar_ip, or the IP address of the station I am testing against. Within my Powershell script, I want to save the output of the script, and I can do so by calling out $args, but this only writes what I call out as a parameter. How can I tell my script to output a different custom variable?
For example, I am checking stations to ensure a service is running, and my Param is fvar_ip. My output is my fvar_ip variable.csv. Since I do not easily recognize the IP of every station, I would prefer my output be fvar_hostname.csv. How would I go about calling out this HM Variable in my script? |
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12791 Location: USA
|
Posted: Tue Nov 26, 2019 7:13 am Post subject: |
|
|
Not sure I understand the problem.
You can use variable as command line parameter.
Then you can refer to this parameter and create file with specified name, e.g. using powershell command like
Out-File -FilePath .\$($args[1])_file.csv
Regards
Alex |
|
Back to top |
|
|
bgorton
Joined: 03 May 2019 Posts: 11
|
Posted: Tue Nov 26, 2019 10:45 am Post subject: |
|
|
In my HostMonitor test, I am running a Shell Script. In the Params box, I specify %fvar_ip% which is set as a Variable to the stations IP address. Within the Script Manger, I am calling my Scritpt using powershell.exe %script% %params%
The IP address is passed to my script and is used to connect to the station to perform the script (DNS at our facility is a joke at best, otherwise I would simply use the hostname). Within my script I call out 'ExportCSV $args.csv and it saves the file as \192.168.1.1.csv. I would like to save the file as \hostname.csv. In Hostmonitor I have a variable %fvar_hostname% set. How can I add this variable to my script? I have tried adding this variable to my paramaters in HM, then in my script telling it to save to $($args[1]).csv but the file name is blank, it just saves it as .csv |
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12791 Location: USA
|
Posted: Tue Nov 26, 2019 11:30 am Post subject: |
|
|
If you want to pass 2 parameters then specify "%fvar_ip%" "%fvar_hostname%" as parameters for the test.
Then $args[0] will represent IP (1st parameter), $args[1] will return hostname (2nd parameter)
Regards
Alex |
|
Back to top |
|
|
bgorton
Joined: 03 May 2019 Posts: 11
|
Posted: Tue Nov 26, 2019 1:07 pm Post subject: |
|
|
Ok, that makes sense, but its not working for me. When I specify in HM in my paramaters "%fvar_ip%" "%fvar_hostname%" and tab to another box, my IP and Hostname populate in the Params box. But in my script, when I specify to output to .\$args[1].csv, I am not getting any output. Here is my script:
Code: | $ServiceName = 'DR AppGate Manager'
$restartlog = "L:\logs\Digate Restart\$args[1].csv"
$count = 0
#end region variables
#--- SCRIPT ---#
#region script
$arrService = Get-Service -ComputerName $args[0] -Name $ServiceName
if ($arrService.Status -eq 'Running')
{"scriptres:Ok:Running"
(exit)}
if ($arrService.Status -ne 'Running')
{
$arrService | Start-Service
Start-Sleep -seconds 15
$arrService.Refresh()
$count++
until($count -eq 5)
if ($arrService.Status -eq 'Running')
{
"scriptres:Ok:Restarted"
"$DateFormat, $args[1], Restarted" | Out-File $restartlog -Append
}
} |
My response in HM is correctly reporting if the service is restarted, but my output is now nothing. Even if I replace the "Out-File" with "Export-CSV" I get no CSV created/written. |
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12791 Location: USA
|
Posted: Tue Nov 26, 2019 2:25 pm Post subject: |
|
|
Do you need this script just to check and restart service?
HostMonitor offers Service test and Restart service action. Built-in tests and actions use less system resources.
I think you should correct line #2
$restartlog = "L:\logs\Digate Restart\$($args[1]).csv"
Regards
Alex |
|
Back to top |
|
|
bgorton
Joined: 03 May 2019 Posts: 11
|
Posted: Wed Nov 27, 2019 12:03 pm Post subject: |
|
|
That worked, thank you so much for your help! |
|
Back to top |
|
|
KS-Soft
Joined: 03 Apr 2002 Posts: 12791 Location: USA
|
Posted: Wed Nov 27, 2019 12:15 pm Post subject: |
|
|
You are welcome
Regards
Alex |
|
Back to top |
|
|
|