@echo off

REM Licensed to the Apache Software Foundation (ASF) under one
REM or more contributor license agreements.  See the NOTICE file
REM distributed with this work for additional information
REM regarding copyright ownership.  The ASF licenses this file
REM to you under the Apache License, Version 2.0 (the
REM "License"); you may not use this file except in compliance
REM with the License.  You may obtain a copy of the License at
REM
REM   http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing,
REM software distributed under the License is distributed on an
REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
REM KIND, either express or implied.  See the License for the
REM specific language governing permissions and limitations
REM under the License.

rem ----------------------------------------------------------------------------------------------------------------
set PasswordReset_Script=cloud-set-guest-password-configdrive.vbs
echo Set owshNet = CreateObject^("WScript.Network"^)                                                                                                 > %PasswordReset_Script%
echo strComputer = owshNet.ComputerName                                                                                                             >> %PasswordReset_Script%
echo Const HKEY_CURRENT_USER = ^&H80000001                                                                                                          >> %PasswordReset_Script%
echo registryKeyPath = "SOFTWARE\CLOUDSTACKmd5Checksum\"                                                                                            >> %PasswordReset_Script%
echo registryKeyName = "PasswordMd5Checksum"                                                                                                        >> %PasswordReset_Script%
echo CDPath = ""                                                                                                                                    >> %PasswordReset_Script%
echo configLabelName = "config-2"                                                                                                                     >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo Set fso = CreateObject ^("Scripting.FileSystemobject"^)                                                                                        >> %PasswordReset_Script%
echo For Each drive In fso.Drives                                                                                                                   >> %PasswordReset_Script%
echo    If drive.DriveType = 4 And drive.IsReady Then                                                                                               >> %PasswordReset_Script%
echo       If drive.VolumeName = configLabelName Then                                                                                               >> %PasswordReset_Script%
echo            CDPath = drive.Path                                                                                                                 >> %PasswordReset_Script%
echo       End If                                                                                                                                   >> %PasswordReset_Script%
echo       Exit For                                                                                                                                 >> %PasswordReset_Script%
echo    End If                                                                                                                                      >> %PasswordReset_Script%
echo Next                                                                                                                                           >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo IF CDPath = "" Then                                                                                                                            >> %PasswordReset_Script%
echo    WScript.Echo "Config Drive not found to set the password to VM"                                                                             >> %PasswordReset_Script%
echo    WScript.Quit                                                                                                                                >> %PasswordReset_Script%
echo End If                                                                                                                                         >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo PasswordFileName = "\VM_PASSWORD.txt"                                                                                                          >> %PasswordReset_Script%
echo PasswordFilePath = CDPath ^& "\CLOUDSTACK\PASSWORD" ^& PasswordFileName                                                                        >> %PasswordReset_Script%
echo Set ObjFile = fso.OpenTextFile^(PasswordFilePath^)                                                                                             >> %PasswordReset_Script%
echo If Err.Number ^<^> 0 Then                                                                                                                      >> %PasswordReset_Script%
echo    WScript.Echo "Error: " ^& PasswordFilePath ^& " --- " ^& Err.Description                                                                    >> %PasswordReset_Script%
echo    WScript.Quit                                                                                                                                >> %PasswordReset_Script%
echo End If                                                                                                                                         >> %PasswordReset_Script%
echo Password = ObjFile.ReadLine                                                                                                                    >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo PasswordMd5checsumFileName = "\VM_PASSWORD_MD5CHECKSUM.txt"                                                                                    >> %PasswordReset_Script%
echo PasswordMd5checsumFilePath = CDPath ^& "\CLOUDSTACK\PASSWORD" ^& PasswordMd5checsumFileName                                                    >> %PasswordReset_Script%
echo Set ObjFile2 = fso.OpenTextFile^(PasswordMd5checsumFilePath^)                                                                                  >> %PasswordReset_Script%
echo If Err.Number ^<^> 0 Then                                                                                                                      >> %PasswordReset_Script%
echo    WScript.Echo "Error: " ^& PasswordMd5checsumFilePath ^& " --- " ^& Err.Description                                                          >> %PasswordReset_Script%
echo    WScript.Quit                                                                                                                                >> %PasswordReset_Script%
echo End If                                                                                                                                         >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo registryKeyValue = ObjFile2.ReadLine                                                                                                           >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo ' The below logic is to make sure script sets the password to VM only when new password appears in config drive                                >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo Set registryObject=GetObject^("winmgmts:{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\default:StdRegProv"^)                    >> %PasswordReset_Script%
echo If registryObject.EnumKey^(HKEY_CURRENT_USER, registryKeyPath, "", ""^) = 0 Then                                                               >> %PasswordReset_Script%
echo    registryObject.GetStringValue HKEY_CURRENT_USER, registryKeyPath, registryKeyName, previousRegistryKeyValue                                 >> %PasswordReset_Script%
echo    If ^(previousRegistryKeyValue ^<^> registryKeyValue^) Then                                                                                  >> %PasswordReset_Script%
echo        SavePasswordChecksum^(^)                                                                                                                >> %PasswordReset_Script%
echo        SetPasswordToVM^(^)                                                                                                                     >> %PasswordReset_Script%
echo    End If                                                                                                                                      >> %PasswordReset_Script%
echo Else                                                                                                                                           >> %PasswordReset_Script%
echo    Return = registryObject.CreateKey^(HKEY_CURRENT_USER, registryKeyPath^)                                                                     >> %PasswordReset_Script%
echo    If ^(Return = 0^) And ^(Err.Number = 0^) Then                                                                                               >> %PasswordReset_Script%
echo        SavePasswordChecksum^(^)                                                                                                                >> %PasswordReset_Script%
echo        SetPasswordToVM^(^)                                                                                                                     >> %PasswordReset_Script%
echo    Else                                                                                                                                        >> %PasswordReset_Script%
echo        Wscript.Echo Now ^& vbTab ^& "Create Registry Key failed while setting the password to VM. Error = " ^& Err.Number                      >> %PasswordReset_Script%
echo    End If                                                                                                                                      >> %PasswordReset_Script%
echo End If                                                                                                                                         >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo '                                                                                                                                              >> %PasswordReset_Script%
echo ' Procedure to set the password to the VM                                                                                                      >> %PasswordReset_Script%
echo '                                                                                                                                              >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo Sub SetPasswordToVM^(^)                                                                                                                        >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo    Set objUser = GetObject^("WinNT://" ^& strComputer ^& "/administrator"^)                                                                    >> %PasswordReset_Script%
echo    If Err.Number ^<^> 0 Then                                                                                                                   >> %PasswordReset_Script%
echo        WScript.Echo Now ^& vbTab ^& "Error connecting to " ^& strComputer ^& " --- " ^& Err.Description                                        >> %PasswordReset_Script%
echo        Err.Clear                                                                                                                               >> %PasswordReset_Script%
echo        WScript.Quit                                                                                                                            >> %PasswordReset_Script%
echo    Else                                                                                                                                        >> %PasswordReset_Script%
echo        Select Case Password                                                                                                                    >> %PasswordReset_Script%
echo            Case ""                                                                                                                             >> %PasswordReset_Script%
echo                WScript.Echo Now ^& vbTab ^& "Config drive did not have any password for the VM"                                                >> %PasswordReset_Script%
echo                WScript.Quit                                                                                                                    >> %PasswordReset_Script%
echo            Case "saved_password"                                                                                                               >> %PasswordReset_Script%
echo                WScript.Echo Now ^& vbTab ^& "VM has already saved the password"                                                                >> %PasswordReset_Script%
echo                WScript.Quit                                                                                                                    >> %PasswordReset_Script%
echo            Case else                                                                                                                           >> %PasswordReset_Script%
echo                objUser.Setpassword Password                                                                                                    >> %PasswordReset_Script%
echo                objUser.Setinfo                                                                                                                 >> %PasswordReset_Script%
echo                If Err.Number ^<^> 0 Then                                                                                                       >> %PasswordReset_Script%
echo                    wscript.Echo Now ^& vbTab ^& "Error setting password for " ^& strComputer ^& "\Administrator" ^& " --- " ^& Err.Description >> %PasswordReset_Script%
echo                    Err.Clear                                                                                                                   >> %PasswordReset_Script%
echo                    WScript.Quit                                                                                                                >> %PasswordReset_Script%
echo                Else                                                                                                                            >> %PasswordReset_Script%
echo                    WScript.Echo Now ^& vbTab ^& "Password set for " ^& strComputer ^& "\Administrator"                                         >> %PasswordReset_Script%
echo                End If                                                                                                                          >> %PasswordReset_Script%
echo        End Select                                                                                                                              >> %PasswordReset_Script%
echo    End If                                                                                                                                      >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo End Sub                                                                                                                                        >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo Sub SavePasswordChecksum^(^)                                                                                                                   >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo    Return = registryObject.SetStringValue^(HKEY_CURRENT_USER, registryKeyPath, registryKeyName, registryKeyValue^)                             >> %PasswordReset_Script%
echo    If ^(Return ^<^> 0^) Or ^(Err.Number ^<^> 0^) Then                                                                                          >> %PasswordReset_Script%
echo        Wscript.Echo Now ^& vbTab ^& "SetStringValue to Registry Key failed while setting the password to VM. Error = " ^& Err.Number           >> %PasswordReset_Script%
echo    End If                                                                                                                                      >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
echo End Sub                                                                                                                                        >> %PasswordReset_Script%
echo.                                                                                                                                               >> %PasswordReset_Script%
rem -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

cscript //Nologo %PasswordReset_Script%
