Call out Variables in Powershell

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

Call out Variables in Powershell

Post by bgorton »

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?
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

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
bgorton
Posts: 11
Joined: Fri May 03, 2019 8:33 am

Post by bgorton »

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
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

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
bgorton
Posts: 11
Joined: Fri May 03, 2019 8:33 am

Post by bgorton »

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: Select all

$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.
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

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
bgorton
Posts: 11
Joined: Fri May 03, 2019 8:33 am

Post by bgorton »

That worked, thank you so much for your help!
KS-Soft
Posts: 13012
Joined: Wed Apr 03, 2002 6:00 pm
Location: USA
Contact:

Post by KS-Soft »

You are welcome

Regards
Alex
Post Reply