Home Forums Programming Powershell power my PS script to automatically connect via SQL authentication and execute the command RE: power my PS script to automatically connect via SQL authentication and execute the command

  • Phil Parkin - Friday, October 6, 2017 6:49 AM

    This should get you going, though hard-coding username/password in a script is not good practice, as I'm sure you are aware.

    Yes very much right it is not the right thing..thanks for sharing me the link 
    Let me try to get this tested but still i am not progressing and feel the SQL authentication part is still not working. When i execute my script i am not getting an error but the o/p generating the expected fragmentation report from the database is not coming.

    # Connect to the specified instance
     [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
    $srv = new-object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST"

    #This sets the connection to mixed-mode authentication
    $srv.ConnectionContext.LoginSecure=$false;

    #This sets the login name
    $srv.ConnectionContext.set_Login("abc_XXXX");

    #This sets the password
    $srv.ConnectionContext.set_Password("defghijkl")

    Regards
    Eben