@@ -12,6 +12,7 @@ import (
1212 "strconv"
1313 "strings"
1414
15+ mssql "github.com/microsoft/go-mssqldb"
1516 "github.com/microsoft/go-mssqldb/azuread"
1617 "github.com/microsoft/go-sqlcmd/internal/localizer"
1718 "github.com/microsoft/go-sqlcmd/pkg/console"
@@ -70,7 +71,9 @@ type SQLCmdArguments struct {
7071 RemoveControlCharacters * int
7172 EchoInput bool
7273 QueryTimeout int
73- EnableColumnEnryption bool
74+ EnableColumnEncryption bool
75+ ChangePassword string
76+ ChangePasswordAndExit string
7477 // Keep Help at the end of the list
7578 Help bool
7679}
@@ -423,7 +426,9 @@ func setFlags(rootCmd *cobra.Command, args *SQLCmdArguments) {
423426 _ = rootCmd .Flags ().IntP (removeControlCharacters , "k" , 0 , localizer .Sprintf ("%s Remove control characters from output. Pass 1 to substitute a space per character, 2 for a space per consecutive characters" , "-k [1|2]" ))
424427 rootCmd .Flags ().BoolVarP (& args .EchoInput , "echo-input" , "e" , false , localizer .Sprintf ("Echo input" ))
425428 rootCmd .Flags ().IntVarP (& args .QueryTimeout , "query-timeout" , "t" , 0 , "Query timeout" )
426- rootCmd .Flags ().BoolVarP (& args .EnableColumnEnryption , "enable-column-encryption" , "g" , false , localizer .Sprintf ("Enable column encryption" ))
429+ rootCmd .Flags ().BoolVarP (& args .EnableColumnEncryption , "enable-column-encryption" , "g" , false , localizer .Sprintf ("Enable column encryption" ))
430+ rootCmd .Flags ().StringVarP (& args .ChangePassword , "change-password" , "z" , "" , localizer .Sprintf ("New password" ))
431+ rootCmd .Flags ().StringVarP (& args .ChangePasswordAndExit , "change-password-exit" , "Z" , "" , localizer .Sprintf ("New password and exit" ))
427432}
428433
429434func setScriptVariable (v string ) string {
@@ -682,11 +687,17 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq
682687 connect .ExitOnError = args .ExitOnError
683688 connect .ErrorSeverityLevel = args .ErrorSeverityLevel
684689 connect .DedicatedAdminConnection = args .DedicatedAdminConnection
685- connect .EnableColumnEnryption = args .EnableColumnEnryption
690+ connect .EnableColumnEncryption = args .EnableColumnEncryption
691+ if len (args .ChangePassword ) > 0 {
692+ connect .ChangePassword = args .ChangePassword
693+ }
694+ if len (args .ChangePasswordAndExit ) > 0 {
695+ connect .ChangePassword = args .ChangePasswordAndExit
696+ }
686697}
687698
688699func isConsoleInitializationRequired (connect * sqlcmd.ConnectSettings , args * SQLCmdArguments ) bool {
689- iactive := args .InputFile == nil && args .Query == ""
700+ iactive := args .InputFile == nil && args .Query == "" && len ( args . ChangePasswordAndExit ) == 0
690701 return iactive || connect .RequiresPassword ()
691702}
692703
@@ -749,8 +760,23 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
749760 // connect using no overrides
750761 err = s .ConnectDb (nil , line == nil )
751762 if err != nil {
752- s .WriteError (s .GetError (), err )
753- return 1 , err
763+ switch e := err .(type ) {
764+ // 18488 == password must be changed on connection
765+ case mssql.Error :
766+ if e .Number == 18488 && line != nil && len (args .Password ) == 0 && len (args .ChangePassword ) == 0 && len (args .ChangePasswordAndExit ) == 0 {
767+ b , _ := line .ReadPassword (localizer .Sprintf ("Enter new password:" ))
768+ s .Connect .ChangePassword = string (b )
769+ err = s .ConnectDb (nil , true )
770+ }
771+ }
772+ if err != nil {
773+ s .WriteError (s .GetError (), err )
774+ return 1 , err
775+ }
776+ }
777+
778+ if len (args .ChangePasswordAndExit ) > 0 {
779+ return 0 , nil
754780 }
755781
756782 script := vars .StartupScriptFile ()
0 commit comments