question

VinnyVinchenzo-6154 avatar image
0 Votes"
VinnyVinchenzo-6154 asked VinnyVinchenzo-6154 commented

Need to pass only NOT NULL Hash Table values

Hi,

I need to create a wrapper for a command line executable that accepts 3 optional parameters.

To be able to read those parameters, I did add a Param keyword and that works fine:

Param(
[Parameter(Mandatory=$false)]
[string] $SERVER_PROXY,
[Parameter(Mandatory=$false)]
[string] $SITE_TOKEN,
[Parameter(Mandatory=$false)]
[switch] $PREVENT_REBOOT
)


But then I need to pass to the command line executable, only the parameters that have been included on the command line.

Since they are all optional I did not want to create a giant if with all the possibilities and I thus created a Hash Table:

$CLParams = @{
SERVER_PROXY = $SERVER_PROXY
SITE_TOKEN = $SITE_TOKEN
PREVENT_REBOOT = $PREVENT_REBOOT
}

I do not know if this is wrong since I already have a Param at the beginning of the script but any how, after that I was not sure what to do like if $SERVER_Proxy is $null or something else.

I think I should create a new Hashtable, from the values that are not null and then pass that to the command line but I do not know how to that.

At the end I would finish with something like 'whatever.exe @NewHashTable'

does that make sense?

Thanks

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered VinnyVinchenzo-6154 commented

You can try something like this:

 Param(
     [Parameter(Mandatory=$false)]
         [string] $SERVER_PROXY,
     [Parameter(Mandatory=$false)]
         [string] $SITE_TOKEN,
     [Parameter(Mandatory=$false)]
         [switch] $PREVENT_REBOOT
 )
    
 $CLParams = @{}
 If ($SERVER_PROXY){$CLParams['SERVER_PROXY']     = $SERVER_PROXY}
 If ($SITE_TOKEN){$CLParams['SITE_TOKEN']         = $SITE_TOKEN}
 If ($PREVENT_REBOOT){$CLParams['PREVENT_REBOOT'] = $PREVENT_REBOOT}
    
 $params = ""
 $CLParams.GetEnumerator() |
     ForEach-Object{
         $params += " -{0} {1}" -f $_.Key, $_.Value
     }
 whatever.exe $params

I doubt trying to use "splatting" will work with the EXE, though. That's why I turned the hash into a typical "-paramname paramvalue" string.

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Ok Thanks. Works like a charm.
Vincent

0 Votes 0 ·

Actually, it's probably even a lot more straightforward to do it without the hash at all!

 Param(
      [Parameter(Mandatory=$false)]
          [string] $SERVER_PROXY,
      [Parameter(Mandatory=$false)]
          [string] $SITE_TOKEN,
      [Parameter(Mandatory=$false)]
          [switch] $PREVENT_REBOOT
  )
    
 $params = ""
 If ($SERVER_PROXY)  {$params += " -SERVER_PROXY $SERVER_PROXY"}
 If ($SITE_TOKEN)    {$param  += " -SITE_TOKEN $SITE_TOKEN"}
 If ($PREVENT_REBOOT){$params += " -PREVENT_REBOOT  $PREVENT_REBOOT"}
    
 whatever.exe $params
0 Votes 0 ·

works too!

0 Votes 0 ·

Yes much better, thanks!

0 Votes 0 ·