Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell script to pull Info from all server within a domain

Posted on 2016-11-21
4
Medium Priority
?
96 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
[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
  • 2
  • 2
4 Comments
 
LVL 25

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 25

Accepted Solution

by:
Coralon earned 2000 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

Protect Your Retail Business and Reputation

Wi-Fi access doesn't just impact your business & customer experience, it can also affect your security.  Join us for an informative webinar to learn more about the top threats and trends impacting retail today, and the key solutions to protecting retail networks and reputations.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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