Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Inventory Servers in Domain with Powershell

Hi experts,

I need some help from the PowerShell gurus out there as I am still a novice and learning powershell.

I would like to get an inventory of all the servers in a domain whether the servers are physical or Virtual. Find info such as OS, RAM, CPU, IP Address, make and model, group them by site and then export the output to .csv

If anyone can help this would save me a lot of time.

Many thanks.
0
damejen
Asked:
damejen
  • 9
  • 8
4 Solutions
 
Prashant GirennavarCommented:
Sure,

Below is the script which you can use it to query all the servers from AD and then do a WMI query to get the information abut RAM , CPU etc.

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

# Insert text in to first row.
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server"
$Sheet.Cells.Item(1,2) = "FQDNName"
$Sheet.Cells.Item(1,3) = "Manufacturer"
$Sheet.Cells.Item(1,4) = "Model"
$Sheet.Cells.Item(1,5) = "TotalPhysicalMemory"
$Sheet.Cells.Item(1,6) = "isVirtual" 
$Sheet.Cells.Item(1,7) = "SerialNumber" 
$Sheet.Cells.Item(1,8) = "BiosVersion"
$Sheet.Cells.Item(1,9) = "IPAddress"
$Sheet.Cells.Item(1,10) = "MACAddress"
$Sheet.Cells.Item(1,11) = "IPSubnet"
$Sheet.Cells.Item(1,12) = "DefaultGateway"
$Sheet.Cells.Item(1,13) = "Type"
$Sheet.Cells.Item(1,14) = "DHCPUse"
$Sheet.Cells.item(1,15) = "PasswordLastSet"
$Sheet.Cells.Item(1,16) = "DistinguisedName"
$Sheet.Cells.item(1,17) = "OperatingSystem"
$Sheet.Cells.Item(1,18) = "ServicePack"
$Sheet.Cells.item(1,19) = "BuildNumber"
$Sheet.Cells.Item(1,20) = "BuildType"
$Sheet.Cells.Item(1,21) = "Version"
$Sheet.Cells.Item(1,22) = "Administrator"
$Sheet.Cells.Item(1,23) = "SerialNumberOS"
$Sheet.Cells.Item(1,24) = "ServerOSConcat"




# Format it to make it a little more fancy.
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 15
$WorkBook.Font.Bold = $True

# Go to the second row.
$intRow = 2


$Domain = Get-Content C:\DomainControllers.txt
Foreach($DC in $Domain)
{
Remove-Item C:\ADcomputer.csv -Force
import-module activeDirectory
$a = Get-ADComputer -server $DC -filter * -properties Operatingsystem
$list= @()
Foreach ($os in $a)
{
If($os.Operatingsystem -match "windows Server *")
{
$list += $os | Select-Object name
}
}
$list | Export-Csv C:\adcomputer.csv -Force -NoTypeInformation



# Select text file which contains list of workstations to query.

$arrComputers = Import-Csv C:\adcomputer.csv | Select-Object Name -ExpandProperty name

# Get currently logged in username, client name, manufacturer and model from Win32_ComputerSystem class.
foreach ($strComputer in $arrComputers){

try { $colItems = Get-WmiObject Win32_ComputerSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,1) = $objItem.Name
$Sheet.Cells.Item($intRow,2) = ($objItem | % {$_.DNSHOSTName +'.'+$_.domain}).ToLower()
$Sheet.Cells.Item($intRow,3) = $objItem.Manufacturer
$Sheet.Cells.Item($intRow,4) = $objItem.Model
$Sheet.Cells.Item($intRow,5) = ($objItem.TotalPhysicalMemory/1024MB)
if($objItem.Model -like "VMWare*")
{
$Sheet.Cells.Item($intRow,6) = "Yes"
}
Else
{
$Sheet.Cells.Item($intRow,6)="No"
}
}



# Get workstation serial number from Win32_BIOS class.
$colItems = Get-WmiObject Win32_BIOS -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,7) = $objItem.SerialNumber
if($objItem.BiosVersion -eq $null)
{
$Sheet.Cells.Item($intRow,8)= "Unknown"
}
Else
{
$Sheet.Cells.Item($intRow,8) = $objItem.BiosVersion[0]
}
}
	
#Get OperatingSystme Imforamtion from Win32_OperatingSystem

$colItems = Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,17) = $objItem.Name | % { $_.Split("|")[0] }
$Sheet.Cells.Item($intRow,18) = $objItem.servicepackmajorversion
$Sheet.Cells.Item($intRow,19) = $objItem.BuildNumber
$Sheet.Cells.Item($intRow,20) = $objItem.BuildType
$Sheet.Cells.Item($intRow,21) = $objItem.Version
$Sheet.Cells.Item($intRow,23) = $objItem.SerialNumber
$Sheet.cells.Item($intRow,24) = "=(A$($intRow) & Q$($intRow))"

	}
	
#Get Local Administrator Information
$computer = [ADSI](”WinNT://” + $strComputer + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)
[string]$members = ($Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/', 'DOMAIN\') -replace ('WinNT://', '')
$m = $members.Replace(" ",",")
$Sheet.Cells.Item($intRow,22) = $m.Replace("/","\")
	
#Get DN and PasswordLastSet for computer from ActiveDirectory Query

Import-Module ActiveDirectory
$m = Get-ADComputer -Server $DC -identity $strComputer -Properties * | Select-Object -Property PasswordLastSet , DistinguishedName
$Sheet.Cells.Item($intRow , 15) = $m.passwordlastset.ToString("dd/MM/yyyy hh:mm tt")
$Sheet.Cells.Item($intRow , 16) = $m.DistinguishedName


# Get IP address and MAC address of the enabled network adapter from Win32_NetworkAdapaterConfiguration class.
$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer | where{$_.IPEnabled -eq “$true”}

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,9) = $objItem.IPAddress[0]
$Sheet.Cells.Item($intRow,10) = $objItem.MACAddress
$Sheet.Cells.Item($intRow,11) = $objItem.IPSubnet
$Sheet.Cells.Item($intRow,12) = $objItem.DefaultIPGateway
$Sheet.Cells.Item($intRow,13) = "IPv4"
$Sheet.Cells.Item($intRow,14) = $objItem.DHCPEnabled

$intRow+=1	
}

}

# Append workstation name to log file if uncontactable and show message in output window to advise.
    catch {
        Write-Host "Could not contact $($strComputer)" -ForegroundColor Red
        Out-File -FilePath C:\OCCPAR\couldnotreach1.txt -InputObject $strComputer -Append -Force
        }
}
}

Open in new window


(Note - I have taken this script from internet and modified according to my need)

You just need to create a text file (C:\Domaincontrollers.txt) and place all Domain contollers from differnet domain where you want to query servers

The script will populate the information in a excel file.

Make sure to run this script where RSAT is installed.

Let me know if you have any questions

Thanks,

-Prashant Girennavar.
1
 
damejenAuthor Commented:
Hi Prashant,

Thanks for the script I've created a .txt file with my Domain controllers though when I run the script it tells me "cannot find path C:\ADcomputer.csv because it does not exits?

Also it only writes to the 2nd line of the excel document and it overwrites the same line with each server it discovers...

What could be the issue, is there anything else i need to do before running the script?
0
 
Prashant GirennavarCommented:
ok

Let me know if below works for you

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

# Insert text in to first row.
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server"
$Sheet.Cells.Item(1,2) = "FQDNName"
$Sheet.Cells.Item(1,3) = "Manufacturer"
$Sheet.Cells.Item(1,4) = "Model"
$Sheet.Cells.Item(1,5) = "TotalPhysicalMemory"
$Sheet.Cells.Item(1,6) = "isVirtual" 
$Sheet.Cells.Item(1,7) = "SerialNumber" 
$Sheet.Cells.Item(1,8) = "BiosVersion"
$Sheet.Cells.Item(1,9) = "IPAddress"
$Sheet.Cells.Item(1,10) = "MACAddress"
$Sheet.Cells.Item(1,11) = "IPSubnet"
$Sheet.Cells.Item(1,12) = "DefaultGateway"
$Sheet.Cells.Item(1,13) = "Type"
$Sheet.Cells.Item(1,14) = "DHCPUse"
$Sheet.Cells.item(1,15) = "PasswordLastSet"
$Sheet.Cells.Item(1,16) = "DistinguisedName"
$Sheet.Cells.item(1,17) = "OperatingSystem"
$Sheet.Cells.Item(1,18) = "ServicePack"
$Sheet.Cells.item(1,19) = "BuildNumber"
$Sheet.Cells.Item(1,20) = "BuildType"
$Sheet.Cells.Item(1,21) = "Version"
$Sheet.Cells.Item(1,22) = "Administrator"
$Sheet.Cells.Item(1,23) = "SerialNumberOS"
$Sheet.Cells.Item(1,24) = "ServerOSConcat"




# Format it to make it a little more fancy.
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 15
$WorkBook.Font.Bold = $True

# Go to the second row.
$intRow = 2


$Domain = Get-Content C:\DomainControllers.txt
Foreach($DC in $Domain)
{
Remove-Item C:\ADcomputer.csv -Force
import-module activeDirectory
$a = Get-ADComputer -server $DC -filter * -properties Operatingsystem
$list= @()
Foreach ($os in $a)
{
If($os.Operatingsystem -match "windows Server *")
{
$list += $os | Select-Object name
}
}
$list | Export-Csv C:\adcomputer.csv -Force -NoTypeInformation



# Select text file which contains list of workstations to query.

$arrComputers = Import-Csv C:\adcomputer.csv | Select-Object Name -ExpandProperty name

# Get currently logged in username, client name, manufacturer and model from Win32_ComputerSystem class.
foreach ($strComputer in $arrComputers){

try { $colItems = Get-WmiObject Win32_ComputerSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,1) = $objItem.Name
$Sheet.Cells.Item($intRow,2) = ($objItem | % {$_.DNSHOSTName +'.'+$_.domain}).ToLower()
$Sheet.Cells.Item($intRow,3) = $objItem.Manufacturer
$Sheet.Cells.Item($intRow,4) = $objItem.Model
$Sheet.Cells.Item($intRow,5) = ($objItem.TotalPhysicalMemory/1024MB)
if($objItem.Model -like "VMWare*")
{
$Sheet.Cells.Item($intRow,6) = "Yes"
}
Else
{
$Sheet.Cells.Item($intRow,6)="No"
}
}



# Get workstation serial number from Win32_BIOS class.
$colItems = Get-WmiObject Win32_BIOS -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,7) = $objItem.SerialNumber
if($objItem.BiosVersion -eq $null)
{
$Sheet.Cells.Item($intRow,8)= "Unknown"
}
Else
{
$Sheet.Cells.Item($intRow,8) = $objItem.BiosVersion[0]
}
}
	
#Get OperatingSystme Imforamtion from Win32_OperatingSystem

$colItems = Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,17) = $objItem.Name | % { $_.Split("|")[0] }
$Sheet.Cells.Item($intRow,18) = $objItem.servicepackmajorversion
$Sheet.Cells.Item($intRow,19) = $objItem.BuildNumber
$Sheet.Cells.Item($intRow,20) = $objItem.BuildType
$Sheet.Cells.Item($intRow,21) = $objItem.Version
$Sheet.Cells.Item($intRow,23) = $objItem.SerialNumber
$Sheet.cells.Item($intRow,24) = "=(A$($intRow) & Q$($intRow))"

	}
	
#Get Local Administrator Information
$computer = [ADSI](”WinNT://” + $strComputer + “,computer”)
$Group = $computer.psbase.children.find(”Administrators”)
[string]$members = ($Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/', 'DOMAIN\') -replace ('WinNT://', '')
$m = $members.Replace(" ",",")
$Sheet.Cells.Item($intRow,22) = $m.Replace("/","\")
	
#Get DN and PasswordLastSet for computer from ActiveDirectory Query

Import-Module ActiveDirectory
$m = Get-ADComputer -Server $DC -identity $strComputer -Properties * | Select-Object -Property PasswordLastSet , DistinguishedName
$Sheet.Cells.Item($intRow , 15) = $m.passwordlastset.ToString("dd/MM/yyyy hh:mm tt")
$Sheet.Cells.Item($intRow , 16) = $m.DistinguishedName


# Get IP address and MAC address of the enabled network adapter from Win32_NetworkAdapaterConfiguration class.
$colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer | where{$_.IPEnabled -eq “$true”}

foreach($objItem in $colItems) {
$Sheet.Cells.Item($intRow,9) = $objItem.IPAddress[0]
$Sheet.Cells.Item($intRow,10) = $objItem.MACAddress
$Sheet.Cells.Item($intRow,11) = $objItem.IPSubnet
$Sheet.Cells.Item($intRow,12) = $objItem.DefaultIPGateway
$Sheet.Cells.Item($intRow,13) = "IPv4"
$Sheet.Cells.Item($intRow,14) = $objItem.DHCPEnabled


}
$intRow+=1

}

# Append workstation name to log file if uncontactable and show message in output window to advise.
    catch {
        Write-Host "Could not contact $($strComputer)" -ForegroundColor Red
        Out-File -FilePath C:\OCCPAR\couldnotreach1.txt -InputObject $strComputer -Append -Force
        }
}
}
                                            

Open in new window


Thanks,

-Prashant Girennavar.
1
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
damejenAuthor Commented:
Hi Prishant,

Thanks again for looking into this, although it doesn't seem to quite work for me.
The second issue of the text being overwritten in row 2 is still occuring rather than the script populating the other rows. So its only showing one row of server information, though it gets overwritten when it finds the next server.

The information it is pulling out is exactly what im looking for though apart from the issue above.
0
 
Prashant GirennavarCommented:
The script works for me.

$intRow +=1; is the variable which is responsible to increment the Rows in excel. Check it is proper.

If it does not work , copy your code and paste it here.

Thanks,

-Prashant Girennavar.
0
 
damejenAuthor Commented:
This is the code.

Line 41 in the original was $intRow = 2
I changed that to $intRow +=1 as you suggested. Same result.
0
 
Prashant GirennavarCommented:
I dont see the code.

Can you please send it once again.

Thanks,

-Prashant Girennavar.
0
 
damejenAuthor Commented:
sorry here you go, see attached.
Get-Servers.txt
0
 
damejenAuthor Commented:
Hi Prashant

Im not sure where I'm going wrong with this script. I've added the RSAT to the server i'm running the script on member 2012 server.

I seem to get the following.

It creates the excel file, but then I get the following (please see attachments.)
28-02-2014-16-52-21.png
28-02-2014-16-52-50.png
0
 
Prashant GirennavarCommented:
Do you have the windows server 2008 DC???

basically script is unable to contact do a query of DC as I suspect you are using w2k3 for DC query.

I suggest you to put the DC which has w2k8 as OS in domaincontoller.txt


Let me know if you don't have w2k8.

Thanks,

-Prashant Girennavar.
0
 
damejenAuthor Commented:
Hi Prashant,

No unfortunately the environment I'm using does not have a W2k8 DC's
FFL and DFL is at W2K3. We only have W2K3 DC's at the moment.

Many thanks for you help Prishant.
0
 
Prashant GirennavarCommented:
ok.

Then , you need to extract all the servers from AD and put them in a text file.

I have modified the script , Please populate all the server list in C:\Temp\Servers.txt

and run the script

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

# Insert text in to first row.
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server"
$Sheet.Cells.Item(1,2) = "FQDNName"
$Sheet.Cells.Item(1,3) = "Manufacturer"
$Sheet.Cells.Item(1,4) = "Model"
$Sheet.Cells.Item(1,5) = "TotalPhysicalMemory"
$Sheet.Cells.Item(1,6) = "isVirtual" 
$Sheet.Cells.Item(1,7) = "SerialNumber" 
$Sheet.Cells.Item(1,8) = "BiosVersion"
$Sheet.Cells.Item(1,9) = "IPAddress"
$Sheet.Cells.Item(1,10) = "MACAddress"
$Sheet.Cells.Item(1,11) = "IPSubnet"
$Sheet.Cells.Item(1,12) = "DefaultGateway"
$Sheet.Cells.Item(1,13) = "Type"
$Sheet.Cells.Item(1,14) = "DHCPUse"
$Sheet.Cells.item(1,15) = "PasswordLastSet"
$Sheet.Cells.Item(1,16) = "DistinguisedName"
$Sheet.Cells.item(1,17) = "OperatingSystem"
$Sheet.Cells.Item(1,18) = "ServicePack"
$Sheet.Cells.item(1,19) = "BuildNumber"
$Sheet.Cells.Item(1,20) = "BuildType"
$Sheet.Cells.Item(1,21) = "Version"
$Sheet.Cells.Item(1,22) = "Administrator"
$Sheet.Cells.Item(1,23) = "SerialNumberOS"
$Sheet.Cells.Item(1,24) = "ServerOSConcat"




# Format it to make it a little more fancy.
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 15
$WorkBook.Font.Bold = $True

# Go to the second row.
$intRow = 2


$arrComputers = Get-Content C:\Temp\Servers.txt

# Get currently logged in username, client name, manufacturer and model from Win32_ComputerSystem class.
foreach ($strComputer in $arrComputers){
    $strComputer =   $env:computername
        try { $colItems = Get-WmiObject Win32_ComputerSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer
        
        foreach($objItem in $colItems) {
            $Sheet.Cells.Item($intRow,1) = $objItem.Name
            $Sheet.Cells.Item($intRow,2) = ($objItem | % {$_.DNSHOSTName +'.'+$_.domain}).ToLower()
            $Sheet.Cells.Item($intRow,3) = $objItem.Manufacturer
            $Sheet.Cells.Item($intRow,4) = $objItem.Model
            $Sheet.Cells.Item($intRow,5) = ($objItem.TotalPhysicalMemory/1024MB)
            if($objItem.Model -like "VMWare*"){ $Sheet.Cells.Item($intRow,6) = "Yes"}
            Else {$Sheet.Cells.Item($intRow,6)="No"}
        }

        # Get workstation serial number from Win32_BIOS class.
        $colItems = Get-WmiObject Win32_BIOS -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer
        foreach($objItem in $colItems) { $Sheet.Cells.Item($intRow,7) = $objItem.SerialNumber
            if($objItem.BiosVersion -eq $null){ $Sheet.Cells.Item($intRow,8)= "Unknown"}
            Else {$Sheet.Cells.Item($intRow,8) = $objItem.BiosVersion[0]}
        }
	
        #Get OperatingSystme Imforamtion from Win32_OperatingSystem

        $colItems = Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

        foreach($objItem in $colItems) {
        $Sheet.Cells.Item($intRow,17) = $objItem.Name | % { $_.Split("|")[0] }
        $Sheet.Cells.Item($intRow,18) = $objItem.servicepackmajorversion
        $Sheet.Cells.Item($intRow,19) = $objItem.BuildNumber
        $Sheet.Cells.Item($intRow,20) = $objItem.BuildType
        $Sheet.Cells.Item($intRow,21) = $objItem.Version
        $Sheet.Cells.Item($intRow,23) = $objItem.SerialNumber
        $Sheet.cells.Item($intRow,24) = "=(A$($intRow) & Q$($intRow))"
        }
	
        #Get Local Administrator Information
        $computer = [ADSI](”WinNT://” + $strComputer + “,computer”)
        $Group = $computer.psbase.children.find(”Administrators”)
        [string]$members = ($Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/', 'DOMAIN\') -replace ('WinNT://', '')
        $m = $members.Replace(" ",",")
        $Sheet.Cells.Item($intRow,22) = $m.Replace("/","\")
	


        # Get IP address and MAC address of the enabled network adapter from Win32_NetworkAdapaterConfiguration class.
        $colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer | where{$_.IPEnabled -eq “$true”}

        foreach($objItem in $colItems) {
        $Sheet.Cells.Item($intRow,9) = $objItem.IPAddress[0]
        $Sheet.Cells.Item($intRow,10) = $objItem.MACAddress
        $Sheet.Cells.Item($intRow,11) = $objItem.IPSubnet
        $Sheet.Cells.Item($intRow,12) = $objItem.DefaultIPGateway
        $Sheet.Cells.Item($intRow,13) = "IPv4"
        $Sheet.Cells.Item($intRow,14) = $objItem.DHCPEnabled

        $intRow+=1	
        }
    }

# Append workstation name to log file if uncontactable and show message in output window to advise.
    catch {
        Write-Host "Could not contact $($strComputer)" -ForegroundColor Red
        Out-File -FilePath C:\couldnotreach1.txt -InputObject $strComputer -Append -Force
        }
}

Open in new window


Let me know if you need additional help

Thanks,

-Prashant Girennavar.
0
 
damejenAuthor Commented:
Hi Prashant,

Ok I've created another .txt file with my DC's under C:\Temp\Servers.txt.
Though now I seem to only get results from the Server I'm running the script from. and then the script completes. No other errors.
03-03-2014-12-13-32.png
0
 
Prashant GirennavarCommented:
Make sure you dont have spaces in the text file.

I would suggest you to put only 2 servers in a text file and run the script.

let me know your findings.

-Prashant Girennavar.
0
 
damejenAuthor Commented:
I've only put two Dc's without any spaces in the txt file e.g DC01,DC02

But it still only gives me output for the server I'm running the script on.  No other information on any other servers is being produced.

There are no errors from the script, just only getting information on one server, the one im running the script on.
0
 
Prashant GirennavarCommented:
Ok ,

Try this.

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

# Insert text in to first row.
$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = "Server"
$Sheet.Cells.Item(1,2) = "FQDNName"
$Sheet.Cells.Item(1,3) = "Manufacturer"
$Sheet.Cells.Item(1,4) = "Model"
$Sheet.Cells.Item(1,5) = "TotalPhysicalMemory"
$Sheet.Cells.Item(1,6) = "isVirtual" 
$Sheet.Cells.Item(1,7) = "SerialNumber" 
$Sheet.Cells.Item(1,8) = "BiosVersion"
$Sheet.Cells.Item(1,9) = "IPAddress"
$Sheet.Cells.Item(1,10) = "MACAddress"
$Sheet.Cells.Item(1,11) = "IPSubnet"
$Sheet.Cells.Item(1,12) = "DefaultGateway"
$Sheet.Cells.Item(1,13) = "Type"
$Sheet.Cells.Item(1,14) = "DHCPUse"
$Sheet.Cells.item(1,15) = "PasswordLastSet"
$Sheet.Cells.Item(1,16) = "DistinguisedName"
$Sheet.Cells.item(1,17) = "OperatingSystem"
$Sheet.Cells.Item(1,18) = "ServicePack"
$Sheet.Cells.item(1,19) = "BuildNumber"
$Sheet.Cells.Item(1,20) = "BuildType"
$Sheet.Cells.Item(1,21) = "Version"
$Sheet.Cells.Item(1,22) = "Administrator"
$Sheet.Cells.Item(1,23) = "SerialNumberOS"
$Sheet.Cells.Item(1,24) = "ServerOSConcat"




# Format it to make it a little more fancy.
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 15
$WorkBook.Font.Bold = $True

# Go to the second row.
$intRow = 2


$arrComputers = Get-Content C:\Temp\Servers.txt

# Get currently logged in username, client name, manufacturer and model from Win32_ComputerSystem class.
foreach ($strComputer in $arrComputers){
           try { $colItems = Get-WmiObject Win32_ComputerSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer
        
        foreach($objItem in $colItems) {
            $Sheet.Cells.Item($intRow,1) = $objItem.Name
            $Sheet.Cells.Item($intRow,2) = ($objItem | % {$_.DNSHOSTName +'.'+$_.domain}).ToLower()
            $Sheet.Cells.Item($intRow,3) = $objItem.Manufacturer
            $Sheet.Cells.Item($intRow,4) = $objItem.Model
            $Sheet.Cells.Item($intRow,5) = ($objItem.TotalPhysicalMemory/1024MB)
            if($objItem.Model -like "VMWare*"){ $Sheet.Cells.Item($intRow,6) = "Yes"}
            Else {$Sheet.Cells.Item($intRow,6)="No"}
        }

        # Get workstation serial number from Win32_BIOS class.
        $colItems = Get-WmiObject Win32_BIOS -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer
        foreach($objItem in $colItems) { $Sheet.Cells.Item($intRow,7) = $objItem.SerialNumber
            if($objItem.BiosVersion -eq $null){ $Sheet.Cells.Item($intRow,8)= "Unknown"}
            Else {$Sheet.Cells.Item($intRow,8) = $objItem.BiosVersion[0]}
        }
	
        #Get OperatingSystme Imforamtion from Win32_OperatingSystem

        $colItems = Get-WmiObject Win32_OperatingSystem -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer

        foreach($objItem in $colItems) {
        $Sheet.Cells.Item($intRow,17) = $objItem.Name | % { $_.Split("|")[0] }
        $Sheet.Cells.Item($intRow,18) = $objItem.servicepackmajorversion
        $Sheet.Cells.Item($intRow,19) = $objItem.BuildNumber
        $Sheet.Cells.Item($intRow,20) = $objItem.BuildType
        $Sheet.Cells.Item($intRow,21) = $objItem.Version
        $Sheet.Cells.Item($intRow,23) = $objItem.SerialNumber
        $Sheet.cells.Item($intRow,24) = "=(A$($intRow) & Q$($intRow))"
        }
	
        #Get Local Administrator Information
        $computer = [ADSI](”WinNT://” + $strComputer + “,computer”)
        $Group = $computer.psbase.children.find(”Administrators”)
        [string]$members = ($Group.psbase.invoke(”Members”) | %{$_.GetType().InvokeMember(”Adspath”, ‘GetProperty’, $null, $_, $null)}) -replace ('WinNT://DOMAIN/' + $server + '/'), '' -replace ('WinNT://DOMAIN/', 'DOMAIN\') -replace ('WinNT://', '')
        $m = $members.Replace(" ",",")
        $Sheet.Cells.Item($intRow,22) = $m.Replace("/","\")
	


        # Get IP address and MAC address of the enabled network adapter from Win32_NetworkAdapaterConfiguration class.
        $colItems = Get-WmiObject Win32_NetworkAdapterConfiguration -Namespace "root\CIMV2" -ErrorAction stop -ComputerName $strComputer | where{$_.IPEnabled -eq “$true”}

        foreach($objItem in $colItems) {
        $Sheet.Cells.Item($intRow,9) = $objItem.IPAddress[0]
        $Sheet.Cells.Item($intRow,10) = $objItem.MACAddress
        $Sheet.Cells.Item($intRow,11) = $objItem.IPSubnet
        $Sheet.Cells.Item($intRow,12) = $objItem.DefaultIPGateway
        $Sheet.Cells.Item($intRow,13) = "IPv4"
        $Sheet.Cells.Item($intRow,14) = $objItem.DHCPEnabled

        	$intRow+=1
        }
		
    }

# Append workstation name to log file if uncontactable and show message in output window to advise.
    catch {
        Write-Host "Could not contact $($strComputer)" -ForegroundColor Red
        Out-File -FilePath C:\couldnotreach1.txt -InputObject $strComputer -Append -Force
        }
}
                                            

Open in new window


-Prashant Girennavar.
0
 
damejenAuthor Commented:
This powershell script is originally intended to gather W2k8 Domain, Domain servers information, it has been amended by its creator Prashant Girennavar for W2K3 Domain
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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