June 26, 2015 at 12:56 am #26847
I need a PS script to retrieve a list of computers (from AD) and the user last logged on to every PC.
Is that possible with PS?
June 26, 2015 at 4:35 am #26852
In a roundabout way, yes it is possible. It's not as straightforward as pulling the information from SCCM so if you have that, that's the easier option, as the information is already in the SCCM database.
To achieve something similar in PowerShell, you have two tasks. First you'll need to generate the list of computers. You can do this with the Active Directory module and Get-ADComputer. The Active Directory module will be on your Domain Controller or, ideally, you'll be using it on a computer that has RSAT installed.
You should use Get-Help to figure out exactly what you need to do, but a basic list of computers could be produced with
Get-ADComputer -filter * | Select-Object -ExpandProperty Name | Out-File C:\computers.txt -append -noclobber
Now that you have a list of all the computers in your Active Directory you can use a WMI query to find out who is logged on. The current logged in user can be found in the username property of Win32_ComputerSystem.
You can use foreach to loop over all the computers in the computers.txt file and run
Get-WMIObject Win32_computersystem -computerName $computer | Select-Object UserName
I hope I've given you enough information to make a start on a script.
June 26, 2015 at 4:38 am #26853
June 27, 2015 at 7:07 am #26888
Agree with Matt., a task like that is really better suited to a product such as SCCM. It can retrieve the information for you immediately.
From an inventory point of view, Active Directory discovery is one of the main mechanisms SCCM can use for identifying computers, and getting the details of a WMI class is easily done via the hardware inventory.
If you just need it for finding out who is logging onto a computer, then a logon script that writes the information to a text file is maybe an idea.
June 27, 2015 at 7:35 am #26889
Hi Team-Hopes The bellow Code will meet the requirements.
Get-AdComputer -Filter * -Properties Description|Select -Expand Name,Description|Export-Csv
Get-AdComputer -Filter * -Properties Description|Select -Expand Name,Description|Convertto-Html|Out-File
Kindly Correct If anyMistakes...
July 6, 2015 at 7:47 am #27210
The IT guyParticipant
Hi Lars, Here is a snippet to get the last logged on user
You must be logged in to reply to this topic.