salt icon indicating copy to clipboard operation
salt copied to clipboard

How do I pass variables into a cmd.script?

Open evenyougreg opened this issue 7 years ago • 3 comments

Description of Issue/Question

I'm trying to follow the salt docs on passing variables into a PowerShell script but nothing seems to work for me :/

I would like to be able to change "Java*" to a different application at will from the command line.

The goal is to get the application and version number from a specific application, but the script has Java hard coded into it. I have to output the results into a CSV so I also need the least amount of text possible.

$application = "Java*"

$applicationList = @()

$applicationList += `

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | `
Where-Object -Property DisplayName -like $application | `
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate

$applicationList += `

Get-ItemProperty HKLM:\Software\WOW6432node\Microsoft\Windows\CurrentVersion\Uninstall\* | `
Where-Object -Property DisplayName -like $application | `
Select-Object DisplayName, DisplayVersion, Publisher, InstallDate

Return $applicationList

I imagine something like:

salt -G os:Windows cmd.script salt://scripts/Applications.ps1 env='{"applications": "Google"}'

evenyougreg avatar Apr 23 '19 15:04 evenyougreg

First you need to set your scrpt up properly so that it can receive command line arguments. Once it's running from the command line accepting arguments correctly, then you can use args to pass them using salt.

https://docs.saltstack.com/en/latest/ref/modules/all/salt.modules.cmdmod.html#salt.modules.cmdmod.script

twangboy avatar Apr 23 '19 16:04 twangboy

Thanks! I'll give that a try.

evenyougreg avatar Apr 23 '19 17:04 evenyougreg

That was the hint that I needed! I forgot about needing to have $args[0] in the script, then I just used args='Adobe'* in with the cmd.script module and it worked the first try.

Is there any way to cut out everything but the stdout with cmd.script or is cmd.run_stdout the only way?

I am trying to reduce the noise as much as possible.

Thanks @twangboy

evenyougreg avatar Apr 23 '19 19:04 evenyougreg