Solved

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

Posted on 2014-10-23
8
201 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:Joshua Grantom
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 8

Author Comment

by:Leo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now