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
Solved

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

Posted on 2014-10-23
8
206 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

840 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