Forum Clubic

Besoin D'aide pour un script vbs

salut a tous, je suis en stage dans une boite d’informatique, je suis en train de faire en sorte qu’il ne reste plus qu’une seule machine dans la boite, qui émule d’autres serveurs via VMware. La, je suis en train de mettre en place un systeme de sauvegarde, et mon directeur technique voudrait que ceci se présente de la maniere suivante: via un script vbs mettre en pause la machine virtuel, faire une sauvegarde vers telle destination, puis enfin redemarrer la machine virtuelle. Pouvez vous m’aider à trouver un bout de script qui mette la machine en pause, je pense que je vais réussir a trouver pour la sauvegarde.

Merci d’avance

bon, je commence à avancer, je réussi a mettre en pause, à lancer la sauvegarde quand la machine virutelle s’est enfin mis en pause, puis le remettre en lecture. le truc maintenant est d’arriver à ne pas relancer la machine virtual tant que le processus ubkernel.exe n’est pas terminé.
voila le code, une fois que j’aurais trouvé comment verifier que le processus est terminer, il faudra que je trouve comment mettre en pause une seule machine virtuelle

'======================================

’ Description: VMware VmCOM 1.0 Type Library

’ Name: VMCOMLib
’ Version: 1.0 [of Type Library]
’ GUID: {0407CAC2-41D7-4EB4-898B-4EC06C57BB81}

’ File: VmCOM.dll
’ Version: 1.0.0.1 [of file]

’ Extracted on: 2002.08.28 00:53:08

'=====================================
’ VmErr
'=====================================
Const vmErr_ALREADYCONNECTED = -2147220980
Const vmErr_BADRESPONSE = -2147220978
Const vmErr_BADSTATE = -2147220984
Const vmErr_BADVERSION = -2147220986
Const vmErr_DISCONNECT = -2147220979
Const vmErr_GARBAGE = -2147220977
Const vmErr_INSUFFICIENT_RESOURCES = -2147220970
Const vmErr_INVALIDARGS = -2147220989
Const vmErr_INVALIDVM = -2147220974
Const vmErr_NEEDINPUT = -2147220976
Const vmErr_NETFAIL = -2147220990
Const vmErr_NOACCESS = -2147220988
Const vmErr_NOEXECVMAUTHD = -2147220983
Const vmErr_NOMEM = -2147220991
Const vmErr_NOPROPERTY = -2147220982
Const vmErr_NOSUCHVM = -2147220981
Const vmErr_NOTCONNECTED = -2147220987
Const vmErr_NOTSUPPORTED = -2147220975
Const vmErr_PROXYFAIL = -2147220969
Const vmErr_TIMEOUT = -2147220985
Const vmErr_UNSPECIFIED = -2147219993
Const vmErr_VMBUSY = -2147220971
Const vmErr_VMEXISTS = -2147220972
Const vmErr_VMINITFAILED = -2147220973
'=====================================
’ VmExecutionState
'=====================================
Const vmExecutionState_Off = 2
Const vmExecutionState_On = 1
Const vmExecutionState_Stuck = 4
Const vmExecutionState_Suspended = 3
Const vmExecutionState_Unknown = 5
'=====================================
’ VmPlatform
'=====================================
Const vmPlatform_LINUX = 2
Const vmPlatform_UNKNOWN = 4
Const vmPlatform_VMNIX = 3
Const vmPlatform_WINDOWS = 1
'=====================================
’ VmPowerOpMode
'=====================================
Const vmPowerOpMode_Hard = 1
Const vmPowerOpMode_Soft = 2
Const vmPowerOpMode_TrySoft = 3
'=====================================
’ VmProdInfoType
'=====================================
Const vmProdInfo_Build = 3
Const vmProdInfo_Platform = 2
Const vmProdInfo_Product = 1
Const vmProdInfo_Version_Major = 4
Const vmProdInfo_Version_Minor = 5
Const vmProdInfo_Version_Revision = 6
'=====================================
’ VmProduct
'=====================================
Const vmProduct_ESX = 3
Const vmProduct_GSX = 2
Const vmProduct_UNKNOWN = 4
Const vmProduct_WS = 1
'=====================================
’ VmTimeoutId
'=====================================
Const vmTimeoutId_Default = 1

’ VmCOM VBScript Sample Program 3
’ Copyright 1998 VMware, Inc. All rights reserved. – VMware Confidential

’ Permission is hereby granted, free of charge, to any person obtaining a
’ copy of the software in this file (the “Software”), to deal in the
’ Software without restriction, including without limitation the rights to
’ use, copy, modify, merge, publish, distribute, sublicense, and/or sell
’ copies of the Software, and to permit persons to whom the Software is
’ furnished to do so, subject to the following conditions:

’ The above copyright notice and this permission notice shall be included in
’ all copies or substantial portions of the Software.

’ The names “VMware” and “VMware, Inc.” must not be used to endorse or
’ promote products derived from the Software without the prior written
’ permission of VMware, Inc.

’ Products derived from the Software may not be called “VMware”, nor may
’ “VMware” appear in their name, without the prior written permission of
’ VMware, Inc.

’ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
’ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
’ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
’ VMWARE,INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
’ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
’ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

’ ------

’ This program is for educational purposes only.
’ It is not to be used in production environments.

’ Description:

’ This script gets a list of virtual machines registered on
’ the local server. It attempts to power-on each VM that
’ is not already running and has a line in the config file:

’ autostart=true


’ Instructions for Windows 2000 and Windows XP host:

’ - save the contents of this file to a file named ‘sample3.vbs’

’ - there should be an accompanying file named ‘sample3.wsf’
’ It is placed in the same directory as this file during
’ product installation. This file is responsible for setting
’ up the Windows Script Host environment and loading the
’ VmCOM type library, thereby enabling this script to
’ reference symbolic constants such as vmExecutionState_On

’ - in a command line window, type:
’ cscript //nologo sample3.wsf

Set connect_params = CreateObject(“VmCOM.VmConnectParams”)

’ By default, connects to the local server.
’ To connect to a remote server, uncomment these lines and set
’ the values appropriately.

’ connect_params.hostname = “”
’ connect_params.username = “”
’ connect_params.password = “”

’ And use this if your port number is different
’ connect_params.port = 902

Set vm_server = CreateObject(“VmCOM.VmServerCtl”)

’ Handle errors non-fatally from here on
On Error Resume Next


’ Try connecting to server a few times. It’s possible the VMware services
’ are still in the process of starting up. We’ll wait a maximum of
’ 12 * 10 = 120 seconds = 2 minutes

connected = false
for tries = 1 to 12

vm_server.Connect connect_params
if Err.number = 0 then
connected = true
exit for
end if
WScript.Echo "Could not connect to server: " & Err.Description
WScript.Echo “Retrying in 10 seconds …”
WScript.Sleep 10000
Err.clear
next

if not connected then
WScript.Echo “Failed to connect to server. Giving up.”
WScript.Quit
end if

’ Get a list of all VMs from the server.
Set vmlist = vm_server.RegisteredVmNames

for each config in vmlist
’ Connect to the VM
Set vm = CreateObject(“VmCOM.VmCtl”)
vm.Connect connect_params, config

if Err.Number <> 0 then
WScript.Echo "Could not connect to VM " & config & ": " & Err.Description
Err.Clear
else
’ Check that the VM should be started automatically
auto_start = vm.Config(“autostart”)
auto_start =“true”
if Err.Number <> 0 then
if Err.Number <> vmErr_NOPROPERTY then
WScript.Echo "Could not read autostart variable: " & Err.Number & ": " & Err.Description
else
WScript.Echo "This VM is not configured for autostart: " & config
end if
Err.Clear
else
if auto_start = “true” or auto_start = “TRUE” or auto_start = “poweron” then
’ Check that the VM is powered off

    power_state = vm.ExecutionState
    if Err.Number <> 0 then
       WScript.Echo "Error getting execution state: " & Err.Number & ": " & Err.Description
       Err.Clear
    Else
       
       if power_state = vmExecutionState_Off or power_state = vmExecutionState_On then
	  
	  vm.Suspend(vmPowerOpMode_Hard)
	  if Err.Number <> 0 then
	     WScript.Echo "Error powering on " & config & ": " & Err.Description
	     Err.Clear
              else 
                 ' Wait between starting up VMs to smooth out the load on the server
                 WScript.Sleep 5000
              end if
       end if
    end if
 end if
  end if

end if

Next

Do While vm.ExecutionState<> vmExecutionState_Suspended

Sleep(100)
Loop

Dim oShell

Set oShell = WScript.CreateObject (“WSCript.shell”)

oShell.run “test.ub4”


’ VmCOM VBScript Sample Program 3
’ Copyright 1998 VMware, Inc. All rights reserved. – VMware Confidential

’ Permission is hereby granted, free of charge, to any person obtaining a
’ copy of the software in this file (the “Software”), to deal in the
’ Software without restriction, including without limitation the rights to
’ use, copy, modify, merge, publish, distribute, sublicense, and/or sell
’ copies of the Software, and to permit persons to whom the Software is
’ furnished to do so, subject to the following conditions:

’ The above copyright notice and this permission notice shall be included in
’ all copies or substantial portions of the Software.

’ The names “VMware” and “VMware, Inc.” must not be used to endorse or
’ promote products derived from the Software without the prior written
’ permission of VMware, Inc.

’ Products derived from the Software may not be called “VMware”, nor may
’ “VMware” appear in their name, without the prior written permission of
’ VMware, Inc.

’ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
’ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
’ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
’ VMWARE,INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
’ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
’ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

’ ------

’ This program is for educational purposes only.
’ It is not to be used in production environments.

’ Description:

’ This script gets a list of virtual machines registered on
’ the local server. It attempts to power-on each VM that
’ is not already running and has a line in the config file:

’ autostart=true


’ Instructions for Windows 2000 and Windows XP host:

’ - save the contents of this file to a file named ‘sample3.vbs’

’ - there should be an accompanying file named ‘sample3.wsf’
’ It is placed in the same directory as this file during
’ product installation. This file is responsible for setting
’ up the Windows Script Host environment and loading the
’ VmCOM type library, thereby enabling this script to
’ reference symbolic constants such as vmExecutionState_On

’ - in a command line window, type:
’ cscript //nologo sample3.wsf

Set connect_params = CreateObject(“VmCOM.VmConnectParams”)

’ By default, connects to the local server.
’ To connect to a remote server, uncomment these lines and set
’ the values appropriately.

’ connect_params.hostname = “”
’ connect_params.username = “”
’ connect_params.password = “”

’ And use this if your port number is different
’ connect_params.port = 902

Set vm_server = CreateObject(“VmCOM.VmServerCtl”)

’ Handle errors non-fatally from here on
On Error Resume Next


’ Try connecting to server a few times. It’s possible the VMware services
’ are still in the process of starting up. We’ll wait a maximum of
’ 12 * 10 = 120 seconds = 2 minutes

connected = false
for tries = 1 to 12

vm_server.Connect connect_params
if Err.number = 0 then
connected = true
exit for
end if
WScript.Echo "Could not connect to server: " & Err.Description
WScript.Echo “Retrying in 10 seconds …”
WScript.Sleep 10000
Err.clear
next

if not connected then
WScript.Echo “Failed to connect to server. Giving up.”
WScript.Quit
end if

’ Get a list of all VMs from the server.
Set vmlist = vm_server.RegisteredVmNames

for each config in vmlist
’ Connect to the VM
Set vm = CreateObject(“VmCOM.VmCtl”)
vm.Connect connect_params, config

if Err.Number <> 0 then
WScript.Echo "Could not connect to VM " & config & ": " & Err.Description
Err.Clear
else
’ Check that the VM should be started automatically
auto_start = vm.Config(“autostart”)
auto_start =“true”
if Err.Number <> 0 then
if Err.Number <> vmErr_NOPROPERTY then
WScript.Echo "Could not read autostart variable: " & Err.Number & ": " & Err.Description
else
WScript.Echo "This VM is not configured for autostart: " & config
end if
Err.Clear
else
if auto_start = “true” or auto_start = “TRUE” or auto_start = “poweron” then
’ Check that the VM is powered off

    power_state = vm.ExecutionState
    if Err.Number <> 0 then
       WScript.Echo "Error getting execution state: " & Err.Number & ": " & Err.Description
       Err.Clear
    Else
       
       if power_state = vmExecutionState_Off or power_state = vmExecutionState_Suspended then
	  
	  vm.Start(vmPowerOpMode_Soft)
	  if Err.Number <> 0 then
	     WScript.Echo "Error powering on " & config & ": " & Err.Description
	     Err.Clear
              else 
                 ' Wait between starting up VMs to smooth out the load on the server
                 WScript.Sleep 5000
              end if
       end if
    end if
 end if
  end if

end if

next
Edité le 18/07/2007 à 09:39

c’est bon, j’ai presque fini mon script, il ne me reste plus qu’a faire en sorte que telle ou telle machine virtuelle se mette en pause^^

je vous mets le script presque fini

[CODE]’=====================================
’ VmErr
'=====================================
Const vmErr_ALREADYCONNECTED = -2147220980
Const vmErr_BADRESPONSE = -2147220978
Const vmErr_BADSTATE = -2147220984
Const vmErr_BADVERSION = -2147220986
Const vmErr_DISCONNECT = -2147220979
Const vmErr_GARBAGE = -2147220977
Const vmErr_INSUFFICIENT_RESOURCES = -2147220970
Const vmErr_INVALIDARGS = -2147220989
Const vmErr_INVALIDVM = -2147220974
Const vmErr_NEEDINPUT = -2147220976
Const vmErr_NETFAIL = -2147220990
Const vmErr_NOACCESS = -2147220988
Const vmErr_NOEXECVMAUTHD = -2147220983
Const vmErr_NOMEM = -2147220991
Const vmErr_NOPROPERTY = -2147220982
Const vmErr_NOSUCHVM = -2147220981
Const vmErr_NOTCONNECTED = -2147220987
Const vmErr_NOTSUPPORTED = -2147220975
Const vmErr_PROXYFAIL = -2147220969
Const vmErr_TIMEOUT = -2147220985
Const vmErr_UNSPECIFIED = -2147219993
Const vmErr_VMBUSY = -2147220971
Const vmErr_VMEXISTS = -2147220972
Const vmErr_VMINITFAILED = -2147220973
'=====================================
’ VmExecutionState
'=====================================
Const vmExecutionState_Off = 2
Const vmExecutionState_On = 1
Const vmExecutionState_Stuck = 4
Const vmExecutionState_Suspended = 3
Const vmExecutionState_Unknown = 5
'=====================================
’ VmPlatform
'=====================================
Const vmPlatform_LINUX = 2
Const vmPlatform_UNKNOWN = 4
Const vmPlatform_VMNIX = 3
Const vmPlatform_WINDOWS = 1
'=====================================
’ VmPowerOpMode
'=====================================
Const vmPowerOpMode_Hard = 1
Const vmPowerOpMode_Soft = 2
Const vmPowerOpMode_TrySoft = 3
'=====================================
’ VmProdInfoType
'=====================================
Const vmProdInfo_Build = 3
Const vmProdInfo_Platform = 2
Const vmProdInfo_Product = 1
Const vmProdInfo_Version_Major = 4
Const vmProdInfo_Version_Minor = 5
Const vmProdInfo_Version_Revision = 6
'=====================================
’ VmProduct
'=====================================
Const vmProduct_ESX = 3
Const vmProduct_GSX = 2
Const vmProduct_UNKNOWN = 4
Const vmProduct_WS = 1
'=====================================
’ VmTimeoutId
'=====================================
Const vmTimeoutId_Default = 1

Set connect_params = CreateObject(“VmCOM.VmConnectParams”)

’ By default, connects to the local server.
’ To connect to a remote server, uncomment these lines and set
’ the values appropriately.

’ connect_params.hostname = “”
’ connect_params.username = “”
’ connect_params.password = “”

’ And use this if your port number is different
’ connect_params.port = 902

Set vm_server = CreateObject(“VmCOM.VmServerCtl”)

’ Handle errors non-fatally from here on
On Error Resume Next


’ Try connecting to server a few times. It’s possible the VMware services
’ are still in the process of starting up. We’ll wait a maximum of
’ 12 * 10 = 120 seconds = 2 minutes

connected = false
for tries = 1 to 12

vm_server.Connect connect_params
if Err.number = 0 then
connected = true
exit for
end if
WScript.Echo "Could not connect to server: " & Err.Description
WScript.Echo “Retrying in 10 seconds …”
WScript.Sleep 10000
Err.clear
next

if not connected then
WScript.Echo “Failed to connect to server. Giving up.”
WScript.Quit
end if

’ Get a list of all VMs from the server.
Set vmlist = vm_server.RegisteredVmNames

for each config in vmlist
’ Connect to the VM
Set vm = CreateObject(“VmCOM.VmCtl”)
vm.Connect connect_params, config

if Err.Number <> 0 then
WScript.Echo "Could not connect to VM " & config & ": " & Err.Description
Err.Clear
else
’ Check that the VM should be started automatically
auto_start = vm.Config(“autostart”) 'On peut essayer d’enlever les autostart =true
auto_start =“true”
if Err.Number <> 0 then
if Err.Number <> vmErr_NOPROPERTY then
WScript.Echo "Could not read autostart variable: " & Err.Number & ": " & Err.Description
else
WScript.Echo "This VM is not configured for autostart: " & config
end if
Err.Clear
else
if auto_start = “true” or auto_start = “TRUE” or auto_start = “poweron” then
’ Check that the VM is powered off

    power_state = vm.ExecutionState
    if Err.Number <> 0 then
       WScript.Echo "Error getting execution state: " & Err.Number & ": " & Err.Description
       Err.Clear
    Else
       
       if power_state = vmExecutionState_Off or power_state = vmExecutionState_On then
	  
	  vm.Suspend(vmPowerOpMode_Hard)
	  if Err.Number <> 0 then
	     WScript.Echo "Error powering on " & config & ": " & Err.Description
	     Err.Clear
              else 
                 ' Wait between starting up VMs to smooth out the load on the server
                 WScript.Sleep 5000
              end if
       end if
    end if
 end if
  end if

end if

Next

Do While vm.ExecutionState<> vmExecutionState_Suspended 'on demande au code de vérifier si la machine virtuelle est bien en pause

Sleep(1000) 'on mets un sleep de 1 sec pour faire en sorte que la boucle ne se fasse pas trop souvent
Loop

Dim oShell

Set oShell = WScript.CreateObject (“WSCript.shell”)

oShell.run “test.ub4” 'on ouvre le fichier de backup qui lui est déja configuré
Sleep(10000) 'on mets un sleep de 10 sec pour faire en sorte que le programme de sauvegarde est le temps de se lancé

Dim objWMIService, objProcess, colProcess
Dim strComputer, strList
Dim test

copie_active=1
strComputer = “.”
Do While (copie_active=1)
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\” _
& strComputer & “\root\cimv2”)

Set colProcess = objWMIService.ExecQuery _
(“Select * from Win32_Process”)
copie_active=0
For Each objProcess in colProcess
strList = strList & vbCr & objProcess.Name

if objProcess.Name="UbKernel.exe" Then 'le processus qui doit être fermé pour que le script continu est UbKernel.exe 
copie_active=1
 
End If

Next

loop 'la boucle recommence tant que UbKernel.exe n’est pas fermé

Set connect_params = CreateObject(“VmCOM.VmConnectParams”)

’ By default, connects to the local server.
’ To connect to a remote server, uncomment these lines and set
’ the values appropriately.

’ connect_params.hostname = “”
’ connect_params.username = “”
’ connect_params.password = “”

’ And use this if your port number is different
’ connect_params.port = 902

Set vm_server = CreateObject(“VmCOM.VmServerCtl”)

’ Handle errors non-fatally from here on
On Error Resume Next


’ Try connecting to server a few times. It’s possible the VMware services
’ are still in the process of starting up. We’ll wait a maximum of
’ 12 * 10 = 120 seconds = 2 minutes

connected = false
for tries = 1 to 12

vm_server.Connect connect_params
if Err.number = 0 then
connected = true
exit for
end if
WScript.Echo "Could not connect to server: " & Err.Description
WScript.Echo “Retrying in 10 seconds …”
WScript.Sleep 10000
Err.clear
next

if not connected then
WScript.Echo “Failed to connect to server. Giving up.”
WScript.Quit
end if

’ Get a list of all VMs from the server.
Set vmlist = vm_server.RegisteredVmNames

for each config in vmlist
’ Connect to the VM
Set vm = CreateObject(“VmCOM.VmCtl”)
vm.Connect connect_params, config

if Err.Number <> 0 then
WScript.Echo "Could not connect to VM " & config & ": " & Err.Description
Err.Clear
else
’ Check that the VM should be started automatically
auto_start = vm.Config(“autostart”) 'On peut essayer d’enlever les autostart =true
auto_start =“true”
if Err.Number <> 0 then
if Err.Number <> vmErr_NOPROPERTY then
WScript.Echo "Could not read autostart variable: " & Err.Number & ": " & Err.Description
else
WScript.Echo "This VM is not configured for autostart: " & config
end if
Err.Clear
else
if auto_start = “true” or auto_start = “TRUE” or auto_start = “poweron” then
’ Check that the VM is powered off

    power_state = vm.ExecutionState
    if Err.Number <> 0 then
       WScript.Echo "Error getting execution state: " & Err.Number & ": " & Err.Description
       Err.Clear
    Else
       
       if power_state = vmExecutionState_Off or power_state = vmExecutionState_Suspended then
	  
	  vm.Start(vmPowerOpMode_Soft)
	  if Err.Number <> 0 then
	     WScript.Echo "Error powering on " & config & ": " & Err.Description
	     Err.Clear
              else 
                 ' Wait between starting up VMs to smooth out the load on the server
                 WScript.Sleep 5000
              end if
       end if
    end if
 end if
  end if

end if

next

[/CODE]

Je pense que je vais déplacer ce sujet vers le forum programmation, plus approprié que celui de Windows ! :smiley:

Windows [:fleche] Programmation
:jap:

ça m’énerve, je n’arrive pas a faire en sorte que seulement une machine se lance :’(
grrr.
Help, I need somebody’s help
bon, voila déja pour savoir le nom de la machine virtuelle qui tourne, enfin je crois^^
str = vm.GuestInfo(“name”)
sa se place ou?
Edité le 19/07/2007 à 11:39

Youhou, sa y’est, j’ai fini mon script, si jamais quelqun devait un jour à avoir à utiliser un truc comme ça, je mets le code

'=====================================
' VmErr
'=====================================
Const vmErr_ALREADYCONNECTED = -2147220980
Const vmErr_BADRESPONSE = -2147220978
Const vmErr_BADSTATE = -2147220984
Const vmErr_BADVERSION = -2147220986
Const vmErr_DISCONNECT = -2147220979
Const vmErr_GARBAGE = -2147220977
Const vmErr_INSUFFICIENT_RESOURCES = -2147220970
Const vmErr_INVALIDARGS = -2147220989
Const vmErr_INVALIDVM = -2147220974
Const vmErr_NEEDINPUT = -2147220976
Const vmErr_NETFAIL = -2147220990
Const vmErr_NOACCESS = -2147220988
Const vmErr_NOEXECVMAUTHD = -2147220983
Const vmErr_NOMEM = -2147220991
Const vmErr_NOPROPERTY = -2147220982
Const vmErr_NOSUCHVM = -2147220981
Const vmErr_NOTCONNECTED = -2147220987
Const vmErr_NOTSUPPORTED = -2147220975
Const vmErr_PROXYFAIL = -2147220969
Const vmErr_TIMEOUT = -2147220985
Const vmErr_UNSPECIFIED = -2147219993
Const vmErr_VMBUSY = -2147220971
Const vmErr_VMEXISTS = -2147220972
Const vmErr_VMINITFAILED = -2147220973
'=====================================
' VmExecutionState
'=====================================
Const vmExecutionState_Off = 2
Const vmExecutionState_On = 1
Const vmExecutionState_Stuck = 4
Const vmExecutionState_Suspended = 3
Const vmExecutionState_Unknown = 5
'=====================================
' VmPlatform
'=====================================
Const vmPlatform_LINUX = 2
Const vmPlatform_UNKNOWN = 4
Const vmPlatform_VMNIX = 3
Const vmPlatform_WINDOWS = 1
'=====================================
' VmPowerOpMode
'=====================================
Const vmPowerOpMode_Hard = 1
Const vmPowerOpMode_Soft = 2
Const vmPowerOpMode_TrySoft = 3
'=====================================
' VmProdInfoType
'=====================================
Const vmProdInfo_Build = 3
Const vmProdInfo_Platform = 2
Const vmProdInfo_Product = 1
Const vmProdInfo_Version_Major = 4
Const vmProdInfo_Version_Minor = 5
Const vmProdInfo_Version_Revision = 6
'=====================================
' VmProduct
'=====================================
Const vmProduct_ESX = 3
Const vmProduct_GSX = 2
Const vmProduct_UNKNOWN = 4
Const vmProduct_WS = 1
'=====================================
' VmTimeoutId
'=====================================
Const vmTimeoutId_Default = 1

Set connect_params = CreateObject("VmCOM.VmConnectParams")

' By default, connects to the local server.
' To connect to a remote server, uncomment these lines and set
' the values appropriately.
'
' connect_params.hostname = "<host>"
' connect_params.username = "<user>"
' connect_params.password = "<password>"
'
' And use this if your port number is different
' connect_params.port = 902

Set vm_server = CreateObject("VmCOM.VmServerCtl")

' Handle errors non-fatally from here on
On Error Resume Next

'
' Try connecting to server a few times. It's possible the VMware services
' are still in the process of starting up. We'll wait a maximum of
' 12 * 10 = 120 seconds = 2 minutes
'
connected = false
for tries = 1 to 12
  
   vm_server.Connect connect_params
   if Err.number = 0 then
      connected = true
      exit for
   end if
   WScript.Echo "Could not connect to server: " & Err.Description
   WScript.Echo "Retrying in 10 seconds ..."
   WScript.Sleep 10000
   Err.clear
next

if not connected then 
   WScript.Echo "Failed to connect to server. Giving up."
   WScript.Quit
end if

' Get a list of all VMs from the server.
Set vmlist = vm_server.RegisteredVmNames

for each config in vmlist
   ' Connect to the VM
   Set vm = CreateObject("VmCOM.VmCtl")
   vm.Connect connect_params, config
  
   if Err.Number <> 0 then 
      WScript.Echo "Could not connect to VM " & config & ": " & Err.Description
      Err.Clear
   Else
   If InStr(vm.Config("displayName"),"Satu")<>0 Then
      ' Check that the VM should be started automatically
      auto_start = vm.Config("autostart")           'On peut essayer d'enlever les autostart =true
      auto_start ="true"
      if Err.Number <> 0 then
	 if Err.Number <> vmErr_NOPROPERTY then 
	    WScript.Echo "Could not read autostart variable: " & Err.Number & ": " & Err.Description
         else 
	    WScript.Echo "This VM is not configured for autostart: " & config 
	 end if
         Err.Clear
      else
	 if auto_start = "true" or auto_start = "TRUE" or auto_start = "poweron" then
	    ' Check that the VM is powered off

	    power_state = vm.ExecutionState
	    if Err.Number <> 0 then
	       WScript.Echo "Error getting execution state: " & Err.Number & ": " & Err.Description
	       Err.Clear
	    Else
	       
	       if power_state = vmExecutionState_Off or power_state = vmExecutionState_On then
		  
		  vm.Suspend(vmPowerOpMode_Hard)
		  if Err.Number <> 0 then
		     WScript.Echo "Error powering on " & config & ": " & Err.Description
		     Err.Clear
                  else 
                     ' Wait between starting up VMs to smooth out the load on the server
                     WScript.Sleep 5000
                  end if
	       end if
	    end if
	 end if
      end if
   end If
   End If

Next

Do While vm.ExecutionState<> vmExecutionState_Suspended 'on demande au code de vérifier si la machine virtuelle est bien en pause


Sleep(1000) 'on mets un sleep de 1 sec pour faire en sorte que la boucle ne se fasse pas trop souvent
Loop




Dim oShell 

Set oShell = WScript.CreateObject ("WSCript.shell") 

oShell.run "test.ub4" 'on ouvre le fichier de backup qui lui est déja configuré
Sleep(10000) 'on mets un sleep de 10 sec pour faire en sorte que le programme de sauvegarde est le temps de se lancé

Dim objWMIService, objProcess, colProcess
Dim strComputer, strList
Dim test

copie_active=1
strComputer = "."
Do While (copie_active=1)
  Set objWMIService = GetObject("winmgmts:" _
  & "{impersonationLevel=impersonate}!\\" _
  & strComputer & "\root\cimv2")
  
  Set colProcess = objWMIService.ExecQuery _
  ("Select * from Win32_Process")
  copie_active=0
  For Each objProcess in colProcess
    strList = strList & vbCr & objProcess.Name
    
    
    if objProcess.Name="UbKernel.exe" Then 'le processus qui doit être fermé pour que le script continu est UbKernel.exe 
    copie_active=1
     
    End If
  
  Next
  
loop 'la boucle recommence tant que UbKernel.exe n'est pas fermé

 Set connect_params = CreateObject("VmCOM.VmConnectParams")

' By default, connects to the local server.
' To connect to a remote server, uncomment these lines and set
' the values appropriately.
'
' connect_params.hostname = "<host>"
' connect_params.username = "<user>"
' connect_params.password = "<password>"
'
' And use this if your port number is different
' connect_params.port = 902

Set vm_server = CreateObject("VmCOM.VmServerCtl")

' Handle errors non-fatally from here on
On Error Resume Next

'
' Try connecting to server a few times. It's possible the VMware services
' are still in the process of starting up. We'll wait a maximum of
' 12 * 10 = 120 seconds = 2 minutes
'
connected = false
for tries = 1 to 12
  
   vm_server.Connect connect_params
   if Err.number = 0 then
      connected = true
      exit for
   end if
   WScript.Echo "Could not connect to server: " & Err.Description
   WScript.Echo "Retrying in 10 seconds ..."
   WScript.Sleep 10000
   Err.clear
next

if not connected then 
   WScript.Echo "Failed to connect to server. Giving up."
   WScript.Quit
end if

' Get a list of all VMs from the server.
Set vmlist = vm_server.RegisteredVmNames

for each config in vmlist
   ' Connect to the VM
   Set vm = CreateObject("VmCOM.VmCtl")
   vm.Connect connect_params, config
  
   if Err.Number <> 0 then 
      WScript.Echo "Could not connect to VM " & config & ": " & Err.Description
      Err.Clear
   Else
   If InStr(vm.Config("displayName"),"Satu")<>0 Then
      ' Check that the VM should be started automatically
      auto_start = vm.Config("autostart")               'On peut essayer d'enlever les autostart =true
      auto_start ="true"
      if Err.Number <> 0 then
	 if Err.Number <> vmErr_NOPROPERTY then 
	    WScript.Echo "Could not read autostart variable: " & Err.Number & ": " & Err.Description
         else 
	    WScript.Echo "This VM is not configured for autostart: " & config 
	 end if
         Err.Clear
      else
	 if auto_start = "true" or auto_start = "TRUE" or auto_start = "poweron" then
	    ' Check that the VM is powered off

	    power_state = vm.ExecutionState
	    if Err.Number <> 0 then
	       WScript.Echo "Error getting execution state: " & Err.Number & ": " & Err.Description
	       Err.Clear
	    Else
	       
	       if power_state = vmExecutionState_Off or power_state = vmExecutionState_Suspended then
		  
		  vm.Start(vmPowerOpMode_Soft)
		  if Err.Number <> 0 then
		     WScript.Echo "Error powering on " & config & ": " & Err.Description
		     Err.Clear
                  else 
                     ' Wait between starting up VMs to smooth out the load on the server
                     WScript.Sleep 5000
                  end if
	       end if
	    end if
	 end if
      end if
   end If
   End if

next