?
Solved

Search AD with Powershell for Active computer accounts

Posted on 2014-02-28
21
Medium Priority
?
790 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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

Technology Partners: 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!

Question has a verified solution.

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

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses

777 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