Task to test functionality of this powershell script

  Kiến thức lập trình

This is a test of a script used for gathering data. Sharing in case needed. Question is thoughts if used?

# Import the Active Directory module
Import-Module ActiveDirectory

# Get the date 90 days ago
$90DaysAgo = (Get-Date).AddDays(-90)

# Define the parent OUs
$ParentOUs = "OU=Clients,DC=yourdomain,DC=com", "OU=Servers,DC=yourdomain,DC=com", "OU=ServersDev,DC=yourdomain,DC=com"  # Replace with your actual Distinguished Names

# Create an array to hold the output
$Output = @()

# Loop through each parent OU
foreach ($ParentOU in $ParentOUs) {
    # Get all computer objects in the parent OU that haven't logged on for 90 days or more
    $StaleComputers = Get-ADComputer -Filter {LastLogonTimeStamp -lt $90DaysAgo} -SearchBase $ParentOU -Properties Name, LastLogonTimeStamp, OperatingSystem, Enabled, IPv4Address, PasswordLastSet

    # Loop through each stale computer
    foreach ($Computer in $StaleComputers) {
        # Get the computer's last logon date
        $LastLogonDate = [DateTime]::FromFileTime($Computer.LastLogonTimeStamp)

        # Add the computer's information to the output
        $Output += [PSCustomObject]@{
            'Computer Name' = $Computer.Name
            'Last Logon Date' = $LastLogonDate
            'Operating System' = $Computer.OperatingSystem
            'Enabled' = $Computer.Enabled
            'IP Address' = $Computer.IPv4Address
            'Parent OU' = $ParentOU
        }
    }
}

# Export the output to a CSV file
$Output | Export-Csv -Path "C:tempMetrics - Stale Computers - April 24.csv" -NoTypeInformation

This is a test of a script used for gathering data.

New contributor

AlaskanBullWorm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT