Solved

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

Posted on 2014-10-23
8
203 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
 
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
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 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…

911 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

16 Experts available now in Live!

Get 1:1 Help Now