Link to home
Start Free TrialLog in
Avatar of chgl
chglFlag for United Kingdom of Great Britain and Northern Ireland

asked on

DStools and powershell

Hi Guys,

We have server 2003 r2 and therefore do no have the powershell module for AD.

On powershell i typed in

$comp=dsquery computer -name *

it give me the full OU path e.g "CN=PC249083166313,OU=WA Computers,OU=Wales (WA),OU=Professional Services (PS),OU=Departmental Accounts,OU=CI,DC=ci,
DC=org"

however I only need the computer name in the variable $comp,

I then need to run an query and find out who is logged onto these machines and put it in an excel spreadsheet. How can I do this?

Thank in advance for the help.

Regards,
k
Avatar of becraig
becraig
Flag of United States of America image

So I recently wrote and tested something that does this:

Give below a test, if you need tweaking there are some other really good guys here who can collaborate to get you where you need to be.

I am exporting to csv you should have no problem simply opening the csv file in excel.

$output = ""
$output +=
@"
Server, UserName, LogonTime
"@
Import-Module activedirectory
$servers = (Get-ADComputer  -filter {name -like "*"} | select -expand name )
            ForEach($server in $servers) {
                $LogonSessions = Get-WmiObject Win32_LogonSession -ComputerName $server     
                ForEach($LogonSession in $LogonSessions) {
                    $LoggedOnUser = Get-WmiObject -Query "Associators of {Win32_LogonSession.LogonId=$($LogonSession.LogonId)} Where AssocClass=Win32_LoggedOnUser Role=Dependent" -ComputerName $server | select-Object Domain,Name,SID,StartTime,LogonID,LogonType,LogonTypeName,ComputerName
foreach ($user in $LoggedOnUser)
{
if ($LoggedOnUser.Name -ne "" -and $LoggedOnUser.Name -ne $null)
{
                    $LoggedOnUser.StartTime = [Management.ManagementDateTimeConverter]::ToDateTime($LogonSession.starttime)
                    $LoggedOnUser.LogonID = $LogonSession.LogonID
                    $LoggedOnUser.LogonType = $LogonSession.logontype
                    $LoggedOnUser.ComputerName = $server
                    $since = $LoggedOnUser.StartTime
					$user = $LoggedOnUser.Name
$output +=
@"
$server, $user, $since
"@
                 }
				 } #End foreach user
                } # end  ForEach $LogonSession               
            }  # end ForEach $Computer
             
  $output | Export-Csv -NoType file.csv

Open in new window

Avatar of chgl

ASKER

^ Hi we don't have powershell for AD, and I don't want it right now.

I need to know how to get the computer name from "dsquery computer -name *" into a variable.
SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chgl

ASKER

Thank you Subsun and Qlemo - very useful.

However I am trying to learn how to split the data in an array, this is also my intent for asking.
However I am trying to learn how to split the data in an array
Sorry, I am not clear about the question. Which data you want to split?  Can you please explain?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of chgl

ASKER

Hi Qlemo,

Thank you very much, very useful and it works!

However please can you send me a link that explains what youve written because I dont understand the [^ .... [2]

Many thanks again.
Avatar of chgl

ASKER

what does the '$1' and the [2] mean?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial