?
Solved

Powershell script to know which Server it logged on and applications installed

Posted on 2014-10-23
8
Medium Priority
?
213 Views
Last Modified: 2014-10-30
Hi, i need assistance to write a powershell script and output it in CSV file, the script will interrogate AD to get computers information from a specific OU, and find out from that OU the computers last login time stamp, location based on IP and applications installed on each computer.
0
Comment
Question by:Leo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40402338
Here is a first version, you didn't specify if you want a different file per computer or if you want it all together in one CSV.

You will need to modify the If and ElseIf Statements to add all of your IP ranges and Location Names

$results = @()
Get-ADComputer -filter * -SearchBase "CN=Computers,DC=domain,DC=com" -Properties Name,IPv4Address,lastlogonTimestamp | 
% {
if($_.IPv4Address -match "10.9.") { $location = "Site 1" }
ElseIf($_.IPv4Address -match "10.8."){ $location = "Site 2" }
ElseIf($_.IPv4Address -match "10.7."){ $location = "Site 3" }
Else { $location = "Unknown"}
$timestamp = [DateTime]::FromFileTime($_.lastlogonTimestamp)
$name = $_.Name
$results += Get-WmiObject -Class Win32_Product | Select @{Name="Computer Name";Expression={$name}},@{Name="Application Name";Expression={$_.Name}},@{Name="LastLogon";Expression={$timestamp}},@{Name="Location";Expression={$location}}
  }
$results | Export-Csv "C:\Applications Installed.csv" -nti

Open in new window

0
 
LVL 8

Author Comment

by:Leo
ID: 40404692
you didn't specify if you want a different file per computer or if you want it all together in one CSV.
A different  file for each computer, and the computer information  is retrieved from a specific OU in AD.
0
 
LVL 8

Author Comment

by:Leo
ID: 40404703
and can you please clarify me the expressions in parenthesis "".
if($_.IPv4Address -match "10.9.") { $location = "Site 1" }
ElseIf($_.IPv4Address -match "10.8."){ $location = "Site 2" }
ElseIf($_.IPv4Address -match "10.7."){ $location = "Site 3" }
Else { $location = "Unknown"}
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 16

Expert Comment

by:Joshua Grantom
ID: 40405223
In your post you said you wanted location based on IP so that is for you to put in your IP subnet for each site and give it a name.

For example, all IPs that start with "192.168.1" are in New York, all that start with "192.168.2" are in Sacramento, etc

if($_.IPv4Address -match "192.168.1") { $location = "New York" }
ElseIf($_.IPv4Address -match "192.168.2"){ $location = "Sacramento" }
ElseIf($_.IPv4Address -match "10.7."){ $location = "Site 3" }
Else { $location = "Unknown"}

If you need more/less just add/remove the ElseIf($_.IPv4Address -match "10.7."){ $location = "Site 3" } line for each site and fill in the details.
0
 
LVL 8

Author Comment

by:Leo
ID: 40405718
Thanks, i have run the script, it waited for too long, i stopped it, run it again, its still running, have seen any results in the output file.
0
 
LVL 8

Author Comment

by:Leo
ID: 40405767
Apologies i did produce output after a long wait :-)
just checking the output....
will keep you informed.
many thanks.
0
 
LVL 8

Author Comment

by:Leo
ID: 40405784
If i want to add a field for their IP address for each machine, what do i have to add in the script?
0
 
LVL 16

Accepted Solution

by:
Joshua Grantom earned 2000 total points
ID: 40406279
If you want to just have the IP Address instead of the "Site" use this.

$results = @()
Get-ADComputer -filter * -SearchBase "CN=Computers,DC=domain,DC=com" -Properties Name,IPv4Address,lastlogonTimestamp | 
% {
$timestamp = [DateTime]::FromFileTime($_.lastlogonTimestamp)
$name = $_.Name
$ipaddress = $_.IPv4Address
$results += Get-WmiObject -Class Win32_Product | Select @{Name="Computer Name";Expression={$name}},@{Name="Application Name";Expression={$_.Name}},@{Name="LastLogon";Expression={$timestamp}},@{Name="IP Address";Expression={$ipaddress}}
  }
$results | Export-Csv "C:\Applications Installed.csv" -nti

Open in new window


If you want to add the IP Address also, use this.

$results = @()
Get-ADComputer -filter * -SearchBase "CN=Computers,DC=domain,DC=com" -Properties Name,IPv4Address,lastlogonTimestamp | 
% {
if($_.IPv4Address -match "10.9.") { $location = "Site 1" }
ElseIf($_.IPv4Address -match "10.8."){ $location = "Site 2" }
ElseIf($_.IPv4Address -match "10.7."){ $location = "Site 3" }
Else { $location = "Unknown"}
$timestamp = [DateTime]::FromFileTime($_.lastlogonTimestamp)
$name = $_.Name
$ipaddress = $_.IPv4Address
$results += Get-WmiObject -Class Win32_Product | Select @{Name="Computer Name";Expression={$name}},@{Name="Application Name";Expression={$_.Name}},@{Name="LastLogon";Expression={$timestamp}},@{Name="Location";Expression={$location}},@{Name="IP Address";Expression={$ipaddress}}
  }
$results | Export-Csv "C:\Applications Installed.csv" -nti

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A hard and fast method for reducing Active Directory Administrators members.
Let's recap what we learned from yesterday's Skyport Systems webinar.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

765 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