Poershell script to chechk the db owner ?

  • Guys,

    Can you help me in providing the powershell script( or any link wheer i can get it)to check the db owner?I am very new to powershell

    Thanks,

    Mithra

  • This should get you what you want using SMO:

    param (

    [string] $server,

    [string] $database

    )

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") |

    Out-Null

    $sql_server = New-Object ("Microsoft.SqlServer.Management.Smo.Server") $server

    $db = $sql_server.Databases.Item($database)

    $db_owner = $db.Owner

    Write-Host "$database Owner: $db_owner"

    Save this as a script file and it can be called like:

    .\GetDbOwner.ps1 -server YourServer\YourInstance -database YourDatabase

    Here is a version-controlled copy of the above script for future reference.



    Twitter: @SQLife
    Email: sqlsalt(at)outlook(dot)com

  • WHY are we making a trip to Powershell for this property???

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thanks a lot,Thomas!!!!

  • Jeff Moden (7/1/2012)


    WHY are we making a trip to Powershell for this property???

    Well, I used something like this once to check ~30 instances for any db's that still had the previous DBA as the owner. 🙂

  • Jeff Moden (7/1/2012)


    WHY are we making a trip to Powershell for this property???

    Multi-instance and multi-server would have been my guess. Without a CMS in place, it would make sense to me.



    Twitter: @SQLife
    Email: sqlsalt(at)outlook(dot)com

  • Thanks guys... I understand why some of us might do such a thing but I really want to know why the OP wants to do it. If it's not for something like multiple servers, then there are much easier ways to get this information than a trip to Powershell.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/2/2012)


    Thanks guys... I understand why some of us might do such a thing but I really want to know why the OP wants to do it. If it's not for something like multiple servers, then there are much easier ways to get this information than a trip to Powershell.

    Oh absolutely, I couldn't agree more. If this is a single instance then that'd be like building a clock just to find out what time it is.



    Twitter: @SQLife
    Email: sqlsalt(at)outlook(dot)com

  • Jeff Moden (7/2/2012)


    Thanks guys... I understand why some of us might do such a thing but I really want to know why the OP wants to do it. If it's not for something like multiple servers, then there are much easier ways to get this information than a trip to Powershell.

    Very true. I think at the time, I was also trying to teach myself Powershell; and this was a an easy query to compare expected results with the actual values.

    I'm still rubbish at PS though 😀

  • Thomas Stringer (7/2/2012)


    Jeff Moden (7/2/2012)


    Thanks guys... I understand why some of us might do such a thing but I really want to know why the OP wants to do it. If it's not for something like multiple servers, then there are much easier ways to get this information than a trip to Powershell.

    Oh absolutely, I couldn't agree more. If this is a single instance then that'd be like building a clock just to find out what time it is.

    BWAA-HAAA!!! I love the analogy! Thanks for the great laugh, Thomas... and on a Monday morning, no less. 🙂

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (7/2/2012)


    Thomas Stringer (7/2/2012)


    Jeff Moden (7/2/2012)


    Thanks guys... I understand why some of us might do such a thing but I really want to know why the OP wants to do it. If it's not for something like multiple servers, then there are much easier ways to get this information than a trip to Powershell.

    Oh absolutely, I couldn't agree more. If this is a single instance then that'd be like building a clock just to find out what time it is.

    BWAA-HAAA!!! I love the analogy! Thanks for the great laugh, Thomas... and on a Monday morning, no less. 🙂

    😀 Glad I could provide some Monday morning humor!!!



    Twitter: @SQLife
    Email: sqlsalt(at)outlook(dot)com

  • i'm a dinosaur and prefer good old T-SQL 🙂

    USE [SomeDB]

    GO

    SELECT 'The database owner is: ' + QUOTENAME(SUSER_SNAME(dp.sid))

    FROM sys.database_principals dp

    WHERE dp.name = 'dbo'

    GO

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply