join the MSSQLTips community

MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

 
Retrieve List of Databases and their Properties using PowerShell - MSSQLTips

MSSQLTips

MSSQLTips.com - your daily source for SQL Server tips
Welcome to MSSQLTips Sign in | Join | Help
in Search

Retrieve List of Databases and their Properties using PowerShell

Last post 10-20-2009 8:58 AM by bass_player. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 05-26-2009 12:30 AM

    Retrieve List of Databases and their Properties using PowerShell

    This post is related to this tip: Retrieve List of Databases and their Properties using PowerShell

    http://www.mssqltips.com/tip.asp?tip=1759

  • 05-27-2009 12:40 PM In reply to

    • sqljim
    • Not Ranked
    • Joined on 05-27-2009
    • Posts 1

    Re: Retrieve List of Databases and their Properties using PowerShell

    Great tip. I was trying to add the datafiles, log files and their sizes to the export to Excel but, couldn't get the right property. Suggestion on how to get it?

  • 05-27-2009 4:05 PM In reply to

    • Jacare
    • Not Ranked
    • Joined on 05-27-2009
    • Posts 1

    Re: Retrieve List of Databases and their Properties using PowerShell

    When I try to execute a following statement $Excel = $Excel.Workbooks.Add() in powershell I receive this error:

    "Old format
    or invalid type library. (Exception from HRESULT: 0x80028018
    (TYPE_E_INVDATAREAD))" message..

    What can I do to get around this ?

  • 05-28-2009 9:30 AM In reply to

    Re: Retrieve List of Databases and their Properties using PowerShell

    You can add this inside the ForEach loop for the databases collection but you do need to have it formatted properly in Excel as each database would have at least two files

        $fileGroups = $db.FileGroups
        ForEach ($fg in $fileGroups)
        {
            $fg.Files | Select $db.Name, Name, FileName, size, UsedSpace
        }
        $db.LogFiles | Select $db.Name, Name, FileName, Size, UsedSpace

  • 05-28-2009 9:42 AM In reply to

    Re: Retrieve List of Databases and their Properties using PowerShell

    This might be because of the difference between the local user settings and Office settings.  Check the Regional Settings in your user profile or you can include a line in the PowerShell script to call the CultureInfo class but that's a bit geeky

    CultureInfo Class

    http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.aspx 

    Here's a Microsoft KB article for this

    http://support.microsoft.com/default.aspx?scid=kb;en-us;320369

  • 07-03-2009 7:56 AM In reply to

    Re: Retrieve List of Databases and their Properties using PowerShell

    Hi, I too got the same error. I've found the following code but can't seem to get any further :-(

    $xl = New-Object -comobject "excel.application"

    $xl.visible = $true

    $xlbooks =$xl.workbooks

    $newci = [System.Globalization.CultureInfo]"en-GB"

    $xlbooks.PSBase.GetType().InvokeMember("Add", [Reflection.BindingFlags]::InvokeMethod, $null, $xlbooks, $null, $newci)

    Any ideas how I can incorporate the code from the article below, into the above, so I can reference the $Sheets object? - ($Sheets.Cells.Item() etc)

    $Excel = New-Object -ComObject Excel.Application

    $Excel.visible = $True

    $Excel = $Excel.Workbooks.Add()

    $Sheet = $Excel.Worksheets.Item(1)

  • 07-25-2009 2:43 PM In reply to

    Re: Retrieve List of Databases and their Properties using PowerShell

    Great script.

     I was looking to format my output like this....

    server1,db1

    server1,db2

    server1,db3

    server2,db1

    but haven't been able to figure out how to get $s.name and $s.databases to output that way.  Any suggestions?

  • 10-20-2009 8:58 AM In reply to

    Re: Retrieve List of Databases and their Properties using PowerShell

    If it is just the output without the Excel thing, you can simply do this. Remember, I am just reading the names of the SQL Server instances from a text file so you can simply use that variable beside the database name

    #################################################
    #Read thru the contents of the SQL_Servers.txt file
    foreach ($instance in get-content "D:\SQL_Servers.txt")
    {

    #GetDBs.ps1
    #This script gets SQL Server database information using PowerShell


    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO')| out-null

    # Create an SMO connection to the instance
    $s = new-object ('Microsoft.SqlServer.Management.Smo.Server') $instance

    $dbs = $s.Databases

    foreach ($db in $dbs)
        {
            Write-Host $instance , $db.Name
        }

    }




Page 1 of 1 (8 items)