1

Powershell how to setup parameter as variable ?

The code like this:

get-stat -Entity (get-vmhost ) -Disk -Start 'date-time' -Finish 'date-time'

For parameter -Disk, there are other options can be used. I want to setup this parameter as a variable by the following so I can choose what to run for.

$item = read-host -prompt 'Select Disk, CPU, Network or Common'
get-stat -Entity (get-vmhost ) -$item -Start 'date-time' -Finish 'date-time'

But it wont work returning error message "A positional parameter can't be found"

Any solution to that please?

1 Answer 1

2

you can use PowerShell Splatting a way of bundling parameters to send to a command:

$item = read-host -prompt 'Select Disk, CPU, Network or Common'
$aB = @{$item = $true;
       Entity = (get-vmhost );
       Start = 'date-time';
       Finish  = 'date-time'}
get-stat @aB
Sign up to request clarification or add additional context in comments.

2 Comments

it wont work, return error message "true : The term 'true' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again"
true should either be "true" or 'true' (if it's meant to be a string) or $true (if it's meant to be a boolean true/false value)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.