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

Call out Variables in Powershell

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

PostPosted: Mon Nov 25, 2019 5:35 pm    Post subject: Call out Variables in Powershell Reply with quote

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
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12791
Location: USA

PostPosted: Tue Nov 26, 2019 7:13 am    Post subject: Reply with quote

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



Joined: 03 May 2019
Posts: 11

PostPosted: Tue Nov 26, 2019 10:45 am    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12791
Location: USA

PostPosted: Tue Nov 26, 2019 11:30 am    Post subject: Reply with quote

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



Joined: 03 May 2019
Posts: 11

PostPosted: Tue Nov 26, 2019 1:07 pm    Post subject: Reply with quote

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
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12791
Location: USA

PostPosted: Tue Nov 26, 2019 2:25 pm    Post subject: Reply with quote

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



Joined: 03 May 2019
Posts: 11

PostPosted: Wed Nov 27, 2019 12:03 pm    Post subject: Reply with quote

That worked, thank you so much for your help!
Back to top
View user's profile Send private message
KS-Soft



Joined: 03 Apr 2002
Posts: 12791
Location: USA

PostPosted: Wed Nov 27, 2019 12:15 pm    Post subject: Reply with quote

You are welcome

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