2011-02-18 7 views
6

Existe-t-il un moyen dans PowerShell de passer un HashTable comme argument lorsqu'il est appelé avec cmd.exe?cmd.exe powershell HashTable

Je veux appeler un script comme celui-ci:

powershell "& 'C:\path\to\file.ps1 arg1 arg2 arg3 arg4'" 

Où arg4 est un Hashtable. Est-ce possible?

Répondre

11

Compte tenu d'un script (foo.ps1) comme ceci:

param($a1, $a2, $a3, [hashtable]$a4) 

"a1 is $a1" 
"a2 is $a2" 
"a3 is $a3" 
"a4 is " 
$a4 

Vous pouvez l'appeler à partir cmd.exe comme si la spécification d'un Hashtable comme le quatrième paramètre:

C:\> powershell -command "& {c:\foo.ps1 1 2 three @{name='John';age=45}}" 
a1 is 1 
a2 is 2 
a3 is three 
a4 is 

Name       Value 
----       ----- 
name       John 
age       45 
0

La seule chose qui vient à l'esprit est de sauvegarder la table de hachage en tant que xml, et lui passer le nom du fichier.

Questions connexes