0

I have a variable in my psm1 file that's a KVP hash

$subcmdlist = @{
"addhost" = "Add-Host";
"deletehost" = "Remove-Host";
"setparameter" = "Set-Parameter";
}

This psm1 file also has a function called 'newtask' which accepts an argument of $subcommand.

I'm wondering how i can execute the cmdlet Add-Host when

newtask addhost

is issued from the shell.

I tried to just echo it but that didn't do much good at all. Just printed out the value.

Thanks!

anoopb
  • 533
  • 4
  • 6
  • 20
  • 1
    possible duplicate of [In Powershell, how do you execute an arbitrary native command from a string?](http://stackoverflow.com/questions/6338015/in-powershell-how-do-you-execute-an-arbitrary-native-command-from-a-string) or http://stackoverflow.com/questions/6149167/executing-powershell-script-file-which-is-a-string-value – Matt Oct 28 '14 at 15:40

1 Answers1

1

Use the & sign (aka the call operator), like this: & "Get-Host". This works at least in Powershell 3.0.

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
  • I just realized that calling Invoke-Expression may be better suited for this. See here: http://ss64.com/ps/call.html and here: http://ss64.com/ps/invoke-expression.html – fejesjoco Oct 28 '14 at 16:50