Question Création d'objets depuis variables

Plus d'informations
il y a 14 ans 8 mois #10258 par sebastien
Bonjour à tous,

Je souhaite savoir si il est possible de créer des objets à partir de variable. Voici un cas concret :

[code:1]
Get-HpcNode | % {$_.netbiosname} | Sort-Object | % {
$i += 100
$objLabel_$_ = New-Object System.Windows.Forms.Label
($objLabel_$_).Location = New-Object System.Drawing.Size(30,$i)
($objLabel_$_).Size = New-Object System.Drawing.Size(80,20)
($objLabel_$_).Text = \"$_\"
$objForm.Controls.Add($objLabel_$_)

}[/code:1]

Je tente de me faire une \"console\" de gestion d'un cluster de calcul. Je souhaite afficher chaque nœuds du cluster en fonction de son nom netbios. Je souhaite donc récupérer chaque nom de machine pour créer une entrée dans une \"System.Windows.Forms.Form\". Bien évidement, $objLabel_$_ = ne fonctionne absolument pas.

Cela est il possible ?

Merci

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 14 ans 8 mois #10259 par Richard Lazaro
Bonjour,

Si tu souhaites construire des variables à la volée, tu peux passer par les cmdlets :
(New/Get/Set)-Variable

Le nom est spécifié par un paramètre, donc tu pourras t'amuser avec des variables :

[code:1]
New-Variable -Name \"`$objLabel_$_\" -Value (New-Object System.Windows.Forms.Label)


(Get-Variable -Name \"`$objLabel_$_\" -ValueOnly).Location = New-Object System.Drawing.Size(30,$i)
[/code:1]

Il doit peut être exister une méthode plus simple avec un accesseur directe, mais je trouve rien pour l'instant.

Enfin, cela est utile si tu as besoin de retravailler avec tes varaibles par la suite, sinon :

[code:1]
Get-HpcNode | % {$_.netbiosname} | Sort-Object | % {
$i += 100
$objForm.Controls.Add((New-Object New-Object System.Windows.Forms.Label -Property @{ \"Location\" = (New-Object System.Drawing.Size(30,$i)) ; \"Size\" = (New-Object System.Drawing.Size(80,20)) ; \"Text\" = $_ }))
}
[/code:1]


Bien Cordialement,
Richard Lazaro.

Think-MS : (Get-Life).Days | %{ Learn-More }

\\"Problems cannot be solved by the same level of thinking that created them.\\" - Albert Einstein

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 14 ans 8 mois #10260 par sebastien
Merci Richard de cette réponse !

Je pense que je vais arriver à faire mon bonheur avec :P

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.035 secondes
Propulsé par Kunena