Question
valeur fonction dans plusieurs fonctions
- PatriceLT
- Auteur du sujet
- Visiteur
-
il y a 9 ans 3 semaines #23554
par PatriceLT
valeur fonction dans plusieurs fonctions a été créé par PatriceLT
Bonjour,
J'ai actuellement un soucis de scope concernant une variable qui m'embête car elle ne veut pas se mettre en global...
Voici le script qui est assez long, la partie qui m'intéresse est celle de la fonction TreeView, qui a dedans, une fonction Get-CheckedNode, et j'aimerai retourner la valeur de ce Get-CheckedNode Lorsque je clique sur un bouton (Export-GPO) par exemple.
[code:1]
#===============#
# Import Module #
#===============#
Import-Module ActiveDirectory
Import-Module GroupPolicy
$Snapin = ''
$Snapin = Get-PSSnapIn -Name Quest.ActiveRoles.ADManagement
if(!$Snapin)
{
\"Ajout Du Module PSSnapin AD Quest...\"
Add-PSSnapin Quest.ActiveRoles.ADManagement
\"Module PSSnapin AD Quest Ajouté\"
}
#=================#
# Import Assembly #
#=================#
[void] [system.reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\"«»)
[void] [system.reflection.Assembly]::LoadWithPartialName(\"System.Drawing\"«»)
[void] [System.Reflection.Assembly]::LoadWithPartialName(\"System.DirectoryServices\"«»)
#===========#
# Fonctions #
#===========#
#===========#
# Variables #
#===========#
#====================#
# Création des Forms #
#====================#
$FenetrePrincipale = New-Object System.Windows.Forms.Form
$InstructionDOMAINE = New-Object System.Windows.Forms.Label
$ListeDOMAINE = New-Object System.Windows.Forms.ComboBox
$ResultatDOMAINE = New-Object System.Windows.Forms.RichTextBox
$Resultat = New-Object System.Windows.Forms.RichTextBox
$BouttonDOMAINE = New-Object System.Windows.Forms.Button
$BouttonEXPORTGPO = New-Object System.Windows.Forms.Button
$BouttonBACKUPGPO = New-Object System.Windows.Forms.Button
$BouttonQUITTER = New-Object System.Windows.Forms.Button
#=====================#
# Paramètre des Forms #
#=====================#
#=/ Fenetre Principale \=#
$FenetrePrincipale.Size = New-Object System.Drawing.Size(800, 800)
$FenetrePrincipale.FormBorderStyle = 'Fixed3D'
$FenetrePrincipale.StartPosition = 'CenterScreen'
$FenetrePrincipale.TopMost = $false
#==================#
# Paramètre Labels #
#==================#
$InstructionDOMAINE.Size = New-Object System.Drawing.Size(180, 20)
$InstructionDOMAINE.Location = New-Object System.Drawing.Size(20, 20)
$InstructionDOMAINE.Text = \"Merci de sélectionner un domaine : \"
$InstructionDOMAINE.TextAlign = 'MiddleCenter'
#===================#
# Paramètre ListBox #
#===================#
#=/ DOMAINE \=#
$table1 = New-Object System.Data.DataTable
$NomDomaine = New-Object System.Data.DataColumn DOMAINE, ([string])
$AdresseDomaine = New-Object System.Data.DataColumn ADRESSE, ([string])
$DN = New-Object System.Data.DataColumn DN, ([string])
$table1.Columns.Add($NomDomaine)
$table1.Columns.Add($AdresseDomaine)
$table1.Columns.Add($DN)
$ligne = $table1.NewRow()
$ligne.DOMAINE = \"PATRICE\"
$ligne.ADRESSE = \"PATRICE.LAB\"
$ligne.DN = \"DC=PATRICE,DC=LAB\"
$table1.Rows.Add($ligne)
$vu1 = New-Object System.Data.DataView($table1)
$vu1.Sort = \"DOMAINE ASC\"
$ListeDOMAINE.Size = New-Object System.Drawing.Size(150,20)
$ListeDOMAINE.Location = New-Object System.Drawing.Size(220, 20)
$ListeDOMAINE.DropDownStyle = \"DropDownList\"
$ListeDOMAINE.BindingContext = New-Object System.Windows.Forms.BindingContext
$ListeDOMAINE.DataSource = $vu1
$ListeDOMAINE.DisplayMember = \"DOMAINE\"
$ListeDOMAINE.SelectedItem = \"ADRESSE\"
$ListeDOMAINE.SelectedItem = \"DN\"
#====================#
# Paramètre TreeView #
#====================#
Function TreeView
{
param (
$root = [ADSI]\"LDAP://$($ListeDOMAINE.SelectedItem.DN.toLower())\"
)
# the Main function that can be loaded or gets started at the end of the script
Function Browse-ActiveDirectory {
param (
$root = [ADSI]\"LDAP://$($ListeDOMAINE.SelectedItem.DN.toLower())\"
)
# Try to connect to the Domain root
&{trap {throw \"$($_)\"};[void]$Root.get_Name()}
# Make the form
$form = new-object Windows.Forms.form
$form.Size = new-object System.Drawing.Size @(800,600)
$form.text = \"PowerShell ActiveDirectory\"
# Make TreeView to hold the Domain Tree
$TV = new-object windows.forms.TreeView
$TV.Location = new-object System.Drawing.Size(10,30)
$TV.size = new-object System.Drawing.Size(770,470)
$TV.Anchor = \"top, left, right\"
$TV.checkBoxes = $true
Function Get-CheckedNode($node)
{
foreach ($n in $node.nodes)
{
if ($n.checked) { $n.Text }
Get-CheckedNode($n)
}
}
# Add the Button to close the form and return the selected DirectoryEntry
$btnSelect = new-object System.Windows.Forms.Button
$btnSelect.text = \"Select\"
$btnSelect.Location = new-object System.Drawing.Size(710,510)
$btnSelect.size = new-object System.Drawing.Size(70,30)
$btnSelect.Anchor = \"Bottom, right\"
# If Select button pressed set return value to Selected DirectoryEntry and close form
$btnSelect.add_Click({
Get-CheckedNode($TV)
$form.close()
})
# Add Cancel button
$btnCancel = new-object System.Windows.Forms.Button
$btnCancel.text = \"Cancel\"
$btnCancel.Location = new-object System.Drawing.Size(630,510)
$btnCancel.size = new-object System.Drawing.Size(70,30)
$btnCancel.Anchor = \"Bottom, right\"
# If cancel button is clicked set returnvalue to $False and close form
$btnCancel.add_Click({$script:Return = $false ; $form.close()})
# Create a TreeNode for the domain root found
$TNRoot = new-object System.Windows.Forms.TreeNode(\"Root\"«»)
$TNRoot.Name = $root.name
$TNRoot.Text = $root.distinguishedName
$TNRoot.tag = \"NotEnumerated\"
# First time a Node is Selected, enumerate the Children of the selected DirectoryEntry
$TV.add_AfterSelect({
if ($this.SelectedNode.tag -eq \"NotEnumerated\"«»)
{
$de = [ADSI](\"LDAP://$($this.SelectedNode.text)\"«»)
# Add all Children found as Sub Nodes to the selected TreeNode
$de.get_Children() | foreach {
$TN = new-object System.Windows.Forms.TreeNode
$TN.Name = $_.name
$TN.Text = $_.distinguishedName
$TN.tag = \"NotEnumerated\"
$this.SelectedNode.Nodes.Add($TN)
}
# Set tag to show this node is already enumerated
$this.SelectedNode.tag = \"Enumerated\"
}
})
# Add the RootNode to the Treeview
[void]$TV.Nodes.Add($TNRoot)
# Add the Controls to the Form
$form.Controls.Add($TV)
$form.Controls.Add($btnSelect )
$form.Controls.Add($btnCancel )
# Set the Select Button as the Default
$form.AcceptButton = $btnSelect
$Form.Add_Shown({$form.Activate()})
[void]$form.showdialog()
#Return selected DirectoryEntry or $false as Cancel Button is Used
$Resultat.Text = Get-CheckedNode($TV)
}
. Browse-ActiveDirectory $root
$global:ResultatNode = Get-CheckedNode($TV)
}
#=======================#
# Paramètre RichTextBox #
#=======================#
#=/ Resultat Domaine \=#
$ResultatDOMAINE.Size = New-Object System.Drawing.Size(180, 20)
$ResultatDOMAINE.Location = New-Object System.Drawing.Size(550, 20)
$ResultatDOMAINE.ReadOnly = $true
#=/ Résultat \=#
$Resultat.Size = New-Object System.Drawing.Size(740,200)
$Resultat.Location = New-Object System.Drawing.Size(20,520)
$Resultat.ReadOnly = $true
$Resultat.Anchor = 'None'
$Resultat.AutoSize = $true
#===================#
# Paramètre Boutton #
#===================#
#=/ Boutton DOMAINE \=#
$BouttonDOMAINE.Size = New-Object System.Drawing.Size(100,20)
$BouttonDOMAINE.Location = New-Object System.Drawing.Size(400, 20)
$BouttonDOMAINE.Text = \"1 - DOMAINE\"
$BouttonDOMAINE.add_Click({ $ResultatDOMAINE.Text = $ListeDOMAINE.SelectedItem.ADRESSE; })
#=/ Boutton GPO \=#
$BouttonEXPORTGPO.Size = New-Object System.Drawing.Size(120, 20)
$BouttonEXPORTGPO.Location = New-Object System.Drawing.Size(20, 730)
$BouttonEXPORTGPO.Text = \"2 - EXPORT GPO\"
$BouttonEXPORTGPO.Anchor = \"Bottom, Left\"
$BouttonEXPORTGPO.add_Click({
Write-Host $global:ResultatNode
})
#=/ Boutton BACKUPGPO \=#
$BouttonBACKUPGPO.Size = New-Object System.Drawing.Size(120, 20)
$BouttonBACKUPGPO.Location = New-Object System.Drawing.Size(550, 730)
$BouttonBACKUPGPO.Text = \"3 - BACKUP GPO\"
$BouttonBACKUPGPO.Anchor = 'Bottom, Right'
$BouttonBACKUPGPO.add_Click({TreeView # $Resultat.Text = Get-CheckedNode($TV)})
#=/ Boutton QUITTER \=#
$BouttonQUITTER.Size = New-Object System.Drawing.Size(100,20)
$BouttonQUITTER.Location = New-Object System.Drawing.Size(675, 730)
$BouttonQUITTER.Text = \"QUITTER\"
$BouttonQUITTER.Anchor = \"Bottom, Right\"
$BouttonQUITTER.add_Click({$FenetrePrincipale.Close()})
#=================#
# Ajout des Forms #
#=================#
$FenetrePrincipale.Controls.Add($Resultat)
$FenetrePrincipale.Controls.Add($ResultatDOMAINE)
$FenetrePrincipale.Controls.Add($InstructionDOMAINE)
$FenetrePrincipale.Controls.Add($ListeDOMAINE)
$FenetrePrincipale.Controls.Add($BouttonEXPORTGPO)
$FenetrePrincipale.Controls.Add($BouttonBACKUPGPO)
$FenetrePrincipale.Controls.Add($BouttonDOMAINE)
$FenetrePrincipale.Controls.Add($BouttonQUITTER)
#=====================#
# Affichage des Forms #
#=====================#
$FenetrePrincipale.ShowDialog()
[/code:1]
Message édité par: PatriceLT, à: 3/05/17 15:50<br><br>Message édité par: PatriceLT, à: 3/05/17 15:55
J'ai actuellement un soucis de scope concernant une variable qui m'embête car elle ne veut pas se mettre en global...
Voici le script qui est assez long, la partie qui m'intéresse est celle de la fonction TreeView, qui a dedans, une fonction Get-CheckedNode, et j'aimerai retourner la valeur de ce Get-CheckedNode Lorsque je clique sur un bouton (Export-GPO) par exemple.
[code:1]
#===============#
# Import Module #
#===============#
Import-Module ActiveDirectory
Import-Module GroupPolicy
$Snapin = ''
$Snapin = Get-PSSnapIn -Name Quest.ActiveRoles.ADManagement
if(!$Snapin)
{
\"Ajout Du Module PSSnapin AD Quest...\"
Add-PSSnapin Quest.ActiveRoles.ADManagement
\"Module PSSnapin AD Quest Ajouté\"
}
#=================#
# Import Assembly #
#=================#
[void] [system.reflection.Assembly]::LoadWithPartialName(\"System.Windows.Forms\"«»)
[void] [system.reflection.Assembly]::LoadWithPartialName(\"System.Drawing\"«»)
[void] [System.Reflection.Assembly]::LoadWithPartialName(\"System.DirectoryServices\"«»)
#===========#
# Fonctions #
#===========#
#===========#
# Variables #
#===========#
#====================#
# Création des Forms #
#====================#
$FenetrePrincipale = New-Object System.Windows.Forms.Form
$InstructionDOMAINE = New-Object System.Windows.Forms.Label
$ListeDOMAINE = New-Object System.Windows.Forms.ComboBox
$ResultatDOMAINE = New-Object System.Windows.Forms.RichTextBox
$Resultat = New-Object System.Windows.Forms.RichTextBox
$BouttonDOMAINE = New-Object System.Windows.Forms.Button
$BouttonEXPORTGPO = New-Object System.Windows.Forms.Button
$BouttonBACKUPGPO = New-Object System.Windows.Forms.Button
$BouttonQUITTER = New-Object System.Windows.Forms.Button
#=====================#
# Paramètre des Forms #
#=====================#
#=/ Fenetre Principale \=#
$FenetrePrincipale.Size = New-Object System.Drawing.Size(800, 800)
$FenetrePrincipale.FormBorderStyle = 'Fixed3D'
$FenetrePrincipale.StartPosition = 'CenterScreen'
$FenetrePrincipale.TopMost = $false
#==================#
# Paramètre Labels #
#==================#
$InstructionDOMAINE.Size = New-Object System.Drawing.Size(180, 20)
$InstructionDOMAINE.Location = New-Object System.Drawing.Size(20, 20)
$InstructionDOMAINE.Text = \"Merci de sélectionner un domaine : \"
$InstructionDOMAINE.TextAlign = 'MiddleCenter'
#===================#
# Paramètre ListBox #
#===================#
#=/ DOMAINE \=#
$table1 = New-Object System.Data.DataTable
$NomDomaine = New-Object System.Data.DataColumn DOMAINE, ([string])
$AdresseDomaine = New-Object System.Data.DataColumn ADRESSE, ([string])
$DN = New-Object System.Data.DataColumn DN, ([string])
$table1.Columns.Add($NomDomaine)
$table1.Columns.Add($AdresseDomaine)
$table1.Columns.Add($DN)
$ligne = $table1.NewRow()
$ligne.DOMAINE = \"PATRICE\"
$ligne.ADRESSE = \"PATRICE.LAB\"
$ligne.DN = \"DC=PATRICE,DC=LAB\"
$table1.Rows.Add($ligne)
$vu1 = New-Object System.Data.DataView($table1)
$vu1.Sort = \"DOMAINE ASC\"
$ListeDOMAINE.Size = New-Object System.Drawing.Size(150,20)
$ListeDOMAINE.Location = New-Object System.Drawing.Size(220, 20)
$ListeDOMAINE.DropDownStyle = \"DropDownList\"
$ListeDOMAINE.BindingContext = New-Object System.Windows.Forms.BindingContext
$ListeDOMAINE.DataSource = $vu1
$ListeDOMAINE.DisplayMember = \"DOMAINE\"
$ListeDOMAINE.SelectedItem = \"ADRESSE\"
$ListeDOMAINE.SelectedItem = \"DN\"
#====================#
# Paramètre TreeView #
#====================#
Function TreeView
{
param (
$root = [ADSI]\"LDAP://$($ListeDOMAINE.SelectedItem.DN.toLower())\"
)
# the Main function that can be loaded or gets started at the end of the script
Function Browse-ActiveDirectory {
param (
$root = [ADSI]\"LDAP://$($ListeDOMAINE.SelectedItem.DN.toLower())\"
)
# Try to connect to the Domain root
&{trap {throw \"$($_)\"};[void]$Root.get_Name()}
# Make the form
$form = new-object Windows.Forms.form
$form.Size = new-object System.Drawing.Size @(800,600)
$form.text = \"PowerShell ActiveDirectory\"
# Make TreeView to hold the Domain Tree
$TV = new-object windows.forms.TreeView
$TV.Location = new-object System.Drawing.Size(10,30)
$TV.size = new-object System.Drawing.Size(770,470)
$TV.Anchor = \"top, left, right\"
$TV.checkBoxes = $true
Function Get-CheckedNode($node)
{
foreach ($n in $node.nodes)
{
if ($n.checked) { $n.Text }
Get-CheckedNode($n)
}
}
# Add the Button to close the form and return the selected DirectoryEntry
$btnSelect = new-object System.Windows.Forms.Button
$btnSelect.text = \"Select\"
$btnSelect.Location = new-object System.Drawing.Size(710,510)
$btnSelect.size = new-object System.Drawing.Size(70,30)
$btnSelect.Anchor = \"Bottom, right\"
# If Select button pressed set return value to Selected DirectoryEntry and close form
$btnSelect.add_Click({
Get-CheckedNode($TV)
$form.close()
})
# Add Cancel button
$btnCancel = new-object System.Windows.Forms.Button
$btnCancel.text = \"Cancel\"
$btnCancel.Location = new-object System.Drawing.Size(630,510)
$btnCancel.size = new-object System.Drawing.Size(70,30)
$btnCancel.Anchor = \"Bottom, right\"
# If cancel button is clicked set returnvalue to $False and close form
$btnCancel.add_Click({$script:Return = $false ; $form.close()})
# Create a TreeNode for the domain root found
$TNRoot = new-object System.Windows.Forms.TreeNode(\"Root\"«»)
$TNRoot.Name = $root.name
$TNRoot.Text = $root.distinguishedName
$TNRoot.tag = \"NotEnumerated\"
# First time a Node is Selected, enumerate the Children of the selected DirectoryEntry
$TV.add_AfterSelect({
if ($this.SelectedNode.tag -eq \"NotEnumerated\"«»)
{
$de = [ADSI](\"LDAP://$($this.SelectedNode.text)\"«»)
# Add all Children found as Sub Nodes to the selected TreeNode
$de.get_Children() | foreach {
$TN = new-object System.Windows.Forms.TreeNode
$TN.Name = $_.name
$TN.Text = $_.distinguishedName
$TN.tag = \"NotEnumerated\"
$this.SelectedNode.Nodes.Add($TN)
}
# Set tag to show this node is already enumerated
$this.SelectedNode.tag = \"Enumerated\"
}
})
# Add the RootNode to the Treeview
[void]$TV.Nodes.Add($TNRoot)
# Add the Controls to the Form
$form.Controls.Add($TV)
$form.Controls.Add($btnSelect )
$form.Controls.Add($btnCancel )
# Set the Select Button as the Default
$form.AcceptButton = $btnSelect
$Form.Add_Shown({$form.Activate()})
[void]$form.showdialog()
#Return selected DirectoryEntry or $false as Cancel Button is Used
$Resultat.Text = Get-CheckedNode($TV)
}
. Browse-ActiveDirectory $root
$global:ResultatNode = Get-CheckedNode($TV)
}
#=======================#
# Paramètre RichTextBox #
#=======================#
#=/ Resultat Domaine \=#
$ResultatDOMAINE.Size = New-Object System.Drawing.Size(180, 20)
$ResultatDOMAINE.Location = New-Object System.Drawing.Size(550, 20)
$ResultatDOMAINE.ReadOnly = $true
#=/ Résultat \=#
$Resultat.Size = New-Object System.Drawing.Size(740,200)
$Resultat.Location = New-Object System.Drawing.Size(20,520)
$Resultat.ReadOnly = $true
$Resultat.Anchor = 'None'
$Resultat.AutoSize = $true
#===================#
# Paramètre Boutton #
#===================#
#=/ Boutton DOMAINE \=#
$BouttonDOMAINE.Size = New-Object System.Drawing.Size(100,20)
$BouttonDOMAINE.Location = New-Object System.Drawing.Size(400, 20)
$BouttonDOMAINE.Text = \"1 - DOMAINE\"
$BouttonDOMAINE.add_Click({ $ResultatDOMAINE.Text = $ListeDOMAINE.SelectedItem.ADRESSE; })
#=/ Boutton GPO \=#
$BouttonEXPORTGPO.Size = New-Object System.Drawing.Size(120, 20)
$BouttonEXPORTGPO.Location = New-Object System.Drawing.Size(20, 730)
$BouttonEXPORTGPO.Text = \"2 - EXPORT GPO\"
$BouttonEXPORTGPO.Anchor = \"Bottom, Left\"
$BouttonEXPORTGPO.add_Click({
Write-Host $global:ResultatNode
})
#=/ Boutton BACKUPGPO \=#
$BouttonBACKUPGPO.Size = New-Object System.Drawing.Size(120, 20)
$BouttonBACKUPGPO.Location = New-Object System.Drawing.Size(550, 730)
$BouttonBACKUPGPO.Text = \"3 - BACKUP GPO\"
$BouttonBACKUPGPO.Anchor = 'Bottom, Right'
$BouttonBACKUPGPO.add_Click({TreeView # $Resultat.Text = Get-CheckedNode($TV)})
#=/ Boutton QUITTER \=#
$BouttonQUITTER.Size = New-Object System.Drawing.Size(100,20)
$BouttonQUITTER.Location = New-Object System.Drawing.Size(675, 730)
$BouttonQUITTER.Text = \"QUITTER\"
$BouttonQUITTER.Anchor = \"Bottom, Right\"
$BouttonQUITTER.add_Click({$FenetrePrincipale.Close()})
#=================#
# Ajout des Forms #
#=================#
$FenetrePrincipale.Controls.Add($Resultat)
$FenetrePrincipale.Controls.Add($ResultatDOMAINE)
$FenetrePrincipale.Controls.Add($InstructionDOMAINE)
$FenetrePrincipale.Controls.Add($ListeDOMAINE)
$FenetrePrincipale.Controls.Add($BouttonEXPORTGPO)
$FenetrePrincipale.Controls.Add($BouttonBACKUPGPO)
$FenetrePrincipale.Controls.Add($BouttonDOMAINE)
$FenetrePrincipale.Controls.Add($BouttonQUITTER)
#=====================#
# Affichage des Forms #
#=====================#
$FenetrePrincipale.ShowDialog()
[/code:1]
Message édité par: PatriceLT, à: 3/05/17 15:50<br><br>Message édité par: PatriceLT, à: 3/05/17 15:55
Connexion ou Créer un compte pour participer à la conversation.
- PatriceLT
- Auteur du sujet
- Visiteur
-
il y a 9 ans 3 semaines #23556
par PatriceLT
Réponse de PatriceLT sur le sujet Re:valeur fonction dans plusieurs fonctions
J'ai trouvé, il s'agissait en fait de mettre en fin de function TreeView $global:ResultatNode = GetCheckedNode($TV)
Et d'appeler $global:ResultatNode lorsque je clique sur le bouton Exporter-GPO par exemple.
Ca a l'air de fonctionner.
Si ça peut en aider certains.
Et d'appeler $global:ResultatNode lorsque je clique sur le bouton Exporter-GPO par exemple.
Ca a l'air de fonctionner.
Si ça peut en aider certains.
Connexion ou Créer un compte pour participer à la conversation.
Temps de génération de la page : 0.036 secondes
- Vous êtes ici :
-
Accueil
-
forum
-
PowerShell
-
Entraide pour les débutants
- valeur fonction dans plusieurs fonctions