How I can consult this script for a CSV file?

Posted on 2017-12-07
Low Priority
Last Modified: 2017-12-07
I need to consult the following command to know the user logged for a group of 5000 computers in a csv file, I do not know how to join the import and the Foreach-Object. Can you help me?

The command work for 1 computer, I need consult for 5000 in a csv file.

$userlogged = Get-WmiObject -Class win32_computersystem -ComputerName "nameofcomputer" | select username

 $arr = $userlogged -split "\\"

 $user= $arr[1] -replace "}",""

 Get-ADUser $user -Properties * | select samaccountname, Name, description, office 

Open in new window

Question by:Jose Colmenares
LVL 86

Accepted Solution

oBdA earned 1000 total points
ID: 42395674
This assumes that your csv contains a column "ComputerName" in which the names are listed.
Import-Csv -Path C:\Temp\computers.csv |
	ForEach-Object {
		$ComputerName = $_.ComputerName
		Write-Host "Processing $($ComputerName)"
		$(Try {
			$ConsoleUser = (Get-WmiObject -Query 'Select UserName From Win32_ComputerSystem' -ComputerName $ComputerName -ErrorAction Stop).Username
			If (-not $ConsoleUser) {Throw "No console user logged on!"}
			Get-ADUser -Identity $ConsoleUser.Split('\')[1] -Properties Description, Office -ErrorAction Stop | Select-Object -Property *
		} Catch {
			$_ | Select-Object -Property @{n='Error'; e={$_.Exception.Message}}
		}) | Select-Object @{n='ComputerName'; e={$ComputerName}}, SamAccountName, Name, Description, Office, Error
	} | Export-Csv -NoTypeInformation -Path 'C:\Temp\Results.csv'

Open in new window

And do AD and your network a favor and don't query AD for all user properties, when all you really need (besides the default set returned by the cmdlet) is 'Description' and 'Office'. It may be a bit more typing for you, but it is a lot less stress on AD.

Author Closing Comment

by:Jose Colmenares
ID: 42396031
You're the best. You solved and contributions more than my question and understanding. Thank you.

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Loops Section Overview

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question