Solved

Powershell script to pull Info from all server within a domain

Posted on 2016-11-21
4
35 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 23

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 23

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

13 Experts available now in Live!

Get 1:1 Help Now