??Need to locate computers on your domain?  Use PowerShell.??
Image by Microsoft Learn

??Need to locate computers on your domain? Use PowerShell.??

Below is a PowerShell script that you can use as a starting point to retrieve information from computers in a domain. This script assumes you have the necessary permissions to query Active Directory and access remote computers, and that the machines are configured to allow PowerShell Remoting.

Please ensure to run PowerShell with appropriate permissions and ensure that execution policy (Set-ExecutionPolicy) is configured correctly to run the scripts.

# Import Active Directory Module
Import-Module ActiveDirectory

# Initialize an array to store results
$results = @()

# Get a list of all computers from Active Directory along with their last logon timestamp
$computers = Get-ADComputer -Filter * -Property * | Select-Object DNSHostName,LastLogonDate

# Loop through each computer in the list
foreach ($computer in $computers) {
    $compName = $computer.DNSHostName
    
    try {
        # Get computer and BIOS info using WMI
        $compInfo = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $compName -ErrorAction Stop
        $biosInfo = Get-WmiObject -Class Win32_BIOS -ComputerName $compName -ErrorAction Stop
        $osInfo = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $compName -ErrorAction Stop
        
        # Create a custom object to hold our data
        $info = New-Object PSObject -Property @{
            ComputerName = $compName
            OS = $osInfo.Caption
            LastOnline = $computer.LastLogonDate
            SerialNumber = $biosInfo.SerialNumber
            Manufacturer = $compInfo.Manufacturer
            Model = $compInfo.Model
        }
        
        # Add our data to the results array
        $results += $info
    }
    catch {
        Write-Warning "Failed to retrieve information for $compName. Error: $_"
    }
}

# Display our results in a table
$results | Format-Table -AutoSize

# Optionally, save results to a CSV file
#$results | Export-Csv -Path "computer_info.csv" -NoTypeInformation        
Ensure compliance with IT and security policies in your organization when running scripts in a production environment. Always review, understand, and test scripts thoroughly before deployment.

Notes:

  • Permissions: Ensure your user has permissions to query Active Directory and use WMI on remote computers
  • PowerShell Remoting: Ensure remoting is enabled and configured on target computers.
  • PowerShell Version: Check PowerShell version for compatibility.
  • Execution Policy: Set an appropriate PowerShell execution policy, e.g., Set-ExecutionPolicy RemoteSigned.
  • Firewall: Ensure firewall settings allow for remote WMI queries.
  • Error Handling: The script uses basic error handling to continue if an error occurs - adapt as needed.

Testing: Test the script in a safe environment before production use.

#PowerShell #Scripting #Automation #ActiveDirectory #ITAutomation #ITManagement #SysAdmin #NetworkAdmin #WMI #ComputerManagement #TechTips #WindowsAdmin #ServerManagement #InfoSec #CyberSecurity #WindowsServer #Microsoft365 #EndpointManagement #CloudManagement #DigitalTransformation

要查看或添加评论,请登录

David Schunk的更多文章

社区洞察

其他会员也浏览了