Solved

Powershell script to pull Info from all server within a domain

Posted on 2016-11-21
4
63 Views
Last Modified: 2016-11-29
Hi,

I need help in getting a Powershell script in retrieving info from servers with in a domain OR OU and getting an output to an xlsx file.

Search through domain servers

1.  look for this in the host name "FS",  "FP", “DFS”, “RP”  in name
2. Host-name and OS of the Server.
3. if the File services is installed
4. if File service is installed show the path to shares per its server

thanks,
Eric
0
Comment
Question by:eperez0968
  • 2
  • 2
4 Comments
 
LVL 24

Expert Comment

by:Coralon
ID: 41898446
This was interesting, and I had to leverage all kinds of things to pull what you are looking for.. :-)

$SearchRoot = 'DC=domain,DC=tld'
$filter = "(&(objectCategory=computer)(|(name=*fp*)(name=*fp*)(name=*dfs*)(name=*rp*)))"

$ADSI = [adsisearcher]$filter
$ADSI.SearchRoot = $SearchRoot
$ADSI.PageSize = 1000
$ADSI.SizeLimit = 1000

$Computers = $ADSI.FindAll().Properties.Name  

$Output = @()

foreach ($Computer in $Computers)
{
    $OS = invoke-command -ComputerName $Computer -scriptblock { ((Get-WmiObject -ComputerName $Computer -Class win32_operatingsystem -Property Name) -split '|')[0] }
    $FSInstalled = invoke-command -ComputerName $Computer -ScriptBlock { get-windowsfeature -Name fs* | Select-Object -property Name,Installed }
    $Shares = invoke-command -ComputerName $Computer -ScriptBlock { (Get-WmiObject -ComputerName $Computer -Class win32_share -Property path).path -join ';' }

       $Properties = @{ }
       $Properties.Add('Name',$Computer)
       $Properties.Add('OS',$OS)

    $FSInstalled | ForEach-Object {
        $Properties.Add($_.Name,$_.Installed)
    }
    
    $Properties.Add('Shares', $Shares)

    $Output += (New-Object -TypeName PSCustomObject -Property $Properties)
} 

$output | export-csv -Path "$env:temp\computerprops.txt" -NoTypeInformation 

Open in new window


It is limited to 1000 servers.. but if you have more, then you can increase the limit numbers in the script..

It's not completely tested, but it should work.  You will need to make sure you have WMI access and Powershell remoting access..

Coralon
0
 

Author Comment

by:eperez0968
ID: 41899157
I am getting this error....


Exception calling "FindAll" with "0" argument(s): "Unspecified error
"
At C:\Users\adm.ep546\Desktop\ServerLook.ps1:9 char:1
+ $Computers = $ADSI.FindAll().Properties.Name
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : COMException
0
 
LVL 24

Accepted Solution

by:
Coralon earned 500 total points
ID: 41900061
Couple of questions.. are you running it with an account that has admin access to AD?  It shouldn't be necessary, but its worth knowing..
And are you running it in an elevated PS session?  

Obviously, I'd suggest trying  both..

I had tried this piece on my own AD, and it worked exactly as expected..

But.. another thought.. instead of using the (name= filter.. try using sAMAccountName
and change the properties line to use sAMAccountName also..
$filter = "(&(objectCategory=computer)(|(samaccountname=*fp*)(samaccountname=*fp*)(samaccountname=*dfs*)(samaccountname=*rp*)))"

$Computers = $ADSI.FindAll().Properties.samaccountname 

Open in new window


And try it directly in the powershell prompt.. just to make sure what you can get to work..
And you can verify that the $filter is working properly by just putting in $ADSI.filter after you run the section, so that it should show you the filter.  If that is an issue, then put the filter in the ADSI creation line..
$ADSI = [adsisearcher]"(&(objectCategory=computer)(|(samaccountname=*fp*)(samaccountname=*fp*)(samaccountname=*dfs*)"

Open in new window


If that does work, then the you'll need to modify the computer name part of the script.. by adding 1 more line..
foreach ($Computer in $Computers)
{
# this is the new line below here..
$Computer = $Computer.substring(0, $Computer.Length - 1)

Open in new window

This line will strip off the $ sign from the samaccountname..

Now.. all of this is assuming you have a straight AD that allows direct access (don't need SSL etc..)..

Coralon
0
 

Author Closing Comment

by:eperez0968
ID: 41905784
Awesome!!!! it worked like a charm.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
A brief introduction to what I consider to be the best editor for PowerShell.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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