Passing arguments into powershell script via state

251 views
Skip to first unread message

Joachim Grubelnik

unread,
Jan 26, 2016, 4:49:42 PM1/26/16
to Salt-users
Hello all!

anyone reading this thanks for your time infront. 

tried now a lot of different ways to pass arguments to my powershell script. 
In the docs it states using args='dnsname host_ip aso......' for the command line call ..... but i cannot translate it correctly how to do it with the state file 

if i did this with a linux shell script it worked well ... cause i used the env: parameter and used different ENV Variables so i could access them in the script then .... 
if i do the same for powershell it does not work at all, i starting up with salt  and i could not find and example how to do this correctly
i used the load param part with local variables which worked nice when i call the powershell script but not if i pass it into ... stays empty .... 

hope anyone can point me in the right direction, maybe i have to use a single args='dnsname host_ip aso... ' and in the powershell script somehow use dnsname=args[0] host_ip=args[1] aso ????

i put a code share HERE too .... https://codeshare.io/q8g9o

here is my example i tried to explain step by step what i tried 

1. created a state file 
   
INTDNS.sls

dadi create DNS entry:
  cmd.script:
    - source: salt://files/createdns.ps1
    - cwd: /temp/createdns.ps1
    - shell: 'powershell'
    - kwargs: # tried args:  env:  kwargs: #
      - DNSNAME: "testserver24.mydomain.com"
      - HOST_IP: "192.168.1.1"
      - DNS_SUFFIX: "mydomain.com"
      - DNSSERVER: "192.168.1.10"
      - REVERSE_DNS_ZONE_1: "0.168.192.in-addr.arpa."

-------------------

2. created powershell script createdns.ps1
      
#Load parameters if default values needed local test 
#param(
#    [string]$DNSNAME = "testserver24.mydomain.com",
#    [string]$DNS_SUFFIX = "mydomain.com",
#    [string]$DNSSERVER = "192.168.1.1",
#    [string]$HOST_IP = "192.168.1.100"
#)

import-module DNSServer

$OldObj = Get-DnsServerResourceRecord -Name $DNSNAME -ZoneName $DNS_SUFFIX -RRType "A" -ComputerName $DNSSERVER -ErrorAction SilentlyContinue

If ($OldObj -eq $null) {
  Add-DnsServerResourceRecord -Name $DNSNAME -ZoneName $DNS_SUFFIX -A -CreatePtr:$true -ComputerName $DNSSERVER -IPv4Address $HOST_IP
}
Else {
  $NewObj = Get-DnsServerResourceRecord -Name $DNSNAME -ZoneName $DNS_SUFFIX -RRType "A" -ComputerName $DNSSERVER
  $NewObj.RecordData.Ipv4Address = $HOST_IP
If ($NewObj -ne $OldObj) {
  Set-DnsServerResourceRecord -NewInputObject $NewObj -OldInputObject $OldObj -ZoneName $DNS_SUFFIX -ComputerName $DNSSERVER
}
}
$OldObj = $null
$NewObj = $null

3. salt 'myserver' state.sls INTDNS

 ----- RESULT  BEGIN -----
error message when using salt 'minionid' INTDNS
             stderr:
                  Get-DnsServerResourceRecord : Das Argument f r den Parameter "Name" kann nicht 
                  berpr ft werden. Das Argument ist NULL oder leer. Geben Sie ein Argument an, 
                  das nicht NULL oder leer ist, und f hren Sie den Befehl erneut aus.
                  In C:\temp\tmprsl95w.ps1:12 Zeichen:45
                  + $OldObj = Get-DnsServerResourceRecord -Name $DNSNAME -ZoneName $DNS_SUFFIX 
                  -RRTy ...
                  +                                             ~~~~~~~~
                      + CategoryInfo          : InvalidData: (:) [Get-DnsServerResourceRecord],  
                     ParameterBindingValidationException
                      + FullyQualifiedErrorId : ParameterArgumentValidationError,Get-DnsServerRe 
                     sourceRecord
                   
                  Add-DnsServerResourceRecord : Das Argument f r den Parameter "Name" kann nicht 
                  berpr ft werden. Das Argument ist NULL oder leer. Geben Sie ein Argument an, 
                  das nicht NULL oder leer ist, und f hren Sie den Befehl erneut aus.
                  In C:\temp\tmprsl95w.ps1:15 Zeichen:37
                  +   Add-DnsServerResourceRecord -Name $DNSNAME -ZoneName $DNS_SUFFIX -A 
                  -CreatePtr ...
                  +                                     ~~~~~~~~
                      + CategoryInfo          : InvalidData: (:) [Add-DnsServerResourceRecord],  
                     ParameterBindingValidationException
                      + FullyQualifiedErrorId : ParameterArgumentValidationError,Add-DnsServerRe 
                     sourceRecord
              stdout:
 ----- RESULT END -----

Joachim Grubelnik

unread,
Jan 27, 2016, 1:10:20 PM1/27/16
to Salt-users
ok found a solution myself, see the solution here if someone ever gets the same ....

Reply all
Reply to author
Forward
0 new messages