Solved

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

Posted on 2014-10-23
8
212 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 500 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

688 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