Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 103
  • Last Modified:

Powershell script to pull Info from all server within a domain

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
eperez0968
Asked:
eperez0968
  • 2
  • 2
1 Solution
 
CoralonCommented:
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
 
eperez0968Author Commented:
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
 
CoralonCommented:
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
 
eperez0968Author Commented:
Awesome!!!! it worked like a charm.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now