Need to pass only NOT NULL Hash Table values

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
Vinny vinchenzo
Posts: 1
Last visit: Thu Sep 23, 2021 4:34 am

Need to pass only NOT NULL Hash Table values

Post by Vinny vinchenzo »

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

jvierra
Posts: 14988
Last visit: Sat Dec 04, 2021 10:31 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: Need to pass only NOT NULL Hash Table values

Post by jvierra »

First a "Param" statement is alread a hash table so you don't need to reate a seco\d one.

You can either set defaults on the params or you will have to test them. YOu can use validation statements to cause an exception to be thrown thus ending the command with a message.

First take time to study how a CmdLet is designed to work. It is in the help and all of
this will be explained. Post back if some explanation confuses you.

To pass a hash as a combined set of arguments to a command (CmdLet) you will have to satisfy the CmdLet's parameter requirements. Can't answer that until you learn more about how this works and ask a specific question.

jinalmeida
Posts: 1
Last visit: Fri Oct 22, 2021 12:15 am

Re: Need to pass only NOT NULL Hash Table values

Post by jinalmeida »

jvierra wrote:
Wed Sep 22, 2021 3:44 pm
First a "Param" statement is alread a hash table so you don't need to reate a seco\d one.

You can either set defaults on the params or you will have to test them. YOu can use validation statements to cause an exception to be thrown thus ending the command with a message.

First take time to study how a CmdLet is designed to work. It is in the help and all of
this will be explained. Post back if some explanation confuses you.

To pass a hash as a combined set of arguments to a command (CmdLet) you will have to satisfy the CmdLet's parameter requirements. Can't answer that until you learn more about how this works and ask a specific question.
That is exactly what I was thinking. Thank you for the reply.

jvierra
Posts: 14988
Last visit: Sat Dec 04, 2021 10:31 pm
Answers: 15
Has voted: 3 times
Been upvoted: 12 times

Re: Need to pass only NOT NULL Hash Table values

Post by jvierra »

$PSBoundParameters
Contains a dictionary of the parameters that are passed to a script or function and their current values. This variable has a value only in a scope where parameters are declared, such as a script or function. You can use it to display or change the current values of parameters or to pass parameter values to another script or function.

https://docs.microsoft.com/en-us/powers ... rshell-7.1

Locked