Search AD with Powershell for Active computer accounts

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
badabing1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Santosh GuptaCommented:
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
Justin YeungSenior Systems EngineerCommented:
? {$_.Enabled -eq $True -and $_.OperatingSystem -like "*2000*"}
0
badabing1Author Commented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Justin YeungSenior Systems EngineerCommented:
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
badabing1Author Commented:
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
Santosh GuptaCommented:
0
Justin YeungSenior Systems EngineerCommented:
$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
badabing1Author Commented:
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
Justin YeungSenior Systems EngineerCommented:
$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
badabing1Author Commented:
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
Justin YeungSenior Systems EngineerCommented:
it should work with -append........

what version of PS do you have...........
0
Justin YeungSenior Systems EngineerCommented:
it requires powershell 3.0 for csv to append......... just tried it on 2.0 no such option
0
badabing1Author Commented:
Thanks Justin,

im trying this again, will report back.

cheers
0
badabing1Author Commented:
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
Justin YeungSenior Systems EngineerCommented:
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
badabing1Author Commented:
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
Justin YeungSenior Systems EngineerCommented:
$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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
badabing1Author Commented:
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
Justin YeungSenior Systems EngineerCommented:
lol no problem
0
Justin YeungSenior Systems EngineerCommented:
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
badabing1Author Commented:
cool i will try this as well.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Server OS

From novice to tech pro — start learning today.