Solved

Search AD with Powershell for Active computer accounts

Posted on 2014-02-28
21
768 Views
Last Modified: 2014-03-06
Hi,

Can Expert please amend the below poweshell to inculde ONLY the active computer accounts. As it stands the script exports all the servers Active/Disabled.

Get-ADComputer -Filter * -Properties * | ? {$_.OperatingSystem -like "*2000*"} | select Name, OperatingSystem, OperatingSystemServicePack | Export-CSV C:\servers.csv

Thanks
0
Comment
Question by:badabing1
[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
  • 10
  • 9
  • 2
21 Comments
 
LVL 13

Expert Comment

by:Santosh Gupta
ID: 39894871
Please find the below script.
-----------------------------------------------------------
$computers = "<TextFileListofComputerToRunViaPoSh.txt>"
 
$ADComp = "<OutputFileLocation.txt/.Log>"
 
Get-Content $computers | Foreach {If (Get-ADComputer -Filter {Name -eq $_}) {Add-Content $ADComp "$_ is Enabled $((Get-ADComputer $_).enabled) in Active Directory."} Else {Add-Content $ADComp "Warning!! $_ is not in Active Directory."}}
--------------------------------------------------------------------
http://gallery.technet.microsoft.com/scriptcenter/Powershell-to-determine-if-14d30f94
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39894874
? {$_.Enabled -eq $True -and $_.OperatingSystem -like "*2000*"}
0
 

Author Comment

by:badabing1
ID: 39895236
Thanks you both,

my bad, i should have said servers that have not been logged on. We have number of server accounts in AD that are still enabled but the physical servers no longer exist.

please leave the Enabled variable in but add something for the servers that have not been logged on.

apology.

Thanks
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 14

Expert Comment

by:Justin Yeung
ID: 39895309
you want to get all information and export to a file with all information or just filter the machines that meet your criteria?

if so what exact information you would like to see?

the machine that hasn't been logon since?

the machine is not pingable? (live)

the machine that is good?

OS, Name, and what else you would like? DSN?

do you want that to be moved to a OS after it considered that is dead?
0
 

Author Comment

by:badabing1
ID: 39895328
Hi

I want to be able to export machine  that hasnt been logged on since 60 days to CSV files.

also servers that is not pingable

evrything else same as my first post criteria.

did you mean move to OU? if so not yet but will do once im happy with the inofrmation i need.

Thank you.
0
 
LVL 13

Expert Comment

by:Santosh Gupta
ID: 39895372
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39896541
$servers = Get-ADComputer -Filter *  -Properties *  | ? {$_.OperatingSystem -like "*2000*"}

foreach ($server in $servers)

{

$Checktime = (get-date).adddays(-60)

if ((($server.lastlogondate).date - $checktime.date) -le 0)

{

$ServerLastLogon = "False"

}

Else

{

$ServerLastLogon = "True"

}

 

$ping = Test-Connection -ComputerName $Server.name -count 1  -Quiet -ErrorAction  SilentlyContinue

if ($ping -eq "True")

{

$pingable = "True"

}

Else

{

$pingable = "False"

}

 

$array = @()

$Properties = @{ServerName=$server.name;"Last logon More than 30 days"=$serverlastlogon;pingable=$pingable;Enabled=$server.enabled;OperatingSystem=$server.operatingsystem}

$Newobject = New-Object  PSObject -Property  $Properties

$Array +=$Newobject

 

$Array
}
0
 

Author Comment

by:badabing1
ID: 39900069
Justin - you are a genious!

slight tweak needed though, can you please amend the script so that only the computers that are active (pingable) are exported to a .csv file and anything thats not been logged on more than 30 days are excluded and anything less than that are included?  

(please ensure the list of computers is in a column so is easy to take out from csv)

i need the list of these computers from .csv to run registry settings against them!

This should do it, and i apologise for the changes. But hey you are the EXPERT.

Cheers
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39900633
$servers = Get-ADComputer -Filter *  -Properties *  | ? {$_.OperatingSystem -like "*2000*"}

foreach ($server in $servers)

{

$Checktime = (get-date).adddays(-30)

if ((($server.lastlogondate).date - $checktime.date) -le 0)

{
#$ServerLastLogon = "False"
}

Else
{
$ServerLastLogon = "True"
$ping = Test-Connection -ComputerName $Server.name -count 1  -Quiet -ErrorAction  SilentlyContinue

if ($ping -eq "True")

{
$pingable = "True"
$array = @()

$Properties = @{"Server Name"=$server.name;"Last logon within 30 days"=$serverlastlogon;Pingable=$pingable;Enabled=$server.enabled;"Operating System"=$server.operatingsystem}

$Newobject = New-Object  PSObject -Property  $Properties

$Array +=$Newobject

 


}
}

$path = "d:\test.csv"

$array | Select-Object "Server Name","Last Logon within 30 days",Pingable,"Operating System",Enabled | export-csv $path -Append


}
0
 

Author Comment

by:badabing1
ID: 39900794
Not sure why its complaining the 'Append' variable, but after removing that it seems to have worked. Has anything been excluded to csv file by removing the Append?


Export-Csv : A parameter cannot be found that matches parameter name 'Append'.
At C:\test2.ps1:40 char:128
+ $array | Select-Object "Server Name","Last Logon within 30 days",Pingable,"Operating System",Enabled | export-csv $pa
th -Append <<<<
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand

cheers
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39900986
it should work with -append........

what version of PS do you have...........
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39901191
it requires powershell 3.0 for csv to append......... just tried it on 2.0 no such option
0
 

Author Comment

by:badabing1
ID: 39903309
Thanks Justin,

im trying this again, will report back.

cheers
0
 

Author Comment

by:badabing1
ID: 39904358
Hi Justin,

Updating to powershell 3.0 has worked!

I feel bad asking but would it e bpossoble for you to add the IP address variable as well please?

everything else is fine just the IP address column is missing.

I promise i will not ask for anything more for this script, unless i have logged another ticket.

much appreciated!

Thanks
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39904424
here you go lol

$servers = Get-ADComputer -Filter *  -Properties *  | ? {$_.OperatingSystem -like "*2000*"}

foreach ($server in $servers)

{

$Checktime = (get-date).adddays(-30)

if ((($server.lastlogondate).date - $checktime.date) -le 0)

{
#$ServerLastLogon = "False"
}

Else
{
$ServerLastLogon = "True"
$ping = Test-Connection -ComputerName $Server.name -count 1  -Quiet -ErrorAction  SilentlyContinue
if ($ping -eq "True")

{
$pingable = "True"

$NIC = Get-WmiObject -Class win32_networkadapterconfiguration -ComputerName $Server.Name | ? {$_.IPaddress -ne $null -and $_.IPEnabled -eq $true}



$array = @()

$Properties = @{"Server Name"=$server.name;"Last logon within 30 days"=$serverlastlogon;Pingable=$pingable;Enabled=$server.enabled;"Operating System"=$server.operatingsystem;"IP Address"=$Nic.ipaddress}

$Newobject = New-Object  PSObject -Property  $Properties

$Array +=$Newobject

 


}
}

$path = "d:\test.csv"

$array | Select-Object "Server Name","Last Logon within 30 days",Pingable,"Operating System",Enabled,"IP Address" | export-csv $path -Append


}
0
 

Author Comment

by:badabing1
ID: 39904709
Hi Justin,

im getting system.string[] in IP column and seem to be getting a duplicate entries for one particular server?


Server1      TRUE      TRUE      Windows Server 2003      TRUE      System.String[]

Thanks
0
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 250 total points
ID: 39905181
$servers = Get-ADComputer -Filter *  -Properties *  | ? {$_.OperatingSystem -like "*2000*"}

foreach ($server in $servers)

{

$Checktime = (get-date).adddays(-30)

if ((($server.lastlogondate).date - $checktime.date) -le 0)

{
#$ServerLastLogon = "False"
}

Else
{
$ServerLastLogon = "True"
$ping = Test-Connection -ComputerName $Server.name -count 1  -Quiet -ErrorAction  SilentlyContinue
if ($ping -eq "True")

{
$pingable = "True"

$NIC = Get-WmiObject -Class win32_networkadapterconfiguration -ComputerName $Server.Name | ? {$_.IPaddress -ne $null -and $_.IPEnabled -eq $true}



$array = @()

$Properties = @{"Server Name"=$server.name;"Last logon within 30 days"=$serverlastlogon;Pingable=$pingable;Enabled=$server.enabled;"Operating System"=$server.operatingsystem;"IP Address"=$Nic.ipaddress -join ', '}

$Newobject = New-Object  PSObject -Property  $Properties

$Array +=$Newobject

 


}
}

$path = "d:\test.csv"

$array | Select-Object "Server Name","Last Logon within 30 days",Pingable,"Operating System",Enabled,"IP Address" | export-csv $path -Append


}
0
 

Author Closing Comment

by:badabing1
ID: 39907494
Justin you deserve a gold medal, A true genius!! keep up the good work you are an asset to the Expert community. (I will be logging more script related tickets in future so please look out for them!)

Thank you very much!
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39907664
lol no problem
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39907709
This will work also, since you might have error obtaining ipaddress from a machine that can't resolve WMI object (RPC error etc).
$ping.IPV4address.IPaddresstostring will get the ip from DNS that you ping from instead. (only IP v4 address)

$servers = Get-ADComputer -Filter *  -Properties *  | ? {$_.OperatingSystem -like "*2000*"}

foreach ($server in $servers)

{

$Checktime = (get-date).adddays(-30)

if ((($server.lastlogondate).date - $checktime.date) -le 0)

{
#$ServerLastLogon = "False"
}

Else
{
$ServerLastLogon = "True"
$ping = Test-Connection -ComputerName $Server.name -count 1  -Quiet -ErrorAction  SilentlyContinue
if ($ping -eq "True")

{
$pingable = "True"

#$NIC = Get-WmiObject -Class win32_networkadapterconfiguration -ComputerName $Server.Name | ? {$_.IPaddress -ne $null -and $_.IPEnabled -eq $true}



$array = @()

$Properties = @{"Server Name"=$server.name;"Last logon within 30 days"=$serverlastlogon;Pingable=$pingable;Enabled=$server.enabled;"Operating System"=$server.operatingsystem;"IP Address"=$ping.IPV4address.IPAddressToString}

$Newobject = New-Object  PSObject -Property  $Properties

$Array +=$Newobject

 


}
}

$path = "d:\test.csv"

$array | Select-Object "Server Name","Last Logon within 30 days",Pingable,"Operating System",Enabled,"IP Address" | export-csv $path -Append


}
0
 

Author Comment

by:badabing1
ID: 39908761
cool i will try this as well.

Thanks
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

This article outlines the process to identify and resolve account lockout in an Active Directory environment.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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 …
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…

730 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