??Need to locate computers on your domain? Use PowerShell.??
David Schunk
IT Professional with 10+ Years Experience | CompTIA Sec+ CE | Active Secret Clearance | New Hampshire VMUG Leader | Podcast Host | Adoptee
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:
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