Récupération données Active Director

Occasional Contributor

Bonjour à tous,

 

Je viens vous afin de m'aider concernant un sujet que je ne comprend pas d'où vient le souci. 

Voilà, je développe un petit logiciel (basique) en PowerShell afin de faire des extract de l'AD vers un fichier .csv. Le logiciel permet de sélectionner les options souhaitées à extraire. 
Parmi ces options, une ne fonctionne pas. La clé de récupération Bitlocker. 

 

Voici une partie de mon code:

 

#Initialisation du tableau à exporter
$tabcomputers=@()

#stockage du nom du domaine pour le nom du fichier .csv
$CsvName = $listBoxComputers.SelectedValue 

#création de la bdd des postes dans l'AD
$ADComputersSource = Get-ADComputer -Server $listBox.SelectedValue -Filter * -Properties * 

#vérifie si l'accès au server $listbox.selectedvalue est OK
if($ADComputersSource) 
{
     $objStatusBarComputers.Text = "Checking Active Directory..."

     #boucle sur chaque poste dans la source ADComputerSource
     foreach($computers in $ADComputersSource) 
     {
          #récupère la clé bitlocker pour chaque poste dans la source ADComputersSource
          $Bitlocker=(Get-ADObject -SearchBase (Get-ADComputer -Filter {name -like 
          $computers.Name}) -Filter {objectClass -eq "msFVE-RecoveryInformation"}  
          -Properties msFVE-RecoveryPassword).'msFVE-RecoveryPassword' 
           
          #création d'une entrée pour le tableau
          $ComputerObject = New-Object psobject 

          #ajout du nom de chaque poste dans l'objet $ComputerObject
          $ComputerObject | Add-Member -Name "Name" -MemberType NoteProperty -Value 
          $computers.name -Force

          #ajout du FQDN dans l'objet $ComputerObject
          $ComputerObject | Add-Member -Name "DNS Host Name" -MemberType NoteProperty - 
          Value $computers.dNSHostName -Force

          #ajout du statut actif de chaque poste dans l'objet $ComputerObject
          $ComputerObject | Add-Member -Name "Enabled" -MemberType NoteProperty -Value 
          $($computers.enabled) -Force

          #vérification si l'option OU est coché
          if($checkbox_computers_OU.Checked) 
          {
               #ajout de la localisation AD de l'objet dans l'objet $ComputerObject
               $ComputerObject | Add-Member -Name "Distinguishedname" -MemberType 
               NoteProperty -Value $computers.distinguishedname -Force 
          }
          
          #Vérification si l'option lastlogondate est coché
          if($checkbox_computers_lastlogondate.Checked) 
          {
               #ajout de la dernière connexion du poste dans l'objet $ComputerObject
               $ComputerObject | Add-Member -Name "LastLogondate" -MemberType 
               NoteProperty -Value $computers.lastlogondate -Force 
          }

          #vérification si l'option bitlocker est coché
          if($checkbox_computers_bitlocker.Checked) 
          {
               #ajout de la clé de récupération stockée dans la variable $bitlocker dans 
               #l'objet $ComputerObject
               $ComputerObject | Add-Member -Name "BitLocker Recovery Key" -MemberType 
               NoteProperty -Value $Bitlocker -Force 
          }

          #ajout de l'objet $ComputerObject dans le tableau $tabcomputers
          $tabcomputers += $ComputerObject
          $objStatusBarComputers.Text = "Done"

          #Export du tableau $tabcomputers vers un fichier csv
          $tabcomputers | Export-Csv -Path "Export_AD_Computers_$CsvName.csv" -Delimiter 
          ";" -NoTypeInformation 
     }#end foreach
}#end if

 

 

Bien sur j'ai testé le contenu de ma variable bitlocker en ajoutant un write-host $bitlocker dans mon if($checkbox_computers_bitlocker.checked). Cela me retourne bien les clés de récupération. Cependant, mon csv contient bien la colonne bitlocker mais aucune valeur n'est inscrite dans le tableau. 

Avez-vous une idée ? 

Merci à vous pour votre aide. 

Cordialement,
Mika

10 Replies

Hello @Mike9375,

When you are getting Bitlocker key you are passing whole ADComputer object instead of DistinguishedName to -SearchBase parameter..

Try this:

$Bitlocker=(Get-ADObject -SearchBase (Get-ADComputer -Filter {name -like $computers.Name} | Select-Object -ExpandProperty DistinguishedName) -Filter {objectClass -eq "msFVE-RecoveryInformation"} -Properties msFVE-RecoveryPassword).'msFVE-RecoveryPassword'

Hope that helps.

Bonjour à tous, 

petit up. 

Quelqu'un pour une idée svp ? 

Merci. 

@Mike9375,

Did you try my suggestion?

avez-vous essayé ma suggestion?

@AndySvints 

Hi,

 

My bad, i forget to answer. 

 

I try it yes. But it's not working. 

My first code work in Powershell ISE but not when i export this information in csv. I don't know why. 

Hello,

 

Please can you help me ?

 

Regards

Would you be so kind to provide more details?

You are running same code from ISE and Console and receive different results?
Are you running script directly from Domain Controller or from domain joined computer/server?
What the business goal /use case?
Get Bitlocker password for computers from Ad?
Get data for report/for backup or for processing?

@AndySvints 

Hi,

 

I try my code on ISE and also with the console and i have the same results. 

 

The problem is when i want export the data with Export-csv. The bitlocker data is not present in my csv file. But the others AD data is present (lastlogon, enabled, Distinguishedname...).

 

When i try my code outside of my principal script. It's working. 

 

I try this code: 

 

cls
$ADComputersSource = Get-ADComputer -Server carmignac -Filter * -Properties *
$tabbitlocker=@()

foreach($computer in $ADComputersSource){
    $tab = New-Object psobject
    $tab | Add-Member -Name "Bitlocker Recovery" -MemberType NoteProperty -Value 
    $computer.'ms-Mcs-AdmPwd' -Force
    $tabbitlocker+=$tab
}

$tabbitlocker | Export-Csv -Path ".\test.csv" -NoTypeInformation -Delimiter ";"

 

This code is working but... when i had this code in principal script... it's not working and in my csv file, i don't have any informations for bitlocker. 

 

the goal is to create a report on PCs with a Bitlocker key on the AD for Projet Manager. 

 

Regards.

@Mike9375 ,

Oh I see.

So the code snippet you provided above is getting you the expected result. The problem is that when you have this piece of code in you main script it is not throwing you the error but  providing empty csv column for the Bitlocker info.

Based on the me of variables in your initial question your main script is presenting GUI. Is it possible to show a little bit more code from the main script?

 

@AndySvints 

 

Yes. It's my full script: 

 

cls

#region WinForm
#appel de la classe WinFom
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
#endregion

#region windows
#Création fenêtre principale
$Form = New-Object System.Windows.Forms.Form
$Form.StartPosition='CenterScreen'
$Form.ClientSize = '250,250'
$Form.Text = "Extract Active Directory"
$Form.MaximizeBox = $false
$Form.MinimizeBox = $false
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$Form.BackColor = "#54010b"

#Création fenêtre users
$FormUsers = New-Object System.Windows.Forms.Form
$FormUsers.StartPosition='CenterScreen'
$FormUsers.ClientSize = '321,291'
$FormUsers.Text = "Extract Users Active Directory"
$FormUsers.MaximizeBox = $false
$FormUsers.MinimizeBox = $false
$FormUsers.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$FormUsers.BackColor = "#54010b"

#Création fenêtre computers
$FormComputers = New-Object System.Windows.Forms.Form
$FormComputers.StartPosition='CenterScreen'
$FormComputers.ClientSize = '320,291'
$FormComputers.Text = "Extract Computers Active Directory"
$FormComputers.MaximizeBox = $false
$FormComputers.MinimizeBox = $false
$FormComputers.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$FormComputers.BackColor = "#54010b"
#endregion

#region principal window
#Création bouton Users
$button_users = New-Object System.Windows.Forms.Button
$button_users.Text = "USERS"
$button_users.Size = New-Object System.Drawing.Size(150,40)
$button_users.Location = New-Object System.Drawing.Size(50,50)
$button_users.BackColor = "#808080"
$button_users.ForeColor = "white"

#Création bouton Computers
$button_computers = New-Object System.Windows.Forms.Button
$button_computers.Text = "COMPUTERS"
$button_computers.Size = New-Object System.Drawing.Size(150,40)
$button_computers.Location = New-Object System.Drawing.Size(50,100)
$button_computers.BackColor = "#808080"
$button_computers.ForeColor = "white"

#Création bouton Cancel
$button_cancel = New-Object System.Windows.Forms.Button
$button_cancel.Text = "CANCEL"
$button_cancel.Size = New-Object System.Drawing.Size(150,40)
$button_cancel.Location = New-Object System.Drawing.Size(50,150)
$button_cancel.BackColor = "#808080"
$button_cancel.ForeColor = "white"
#endregion

# form status bar Users
$objStatusBar = New-Object System.Windows.Forms.StatusBar
$objStatusBar.Name = "statusBar"
$objStatusBar.Text = "Ready"

# form status bar Computers
$objStatusBarComputers = New-Object System.Windows.Forms.StatusBar
$objStatusBarComputers.Name = "statusBar"
$objStatusBarComputers.Text = "Ready"

#region List box domain Users
#Création de la table de données (DataTable).
$table1 = New-Object system.Data.DataTable

#Définition des 2 colonnes (Nom, Type).
$colonne1 = New-Object system.Data.DataColumn Domain,([string])
$colonne2 = New-Object system.Data.DataColumn Nom,([string])

#Création des colonnes dans la table de données.
$table1.columns.add($colonne1)
$table1.columns.add($colonne2)

#Ajoute les données ligne par ligne dans la table de données.
$ligne = $table1.NewRow()   #Création de la nouvelle ligne.
$ligne.Domain = "domain1"    #Dans la colonne extension on met la valeur ".fr".
$ligne.Nom = "domain1"      #Dans la colonne pays on met la valeur "France".
$table1.Rows.Add($ligne)    #Ajoute la nouvelle ligne dans la table de données.
$ligne = $table1.NewRow()
$ligne.Domain = "domain2"
$ligne.Nom = "domain2"
$table1.Rows.Add($ligne)

#Création de la vue.
$vu1 = New-Object System.Data.DataView($table1)

#Création de la liste déroulante (ComboBox).
$listBox = New-Object System.Windows.Forms.ComboBox
$listBox.Location = New-Object System.Drawing.Point(60,10) 
$listBox.Size = New-Object System.Drawing.Size(150,50)

#Associe les données à la liste déroulante.
#Pour cela il faut créer un "Binding Context" sur le contrôle.
$listBox.BindingContext = New-Object System.Windows.Forms.BindingContext
$listBox.DataSource = $vu1         #Affecte la vue qui contient les données triées.
$listBox.DisplayMember = "Nom"    #Colonne qui sera affiché (Pays). 
$listBox.ValueMember = "Domain" #Colonne qui sera retourné (Extension).
$listBox.SelectedValue = "Domain1"     #Valeur par défaut.
#endregion

#region List box domain Computers
#Création de la table de données (DataTable).
$table1 = New-Object system.Data.DataTable

#Définition des 2 colonnes (Nom, Type).
$colonne1 = New-Object system.Data.DataColumn Domain,([string])
$colonne2 = New-Object system.Data.DataColumn Nom,([string])

#Création des colonnes dans la table de données.
$table1.columns.add($colonne1)
$table1.columns.add($colonne2)

#Ajoute les données ligne par ligne dans la table de données.
$ligne = $table1.NewRow()   #Création de la nouvelle ligne.
$ligne.Domain = "Domain1"    #Dans la colonne extension on met la valeur ".fr".
$ligne.Nom = "Domain1"      #Dans la colonne pays on met la valeur "France".
$table1.Rows.Add($ligne)    #Ajoute la nouvelle ligne dans la table de données.
$ligne = $table1.NewRow()
$ligne.Domain = "Domain2"
$ligne.Nom = "Domain2"
$table1.Rows.Add($ligne)

#Création de la vue.
$vu1 = New-Object System.Data.DataView($table1)

#Création de la liste déroulante (ComboBox).
$listBoxComputers = New-Object System.Windows.Forms.ComboBox
$listBoxComputers.Location = New-Object System.Drawing.Point(60,10) 
$listBoxComputers.Size = New-Object System.Drawing.Size(150,50)

#Associe les données à la liste déroulante.
#Pour cela il faut créer un "Binding Context" sur le contrôle.
$listBoxComputers.BindingContext = New-Object System.Windows.Forms.BindingContext
$listBoxComputers.DataSource = $vu1         #Affecte la vue qui contient les données triées.
$listBoxComputers.DisplayMember = "Nom"    #Colonne qui sera affiché (Pays). 
$listBoxComputers.ValueMember = "Domain" #Colonne qui sera retourné (Extension).
$listBoxComputers.SelectedValue = "Domain1"     #Valeur par défaut.
#endregion

#region users window
#Création fenêtre users
$button_users_extract = New-Object System.Windows.Forms.Button
$button_users_extract.Text = "EXTRACT"
$button_users_extract.Size = New-Object System.Drawing.Size(100,40)
$button_users_extract.Location = New-Object System.Drawing.Size(40,220)
$button_users_extract.BackColor = "#808080"
$button_users_extract.ForeColor = "white"

$button_users_cancel = New-Object System.Windows.Forms.Button
$button_users_cancel.Text = "CANCEL"
$button_users_cancel.Size = New-Object System.Drawing.Size(100,40)
$button_users_cancel.Location = New-Object System.Drawing.Size(180,220)
$button_users_cancel.BackColor = "#808080"
$button_users_cancel.ForeColor = "white"

$label_users_options = New-Object System.Windows.Forms.label
$label_users_options.Text = "----------------------------         OPTIONS         ----------------------------"
$label_users_options.Size = New-Object System.Drawing.Size(320,20)
$label_users_options.Location = New-Object System.Drawing.Size(0,40)
$label_users_options.ForeColor = "white"

$label_users_domain = New-Object System.Windows.Forms.label
$label_users_domain.Text = "Domain :"
$label_users_domain.Size = New-Object System.Drawing.Size(100,30)
$label_users_domain.Location = New-Object System.Drawing.Size(1,14)
$label_users_domain.ForeColor = "white"

$checkbox_users_enabled = New-Object System.Windows.Forms.CheckBox
$checkbox_users_enabled.Text = "Enabled Only"
$checkbox_users_enabled.Size = New-Object System.Drawing.Size(100,60)
$checkbox_users_enabled.Location = New-Object System.Drawing.Size(10,40)
$checkbox_users_enabled.Checked = $false
$checkbox_users_enabled.BackColor = "Transparent"
$checkbox_users_enabled.ForeColor = "white"

$checkbox_users_email = New-Object System.Windows.Forms.CheckBox
$checkbox_users_email.Text = "Email"
$checkbox_users_email.Size = New-Object System.Drawing.Size(80,60)
$checkbox_users_email.Location = New-Object System.Drawing.Size(115,40)
$checkbox_users_email.Checked = $false
$checkbox_users_email.BackColor = "Transparent"
$checkbox_users_email.ForeColor = "white"

$checkbox_users_lastlogondate = New-Object System.Windows.Forms.CheckBox
$checkbox_users_lastlogondate.Text = "LastLogonDate"
$checkbox_users_lastlogondate.Size = New-Object System.Drawing.Size(100,60)
$checkbox_users_lastlogondate.Location = New-Object System.Drawing.Size(200,40)
$checkbox_users_lastlogondate.Checked = $false
$checkbox_users_lastlogondate.BackColor = "Transparent"
$checkbox_users_lastlogondate.ForeColor = "white"

$checkbox_users_Country = New-Object System.Windows.Forms.CheckBox
$checkbox_users_Country.Text = "Country"
$checkbox_users_Country.Size = New-Object System.Drawing.Size(100,60)
$checkbox_users_Country.Location = New-Object System.Drawing.Size(10,80)
$checkbox_users_Country.Checked = $false
$checkbox_users_Country.BackColor = "Transparent"
$checkbox_users_Country.ForeColor = "white"

$checkbox_users_company = New-Object System.Windows.Forms.CheckBox
$checkbox_users_company.Text = "Company"
$checkbox_users_company.Size = New-Object System.Drawing.Size(80,60)
$checkbox_users_company.Location = New-Object System.Drawing.Size(115,80)
$checkbox_users_company.Checked = $false
$checkbox_users_company.BackColor = "Transparent"
$checkbox_users_company.ForeColor = "white"

$checkbox_users_department = New-Object System.Windows.Forms.CheckBox
$checkbox_users_department.Text = "Department"
$checkbox_users_department.Size = New-Object System.Drawing.Size(100,60)
$checkbox_users_department.Location = New-Object System.Drawing.Size(200,80)
$checkbox_users_department.Checked = $false
$checkbox_users_department.BackColor = "Transparent"
$checkbox_users_department.ForeColor = "white"

$checkbox_users_OU = New-Object System.Windows.Forms.CheckBox
$checkbox_users_OU.Text = "OU Location"
$checkbox_users_OU.Size = New-Object System.Drawing.Size(100,60)
$checkbox_users_OU.Location = New-Object System.Drawing.Size(10,120)
$checkbox_users_OU.Checked = $false
$checkbox_users_OU.BackColor = "Transparent"
$checkbox_users_OU.ForeColor = "white"

$checkbox_users_Site = New-Object System.Windows.Forms.CheckBox
$checkbox_users_Site.Text = "Site"
$checkbox_users_Site.Size = New-Object System.Drawing.Size(80,60)
$checkbox_users_Site.Location = New-Object System.Drawing.Size(115,120)
$checkbox_users_Site.Checked = $false
$checkbox_users_Site.BackColor = "Transparent"
$checkbox_users_Site.ForeColor = "white"

$checkbox_users_streetaddress = New-Object System.Windows.Forms.CheckBox
$checkbox_users_streetaddress.Text = "Street Address"
$checkbox_users_streetaddress.Size = New-Object System.Drawing.Size(100,60)
$checkbox_users_streetaddress.Location = New-Object System.Drawing.Size(200,120)
$checkbox_users_streetaddress.Checked = $false
$checkbox_users_streetaddress.BackColor = "Transparent"
$checkbox_users_streetaddress.ForeColor = "white"

$checkbox_users_telephonenumber = New-Object System.Windows.Forms.CheckBox
$checkbox_users_telephonenumber.Text = "Ext Number"
$checkbox_users_telephonenumber.Size = New-Object System.Drawing.Size(105,60)
$checkbox_users_telephonenumber.Location = New-Object System.Drawing.Size(10,160)
$checkbox_users_telephonenumber.Checked = $false
$checkbox_users_telephonenumber.BackColor = "Transparent"
$checkbox_users_telephonenumber.ForeColor = "white"

$checkbox_users_ipphone = New-Object System.Windows.Forms.CheckBox
$checkbox_users_ipphone.Text = "Int Number"
$checkbox_users_ipphone.Size = New-Object System.Drawing.Size(80,60)
$checkbox_users_ipphone.Location = New-Object System.Drawing.Size(115,160)
$checkbox_users_ipphone.Checked = $false
$checkbox_users_ipphone.BackColor = "Transparent"
$checkbox_users_ipphone.ForeColor = "white"

$checkbox_users_mobile = New-Object System.Windows.Forms.CheckBox
$checkbox_users_mobile.Text = "Mobile Number"
$checkbox_users_mobile.Size = New-Object System.Drawing.Size(100,60)
$checkbox_users_mobile.Location = New-Object System.Drawing.Size(200,160)
$checkbox_users_mobile.Checked = $false
$checkbox_users_mobile.BackColor = "Transparent"
$checkbox_users_mobile.ForeColor = "white"
#endregion

#region computers window
$button_computers_extract = New-Object System.Windows.Forms.Button
$button_computers_extract.Text = "EXTRACT"
$button_computers_extract.Size = New-Object System.Drawing.Size(100,40)
$button_computers_extract.Location = New-Object System.Drawing.Size(40,220)
$button_computers_extract.BackColor = "#808080"
$button_computers_extract.ForeColor = "white"

$button_computers_cancel = New-Object System.Windows.Forms.Button
$button_computers_cancel.Text = "CANCEL"
$button_computers_cancel.Size = New-Object System.Drawing.Size(100,40)
$button_computers_cancel.Location = New-Object System.Drawing.Size(180,220)
$button_computers_cancel.BackColor = "#808080"
$button_computers_cancel.ForeColor = "white"

$label_computers_options = New-Object System.Windows.Forms.label
$label_computers_options.Text = "----------------------------         OPTIONS         ----------------------------"
$label_computers_options.Size = New-Object System.Drawing.Size(320,20)
$label_computers_options.Location = New-Object System.Drawing.Size(0,40)
$label_computers_options.ForeColor = "white"

$label_computers_domain = New-Object System.Windows.Forms.label
$label_computers_domain.Text = "Domain :"
$label_computers_domain.Size = New-Object System.Drawing.Size(100,30)
$label_computers_domain.Location = New-Object System.Drawing.Size(1,14)
$label_computers_domain.ForeColor = "white"

$checkbox_computers_enabled = New-Object System.Windows.Forms.CheckBox
$checkbox_computers_enabled.Text = "Enabled Only"
$checkbox_computers_enabled.Size = New-Object System.Drawing.Size(100,60)
$checkbox_computers_enabled.Location = New-Object System.Drawing.Size(10,40)
$checkbox_computers_enabled.Checked = $false
$checkbox_computers_enabled.BackColor = "Transparent"
$checkbox_computers_enabled.ForeColor = "white"

$checkbox_computers_OU = New-Object System.Windows.Forms.CheckBox
$checkbox_computers_OU.Text = "OU Location"
$checkbox_computers_OU.Size = New-Object System.Drawing.Size(90,60)
$checkbox_computers_OU.Location = New-Object System.Drawing.Size(115,40)
$checkbox_computers_OU.Checked = $false
$checkbox_computers_OU.BackColor = "Transparent"
$checkbox_computers_OU.ForeColor = "white"

$checkbox_computers_lastlogondate = New-Object System.Windows.Forms.CheckBox
$checkbox_computers_lastlogondate.Text = "Last Logon"
$checkbox_computers_lastlogondate.Size = New-Object System.Drawing.Size(100,60)
$checkbox_computers_lastlogondate.Location = New-Object System.Drawing.Size(210,40)
$checkbox_computers_lastlogondate.Checked = $false
$checkbox_computers_lastlogondate.BackColor = "Transparent"
$checkbox_computers_lastlogondate.ForeColor = "white"

$checkbox_computers_bitlocker = New-Object System.Windows.Forms.CheckBox
$checkbox_computers_bitlocker.Text = "Bitlocker Recory Key"
$checkbox_computers_bitlocker.Size = New-Object System.Drawing.Size(130,60)
$checkbox_computers_bitlocker.Location = New-Object System.Drawing.Size(10,80)
$checkbox_computers_bitlocker.Checked = $false
$checkbox_computers_bitlocker.BackColor = "Transparent"
$checkbox_computers_bitlocker.ForeColor = "white"

$checkbox_computers_localpassword = New-Object System.Windows.Forms.CheckBox
$checkbox_computers_localpassword.Text = "Local Password"
$checkbox_computers_localpassword.Size = New-Object System.Drawing.Size(130,60)
$checkbox_computers_localpassword.Location = New-Object System.Drawing.Size(165,80)
$checkbox_computers_localpassword.Checked = $false
$checkbox_computers_localpassword.BackColor = "Transparent"
$checkbox_computers_localpassword.ForeColor = "white"
#endregion

#region add_click_boutons
#principal window
$button_users.add_Click({
    $FormUsers.ShowDialog()
})

$button_computers.add_Click({
    $FormComputers.ShowDialog()
})

$button_cancel.add_Click({
    $Form.Close()
})

#users window
$button_users_extract.add_Click({
   
    $tab=@()
    $CsvName = $listBox.SelectedValue
    
    if($checkbox_users_enabled.Checked)
    {
        $ADUserSource = Get-ADUser -Server $listBox.SelectedValue -Filter {enabled -eq $true} -Properties *,"msDS-UserPasswordExpiryTimeComputed"
        if($ADUserSource)
        {
            $objStatusBar.Text = "Checking Active Directory..."
            foreach($collabs in $ADUserSource)
            {
                $collabUsers = New-Object psobject
                $collabUsers | Add-Member -Name "Displayname" -MemberType NoteProperty -Value $collabs.Displayname -Force
                $collabUsers | Add-Member -Name "Givenname" -MemberType NoteProperty -Value $collabs.Givenname -Force
                $collabUsers | Add-Member -Name "Name" -MemberType NoteProperty -Value $collabs.name -Force
                $collabUsers | Add-Member -Name "Samaccountname" -MemberType NoteProperty -Value $collabs.samaccountname -Force
                $collabUsers | Add-Member -Name "Enabled" -MemberType NoteProperty -Value $($collabs.enabled) -Force
            
                if($checkbox_users_email.Checked)
                {
                    $collabUsers | Add-Member -Name "Email" -MemberType NoteProperty -Value $collabs.mail -Force
                }
                if($checkbox_users_lastlogondate.Checked)
                {
                    $collabUsers | Add-Member -Name "Lastlogondate" -MemberType NoteProperty -Value $collabs.lastlogondate -Force
                }
                if($checkbox_users_Country.Checked)
                {
                    $collabUsers | Add-Member -Name "Country" -MemberType NoteProperty -Value $collabs.co -Force
                }
                if($checkbox_users_company.Checked)
                {
                    $collabUsers | Add-Member -Name "Company" -MemberType NoteProperty -Value $collabs.company -Force
                }
                if($checkbox_users_department.Checked)
                {
                    $collabUsers | Add-Member -Name "Company" -MemberType NoteProperty -Value $collabs.department -Force
                }
                if($checkbox_users_OU.Checked)
                {
                    $collabUsers | Add-Member -Name "Distinguidname" -MemberType NoteProperty -Value $collabs.distinguishedname -Force
                }
                if($checkbox_users_Site.Checked)
                {
                    $collabUsers | Add-Member -Name "Site" -MemberType NoteProperty -Value $collabs.l -Force
                }
                if($checkbox_users_streetaddress.Checked)
                {
                    $collabUsers | Add-Member -Name "Street Address" -MemberType NoteProperty -Value $collabs.streetaddress -Force
                }
                if($checkbox_users_telephonenumber.Checked)
                {
                    $collabUsers | Add-Member -Name "External number" -MemberType NoteProperty -Value $collabs.telephonenumber -Force
                }
                if($checkbox_users_ipphone.Checked)
                {
                    $collabUsers | Add-Member -Name "Internal number" -MemberType NoteProperty -Value $collabs.ipphone -Force
                }
                if($checkbox_users_mobile.Checked)
                {
                    $collabUsers | Add-Member -Name "Mobile Number" -MemberType NoteProperty -Value $collabs.mobile -Force
                }
                $tab += $collabUsers
                $objStatusBar.Text = "Done"
                $tab | Export-Csv -Path "Export_AD_Users_$CsvName.csv" -Delimiter ";" -NoTypeInformation
            }
        }
        else
        {
            $objStatusBar.Text = "ERROR: Serveur " + $listBox.SelectedValue + " indisponible"
        }
    }
    else
    {
        $ADUserSource = Get-ADUser -Server $listBox.SelectedValue -Filter * -Properties *,"msDS-UserPasswordExpiryTimeComputed"
        if($ADUserSource)
        {
            $objStatusBar.Text = "Checking Active Directory..."
            foreach($collabs in $ADUserSource)
            {
                $collabUsers = New-Object psobject
                $collabUsers | Add-Member -Name "Displayname" -MemberType NoteProperty -Value $collabs.Displayname -Force
                $collabUsers | Add-Member -Name "Givenname" -MemberType NoteProperty -Value $collabs.Givenname -Force
                $collabUsers | Add-Member -Name "Name" -MemberType NoteProperty -Value $collabs.name -Force
                $collabUsers | Add-Member -Name "Samaccountname" -MemberType NoteProperty -Value $collabs.samaccountname -Force
                $collabUsers | Add-Member -Name "Enabled" -MemberType NoteProperty -Value $($collabs.enabled) -Force
                
                if($checkbox_users_email.Checked)
                {
                    $collabUsers | Add-Member -Name "Email" -MemberType NoteProperty -Value $collabs.mail -Force
                }
                if($checkbox_users_lastlogondate.Checked)
                {
                    $collabUsers | Add-Member -Name "lastlogondate" -MemberType NoteProperty -Value $collabs.lastlogondate -Force
                }
                if($checkbox_users_Country.Checked)
                {
                    $collabUsers | Add-Member -Name "Country" -MemberType NoteProperty -Value $collabs.co -Force
                }
                if($checkbox_users_company.Checked)
                {
                    $collabUsers | Add-Member -Name "Company" -MemberType NoteProperty -Value $collabs.company -Force
                }
                if($checkbox_users_department.Checked)
                {
                    $collabUsers | Add-Member -Name "Company" -MemberType NoteProperty -Value $collabs.department -Force
                }
                if($checkbox_users_OU.Checked)
                {
                    $collabUsers | Add-Member -Name "Distinguidname" -MemberType NoteProperty -Value $collabs.distinguishedname -Force
                }
                if($checkbox_users_Site.Checked)
                {
                    $collabUsers | Add-Member -Name "Site" -MemberType NoteProperty -Value $collabs.l -Force
                }
                if($checkbox_users_streetaddress.Checked)
                {
                    $collabUsers | Add-Member -Name "Street Address" -MemberType NoteProperty -Value $collabs.streetaddress -Force
                }
                if($checkbox_users_telephonenumber.Checked)
                {
                    $collabUsers | Add-Member -Name "External number" -MemberType NoteProperty -Value $collabs.telephonenumber -Force
                }
                if($checkbox_users_ipphone.Checked)
                {
                    $collabUsers | Add-Member -Name "Internal number" -MemberType NoteProperty -Value $collabs.ipphone -Force
                }
                if($checkbox_users_mobile.Checked)
                {
                    $collabUsers | Add-Member -Name "Mobile Number" -MemberType NoteProperty -Value $collabs.mobile -Force
                }
                $tab += $collabUsers
                $objStatusBar.Text = "Done"
                $tab | Export-Csv -Path "Export_AD_Users_$CsvName.csv" -Delimiter ";" -NoTypeInformation
            }
        }
        else
        {
            $objStatusBar.Text = "ERROR: Serveur " + $listBox.SelectedValue + " indisponible"
        }
    }

})

$button_users_cancel.add_Click({
    $FormUsers.Close()
})

#computers window
$button_computers_extract.add_Click({
    
    $tabcomputers=@()
    $CsvName = $listBoxComputers.SelectedValue
    
    if($checkbox_computers_enabled.Checked)
    {
        $ADComputersSource = Get-ADComputer -Server $listBox.SelectedValue -Filter {enabled -eq $true} -Properties *
        if($ADComputersSource)
        {
            $objStatusBarComputers.Text = "Checking Active Directory..."
            foreach($computers in $ADComputersSource)
            {
                $bitlocker=(Get-ADObject -SearchBase (Get-ADComputer -Filter {name -like $computers.Name}) -Filter {objectClass -eq "msFVE-RecoveryInformation"} -Properties msFVE-RecoveryPassword).'msFVE-RecoveryPassword'
                $ComputerObject = New-Object psobject
                $ComputerObject | Add-Member -Name "Name" -MemberType NoteProperty -Value $computers.name -Force
                $ComputerObject | Add-Member -Name "DNS Host Name" -MemberType NoteProperty -Value $computers.dNSHostName -Force
                $ComputerObject | Add-Member -Name "Enabled" -MemberType NoteProperty -Value $($computers.enabled) -Force
                
                if($checkbox_computers_OU.Checked)
                {
                    $ComputerObject | Add-Member -Name "Distinguishedname" -MemberType NoteProperty -Value $computers.distinguishedname -Force
                }
                if($checkbox_computers_lastlogondate.Checked)
                {
                    $ComputerObject | Add-Member -Name "Lastlogondate" -MemberType NoteProperty -Value $computers.lastlogondate -Force
                }
                if($checkbox_computers_bitlocker.Checked)
                {
                    $ComputerObject | Add-Member -Name "BitLocker Recovery Key" -MemberType NoteProperty -Value $Bitlocker -Force
                }
                if($checkbox_computers_localpassword.Checked)
                {
                    $ComputerObject | Add-Member -Name "Local Password" -MemberType NoteProperty -Value $computers.'ms-Mcs-AdmPwd' -Force
                }
                $tabcomputers += $computerobject
                $objStatusBarComputers.Text = "Done"
                #$tabcomputers | Export-Csv -Path "Export_AD_Computers_$CsvName.csv" -Delimiter ";" -NoTypeInformation
            }
            $tabcomputers | Export-Csv -Path "Export_AD_Computers_$CsvName.csv" -Delimiter ";" -NoTypeInformation

        }
        else
        {
            $objStatusBarComputers.Text = "ERROR: Serveur " + $listBoxComputers.SelectedValue + " indisponible"
        }
    }
    else
    {
        $ADComputersSource = Get-ADComputer -Server $listBox.SelectedValue -Filter * -Properties *
        if($ADComputersSource) #vérifie si l'accès au server $listbox.selectedvalue est OK
        {
            $objStatusBarComputers.Text = "Checking Active Directory..."
            foreach($computers in $ADComputersSource) #boucle sur chaque poste dans la source ADComputerSource
            {
                $bitlocker=(Get-ADObject -SearchBase (Get-ADComputer -Filter {name -like $computers.Name}) -Filter {objectClass -eq "msFVE-RecoveryInformation"} -Properties msFVE-RecoveryPassword).'msFVE-RecoveryPassword'
                $ComputerObject = New-Object psobject
                $ComputerObject | Add-Member -Name "Name" -MemberType NoteProperty -Value $computers.name -Force
                $ComputerObject | Add-Member -Name "DNS Host Name" -MemberType NoteProperty -Value $computers.dNSHostName -Force
                $ComputerObject | Add-Member -Name "Enabled" -MemberType NoteProperty -Value $($computers.enabled) -Force
                if($checkbox_computers_OU.Checked)
                {
                    $ComputerObject | Add-Member -Name "Distinguishedname" -MemberType NoteProperty -Value $computers.distinguishedname -Force
                }
                if($checkbox_computers_lastlogondate.Checked)
                {
                    $ComputerObject | Add-Member -Name "LastLogondate" -MemberType NoteProperty -Value $computers.lastlogondate -Force
                }
                if($checkbox_computers_bitlocker.Checked)
                {
                    Write-Host $bitlockerKey
                    $ComputerObject | Add-Member -Name "Bitlocker Recovery Key" -MemberType NoteProperty -Value $bitlocker -Force
                }
                if($checkbox_computers_localpassword.Checked)
                {
                    $ComputerObject | Add-Member -Name "Local Password" -MemberType NoteProperty -Value $computers.'ms-Mcs-AdmPwd' -Force
                }
                $tabcomputers += $ComputerObject
                $objStatusBarComputers.Text = "Done"
                #$tabcomputers | Export-Csv -Path "Export_AD_Computers_$CsvName.csv" -Delimiter ";" -NoTypeInformation
            }
            $tabcomputers | Export-Csv -Path "Export_AD_Computers_$CsvName.csv" -Delimiter ";" -NoTypeInformation
        }
        else
        {
            $objStatusBarComputers.Text = "ERROR: Serveur " + $listBoxComputers.SelectedValue + " indisponible"
        }
    }
})

 

Thanks. 

Hello @Mike9375,

Two comments for your function $button_computers_extract.add_Click when you are getting $bitlocker variable value:

1) you are using -like operator which potentially can return multiple instances

 

2) -SearchBase parameter of Get-ADObject accept DistinguishedName of the OU in String type and you are passing the whole ADComputer object

 

Please try this instead:

$bitlocker=(Get-ADObject -SearchBase (Get-ADComputer -Filter {name -eq $computers.Name} | Select-Object -ExpandProperty DistinguishedName ) -Filter {objectClass -eq "msFVE-RecoveryInformation"} -Properties msFVE-RecoveryPassword).'msFVE-RecoveryPassword'

Hope that helps.