Outil de recueil d'info Utililateur / Ordinateur dans un Active Directory

Interface Graphique

main.ps1

Import-module -Name .\ADToolsUser_Module -Verbose -Force
Import-Module -Name .\ADToolsComputer_Module -Verbose -Force

<#

 ___________________________________________________
|   _______ ___________                             |
|  | Users | Computers | Servers                    |
|  |_______|___________|                            |
|                                                   |
|   ______________                                  |
|  | Search Field |                                 |
|  | Textbox      |                                 |
|  |______________|                                 |
|  ________________________________________________ |
| | Res Field                                      ||
| |                                                ||
| |                                                ||
| |                                                ||
| |                                                ||
| |                                                ||
| |                                                ||
| |                                                ||
| |                                                ||

enable / disable accounts
rename computers
common infos : Locked account / password last set / last logon date / member of


#>

.\form_window.ps1

form_window.ps1

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

[System.Windows.Forms.Application]::EnableVisualStyles()

$formWindow = New-Object System.Windows.Forms.Form
$formWindow.ClientSize = '1280,720'
$formWindow.BackColor = "#ffffff"
$formWindow.AutoSize = $true

$formWindowTitle = New-Object System.Windows.Forms.Label
$formWindowTitle.Text = "Utilitaire AD"
$formWindowTitle.AutoSize = $true
$formWindowTitle.Location = New-Object System.Drawing.Point(20,20)
$formWindowTitle.Font = "Corbel,11"

$cancelBtn = New-Object System.Windows.Forms.Button
$cancelBtn.BackColor = "#ffffff"
$cancelBtn.Text = "Cancel"
$cancelBtn.Width = 90
$cancelBtn.Height = 30
$cancelBtn.Location = New-Object System.Drawing.Point(1150,690)
$cancelBtn.Font = "Corbel,11"
$cancelBtn.ForeColor = "#000"
$cancelBtn.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

$formWindow.CancelButton = $cancelBtn
#$formWindow.Controls.Add($cancelBtn)

$navBar = New-Object System.Windows.Forms.TabControl
$navBar.AutoSize = $true

$userTab = New-Object System.Windows.Forms.TabPage
$userTab.AutoSize = $true
# $vars can only be modified from included scripts if they have 
# been initialised before the inclusion
# to do : migrate to module
.\user_tab.ps1

$computerTab = New-Object System.Windows.Forms.TabPage
.\computer_tab.ps1

$navBar.Controls.AddRange(@($userTab,$computerTab))
$navbar.Alignment = 'Top'
$navBar.Size = '1250,650'
$navBar.Location = New-Object System.Drawing.Point(10,10)



$formWindow.Controls.AddRange(@($cancelBtn,$navBar))

[void]$formWindow.ShowDialog()

user_tab.ps1

user_tab.ps1


$userTab.Text = "Utilisateurs"

$userResField.Width =   1200
$userResField.Height = 650
$userResField.BackColor = "#424242"
$userResField.ForeColor = "#ffffff"
$userResField.Location = New-Object System.Drawing.Point(5,25)
$userResField.Font = "Corbell, 9"
$userResField.Multiline = $true
$userResField.ScrollBars = "Vertical"

$userSearchField.Multiline = $false
$userSearchField.Width = '400'
$userSearchField.Height = '30'

$searchUserBtn.BackColor = "#ffffff"
$searchUserBtn.Text = "Chercher"
$searchUserBtn.Location = New-Object System.Drawing.Point(450,0)

$searchUserBtn.Add_Click( {
    $userResField.Text = 
        get_aduser_info -searchString $userSearchField.Text
    }
)

$searchLockedUsers.BackColor = "#ffffff"
$searchLockedUsers.Text = "Utilisateurs Verrouillés"
$searchLockedUsers.Autosize = $true
$searchLockedUsers.Location = New-Object System.Drawing.Point(650,0)

$searchLockedUsers.Add_Click( {
    $userResField.Text = get_locked_accounts
} )

$searchDisabledUsers.BackColor = "#ffffff"
$searchDisabledUsers.Text = "Utilisateurs Désactivés"
$searchDisabledUsers.Autosize = $true
$searchDisabledUsers.Location = New-Object System.Drawing.Point(800,0)

$searchDisabledUsers.Add_Click( {
    $userResField.Text = get_disabled_accounts
} )

$userTab.Padding = "3,3,3,3"
$userTab.Controls.AddRange(@($userSearchField,
                                $searchUserBtn,
                                $userResField,
                                $searchLockedUsers,
                                $searchDisabledUsers))


computer_tab.ps1


$computerTab.Text = "Ordinateurs"

$computerResField.Width = 1200
$computerResField.Height = 650
$computerResField.BackColor = "#424242"
$computerResField.ForeColor = "#ffffff"
$computerResField.Location = New-Object System.Drawing.Point(5,25)
$computerResField.Font = "Corbell, 9"
$computerResField.Multiline = $true
$computerResField.ScrollBars = "Vertical"

$computerSearchField.Multiline = $false
$computerSearchField.Width = 400
$computerSearchField.Height = 30

$searchComputerBtn.BackColor = "#ffffff"
$searchComputerBtn.Text = "Chercher"
$searchComputerBtn.Location = New-Object System.Drawing.Point(450,0)
$searchComputerBtn.Add_Click( {
    $computerResField.Text = get_adcomputer_info
})

$computerTab.Controls.AddRange(@($computerResField, $computerSearchField, $searchComputerBtn))

Logique

.\ADToolsUser_Module\ADToolsUser_Module.psm1

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing


function get_aduser_info {
    param (
        [Parameter(Mandatory=$true)]    $searchString
    )

    $userSelectionList = New-Object System.Windows.Forms.CheckedListBox

    $acctResults = Get-ADUser -properties samaccountname -Filter "Name -like '*$searchString*' -or samaccountname -like '*$searchString*'"
    foreach ($ADAccount in $acctResults) {
        $userProp = Get-ADUser -Properties * -Identity $ADAccount
        "Nom : "
        $userProp.GivenName
        $userProp.Surname
        "`r`n"
        if ($userProp.PasswordLastSet){
            "`r`nDernier changement de mot de passe :"
            $userProp.PasswordLastSet.tostring()    
        }
        if ($userProp.Lockedout){
            "LOCKED"
        }
        "`r`n`r`n -- `r`n`r`n"        
    }
}

function get_locked_accounts {
    "Comptes verrouillés : `r`n`r`n"
    $ADAccts = Get-ADUser -Properties samaccountname -Filter *
    $locked = 0
    foreach ($ADAcct in $ADAccts){
        $AcctInfo = Get-ADUser -Properties * -Identity $ADAcct
        if ($AcctInfo.Lockedout){
            $AcctInfo.GivenName
            $AcctInfo.Surname
            "`r`n LOCKED `r`n"
            $locked++
        }
    }
    if ($locked -eq 0){
        "Pas de compte verrouillé"
    }
"`r`n`r`n -- `r`n`r`n"        
}

function get_adgroup_info {
    param (
        [Parameters(Mandatory=$true)] $username
    )

    $gpMembership = Get-ADUser -Identity $ADaccount

}

function get_disabled_accounts {
    $disabledAccts = Search-ADAccount -AccountDisabled -UsersOnly | Select-Object -Property samaccountname 

    if ($disabledAccts){
        foreach ($disabledAcct in $disabledAccts.samaccountname){
            $disabledAcct
            "`r`n"
        }    
    }
    else {
        "Pas d'utilisateur désactivé"
    }
}


$userResField           = New-Object System.Windows.Forms.TextBox
$userSearchField        = New-Object System.Windows.Forms.TextBox
$searchUserBtn          = New-Object System.Windows.Forms.Button
$searchLockedUsers      = New-Object System.Windows.Forms.Button
$searchDisabledUsers    = New-Object System.Windows.Forms.Button

Export-ModuleMember -Variable userResField
Export-ModuleMember -Variable userSearchField
Export-ModuleMember -Variable searchUserBtn
Export-ModuleMember -Variable searchLockedUsers
Export-ModuleMember -Variable searchDisabledUsers


Export-ModuleMember -Function get_aduser_info
Export-ModuleMember -Function get_locked_accounts
Export-ModuleMember -Function get_disabled_accounts

.\ADToolsComputer_Module\ADToolsComputer_Module

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

function get_adcomputer_info {

    $searchString = $computerSearchField.Text
    $searchString

    $adComputers = get-adcomputer  -Properties * -filter *
    foreach ($adComputer in $adComputers) {
        if ($adComputer.CN -like "*$searchString*") {
            $details = Get-ADComputer -Properties * -Identity $adComputer.SamAccountName
            $details.DistinguishedName
            "`r`n"
            $details.SamAccountName
            "`r`n"
            $details.DNSHostName
            "`r`n"
            $details.IPv4Address
            "`r`n"
            $details.OperatingSystem
#            if (Test-Connection -ComputerName $adComputer.CN -Quiet){}
            if ($details.OperatingSystem -like "*Windows 10*"){
                if (Test-Connection -ComputerName $adComputer.CN -Count 1 -Quiet) {
                    "`r`n"
                    $adComputer.CN
                    "`r`n"
                    $result = get_last_logon_user -targetComputer $adComputer.CN
                    $result
                }    
            } 
            #-and (
            "`r`n`r`n -- `r`n`r`n"            
        }

    }
}

function get_last_logon_user {
    param (
        [Parameter(Mandatory=$true)] $targetComputer
    )

    $result = get-winevent -computer $targetComputer -filterhashtable @{Logname='security';ID=4648} -MaxEvents 1 | Select-Object @{N='User';E={$_.Properties[1].Value}},TimeCreated
    "`r`n"
    $result.User
    "`r`n"
    $result.TimeCreated
    "`r`n"
}


$computerResField       = New-Object System.Windows.Forms.TextBox
$computerSearchField    = New-Object System.Windows.Forms.TextBox
$searchComputerBtn      = New-Object System.Windows.Forms.Button

Export-ModuleMember -Variable computerResField
Export-ModuleMember -Variable computerSearchField
Export-ModuleMember -Variable searchComputerBtn

Export-ModuleMember -Function get_adcomputer_info