Archive for the ‘WMI’ tag
Exporting Data to Excel using PowerShell
I had to get a quick audit of systems and i needed to know OS Version, SP version, Memory and Disk Details.
One problem is that if you don’t know what disks you have exporting this to excel is difficult. I chose to dynamically create the headers for the excel file based on the drive letter names. This approach is by no means perfect as you rely on the fact that people have built the systems with sequential drive letters. Any drive letter out of sequence will disturb the excel header names.
Example: If you have 3 servers and two have letters C: and D: and the third has C: and E: this approach will not work for you. Luckily I know my servers have sequential drive letters.
Code:
$excel = New-Object -comobject Excel.Application $excel.visible = $True $wbook = $excel.Workbooks.Add() $wsheet = $wbook.Worksheets.Item(1) $wsheet.Cells.Item(1,1) = "Date" $wsheet.Cells.Item(1,2) = "Server" $wsheet.Cells.Item(1,3) = "Memory" $wsheet.Cells.Item(1,4) = "OSName" $wsheet.Cells.Item(1,5) = "SPVersion" $iRow = 2 $InputFile = "C:\Server.txt" #Input File Containing Servers to be Scanned $Servers = Get-Content $InputFile ForEach($Server in $Servers) { $wsheet.Cells.Item($iRow,1) = Get-Date $wsheet.Cells.Item($iRow,2) =$Server $AllCS = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Server | Select-Object TotalPhysicalMemory ForEach($CS In $AllCS){$wsheet.Cells.Item($iRow,3) = $CS.TotalPhysicalMemory/1GB} $AllOS = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $Server ForEach($OS In $AllOS){$OSVersion = $OS.Caption;$SPVersion = $OS.CSDVersion} $wsheet.Cells.Item($iRow,4) = $OSVersion $wsheet.Cells.Item($iRow,5) = $SPVersion $logicalDisk = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $Server -Filter "DriveType=3" | Select-Object Caption,Size $j = 6 For($i=0;$i -le $LogicalDisk.Length - 1; $i++){ $wsheet.Cells.Item(1,$j) = $LogicalDisk[$i].Caption $wsheet.Cells.Item($iRow,$j) = $LogicalDisk[$i].Size/1GB $j++} $iRow++ } $range = $wsheet.UsedRange $range.Interior.ColorIndex = 19 $range.Font.ColorIndex = 11 $range.Font.Bold = $True $range.EntireColumn.AutoFilter() $range.EntireColumn.AutoFit() $excel.ActiveWorkbook.SaveAs("C:\Server_Data.xls") $excel.ActiveWorkbook.Close $excel.Application.Quit If (ps excel) { kill -name excel}
Hope this helps.
All information is provided on an AS-IS basis, with no warranties and confers no rights.
Disconnected Mailboxes in Exchange 2003
I was looking into finding all disconnected/orphaned mailboxes in an Exchange2003 org. Normally I would use either an old VBScript I have or the Exchange 2003 ESM. I thought it would be good to experiment with PowerShell and try to simplify the VBScript that I have.
Below is a PowerShell script that utilizes ADSI to gather all Exchange 2003 servers in your configuration container. Then for each server it utilizes WMI to connect to the Root/MicrosoftExchangeV2 ‘NameSpace’ and the Exchange_Mailbox provider. From there we filter on only mailboxes that the “DateDiscoveredAbscentInDS” property is NOT NULL. That data is then passed through the pipeline to a Select-Object where a custom Hash table is built to display the Server Name of the filtered mailboxes. The filtered mailboxes and the selected data is then passed through the pipeline and exported to a .csv file on the root of C:\ named with the format C:\DisconnectedMailboxes_%Servername%.csv.
Code:
[String]$StrFilter = "(objectCategory=msExchExchangeServer)" $objRootDSE = [ADSI]"LDAP://RootDSE" [String]$strContainer = $objRootDSE.configurationNamingContext $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-object System.DirectoryServices.DirectoryEntry("LDAP://$strContainer") $objSearcher.PageSize = 1000 $objSearcher.Filter = $strFilter $objSearcher.SearchScope = "Subtree" $colResults = $objSearcher.FindAll() ForEach ($objResult in $colResults) { [String]$ServerDN = $objResult.Properties.distinguishedname $objServer = [ADSI]"LDAP://$ServerDN" [String]$Server = $objServer.cn Get-WmiObject -NameSpace Root\MicrosoftExchangeV2 -Class Exchange_Mailbox -Computer $Server | ` Where { $_.DateDiscoveredAbsentInDS -ne $null } | ` Select-Object @{N="Server";E={$_.__Server}},` StorageGroupName,StoreName,MailboxDisplayName,Size,DateDiscoveredAbsentInDS | ` Export-Csv "C:\DisconnectedMailboxes_$Server.csv" -NoTypeInformation }#End ForEach
Hope this helps.
All information is provided on an AS-IS basis, with no warranties and confers no rights.
Exporting 2003 Mailbox Data to CSV
I have been using this code for a while and thought I would post it.
I manage several Exchange 2003 servers, this PowerShell code will allow you to pull Mailbox data from each server into their own .CSV file.
It uses a built Array for the server list and parses the Array to get Mailbox data from each server.
$Mailboxes = $Null $Computers = "Server1","Server2","Server3","Server4" ForEach($Computer in $Computers) { $Mailboxes = Get-WmiObject -NameSpace Root\MicrosoftExchangeV2 -Class Exchange_Mailbox -Computer $Computer $Mailboxes | Export-Csv "C:\Mailboxes_$Computer.csv" -NoTypeInformation }
You can combine this with ADSI code to discover all your Exchange 2003 servers in your Directory.
This would give you Mailbox data on all of your Exchange 2003 servers without the need for a manual Array.
[Array]$ExchSrvs = @("") [String]$StrFilter = “(objectCategory=msExchExchangeServer)” $objRootDSE = [ADSI]“LDAP://RootDSE” [String]$strContainer = $objRootDSE.configurationNamingContext $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-object System.DirectoryServices.DirectoryEntry(”LDAP://$strContainer”) $objSearcher.PageSize = 1000 $objSearcher.Filter = $strFilter $objSearcher.SearchScope = “Subtree” $colServers = $objSearcher.FindAll() ForEach($objServer in $colServers) { [String]$Server = $objResult.Properties.name $ExchSrvs += $Server } $ExchSrvs.Count
Your final script would look something like this.
[Array]$ExchSrvs = @("") [String]$StrFilter = “(objectCategory=msExchExchangeServer)” $objRootDSE = [ADSI]“LDAP://RootDSE” [String]$strContainer = $objRootDSE.configurationNamingContext $objSearcher = New-Object System.DirectoryServices.DirectorySearcher $objSearcher.SearchRoot = New-object System.DirectoryServices.DirectoryEntry(”LDAP://$strContainer”) $objSearcher.PageSize = 1000 $objSearcher.Filter = $strFilter $objSearcher.SearchScope = “Subtree” $colServers = $objSearcher.FindAll() ForEach($objServer in $colServers) { [String]$Server = $objResult.Properties.name $Mailboxes = $Null $Mailboxes = Get-WmiObject -NameSpace Root\MicrosoftExchangeV2 -Class Exchange_Mailbox -Computer $Server $Mailboxes | Export-Csv "C:\Mailboxes_$Server.csv" -NoTypeInformation }
Hope this helps.
All information is provided on an AS-IS basis, with no warranties and confers no rights.
Getting Exchange 2003 Database Statistics
I routinely analyze Exchange 2003 stores. To do this I previously had written a VB script that dumps all the mailboxes on an Exchange 2003 server using the Exchange_Mailbox WMI class, but as this is a PowerShell article i hear you say “How does this help me?”.
I use PowerShell more and more I really enjoy the simplicity and power of the PowerShell cmdline. So I started to look into producing some clean one liners or small scripts to produce something similar to my large VB script solution.
Note: In all examples below just replace “Server” with an Exchange 2003 server name.
Getting all Exchange 2003 Mailboxes on a single server using WMI,
GWMI -NameSpace Root\MicrosoftExchangeV2 -Class Exchange_Mailbox -ComputerName $Server | Export-CSV "C:\Scripts\Mailboxes.csv" -NoTypeInformation
The next step is to start filtering the output to get detailed information on trouble spots, for example to look for any mailboxes that contain more than ’5000′ items.
To find these we use the ‘.totalItems’ property of the ‘Exchange_Mailbox’ class.
Getting all Exchange 2003 Mailboxes on a single server that have more than 5000 ‘Items’,
GWMI -NameSpace Root\MicrosoftExchangeV2 -Class Exchange_Mailbox -ComputerName $Server | Where{$_.totalitems -gt '5000'} | Export-CSV "C:\Scripts\Mailboxes.csv" -NoTypeInformation
Another WMI ‘Exchange_Mailbox’ class property I use is the ‘.DateDiscoveredAbsentInDS’ this tells me the date the mailbox was disconnected/deleted from the User Object in AD. If for any reason you do not want the store to clean up these automatically you can script the purge of the mailboxes using VBScript or PowerShell. The below code will enable you to see the mailboxes that are ready to be purged but will take no action.
Getting all Exchange 2003 Mailboxes that have been deleted (disconnected from their AD User object) and are ready to be ‘Purged’ on a single server,
GWMI -NameSpace Root\MicrosoftExchangeV2 -Class Exchange_Mailbox -ComputerName $Server | Where{$_.DateDiscoveredAbsentInDS -ne $NULL} | Export-CSV "C:\Scripts\Mailboxes.csv" -NoTypeInformation
Another part to the Exchange Mailbox is the counterpart attributes held in the User object. Some of these attributes are the status of the Mailbox, for example “Is the user over his/her set limits?” or “Is the account disabled in AD?”. To find out the answers here we need to interrogate the user object attribute values. To do this there are many methods depending upon the version of PowerShell you are using and if you have the Quest Management tools loaded. In the example below i am utilizing the Quest Management Tools.
Hope this helps.
All information is provided on an AS-IS basis, with no warranties and confers no rights.
Exporting Server Network Configuration to Excel using PowerShell
Win32_NetworkAdapterConfiguration
Win32_NetworkAdapter
Recently I had to examine over 100+ servers and their network configurations prior to a network IP address change. I also wanted the output in an Excel file for easy transport to my group. I used the above two WMI classes to gather all the required data i needed, the script requires the Excel application installed on the machine to have access to the excel.application COM object. I used an .TXT file as the input for the script, the file contains the servers that I wanted to scan. The file is formatted with one server name per line with no spaces.
All information is provided on an AS-IS basis, with no warranties and confers no rights.
