Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $CsvFile = "path\GetLoggedOnUsersv2.csv"
- Get-ADComputer -Filter * -SearchBase "DC=lab,DC=corp" | ForEach-Object {
- $ADComputer = $_
- Write-Host "Processing $($ADComputer.Name) ..." -ForegroundColor White -NoNewline
- Try {
- $CS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $ADComputer.Name -ErrorAction Stop | Select-Object -Property `
- @{n='Computername'; e={$ADComputer.Name}},
- @{n='DNSHostName'; e={$ADComputer.DNSHostName}},
- @{n='Department'; e={
- If ($_.Username) {
- Try {(Get-ADUser -Identity $_.Username.Split('\')[1] -Property Department -ErrorAction Stop).Department} Catch {"ERROR: $($_.Exception.Message)"}
- }
- }},
- Username,
- Manufacturer,
- Model
- Write-Host " OK ($($CS.Manufacturer) $($CS.Model))." -ForegroundColor Green
- $CS
- } Catch {
- Write-Host " ERROR: $($_.Exception.Message)" -ForegroundColor Red
- $_.Exception.Message | Select-Object -Property @{n='Computername'; e={$ADComputer.Name}}, @{n='Error'; e={$_}}
- }
- } | Select-Object Computername, DNSHostName, Department, Username, Manufacturer, Model, Error |
- Export-Csv -Path $CsvFile -NoTypeInformation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement