Solved

Powershell script to pull Info from all server within a domain

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

23 Experts available now in Live!

Get 1:1 Help Now