Effacer tous les snapshots d'une VM
Supprimer tous les snapshots d'une VM VMWare
Le VbScript qui permet d'appeler le Powershell
Option Explicit
Dim CheminScriptActuel, ScriptFileName, Position
Dim WSHShell, Commande
Dim NomVM, varLoginAuthentVM, varMotDePasseAuthentVM, varNomSnapShot
Dim objFSO, objTextFile, CheminFichierLog
'Déclaration des constantes
Const ForReading = 1
Const ForWritting = 2
Const ForAppending = 8
CheminScriptActuel = Left(wscript.scriptfullname,Len(wscript.scriptfullname)-Len(wscript.scriptname)-1)
'Wscript.Echo "Votre script '" & wscript.scriptname & "' est dans le répertoire '" & CheminScriptActuel & "'."
ScriptFileName = wscript.scriptname
Position = InstrRev(ScriptFileName,".")
if (Position > 0) Then ScriptFileName = Left(ScriptFileName, Position - 1)
CheminFichierLog = CheminScriptActuel & "\" & ScriptFileName & "_log.txt"
NomVM = Trim(InputBox("Entrez le nom de la VM","Nom de la VM","Nom machine"))
If Len(NomVM) > 0 Then
Set WSHShell = CreateObject("WScript.Shell")
Commande = "cmd /k Powershell -file """ & CheminScriptActuel & "\" & ScriptFileName & ".ps1"" -v """ & NomVM & """"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(CheminFichierLog, ForWritting, True)
objTextFile.WriteLine(Commande) 'On ecrit la date et l'heure dans le fichier
objTextFile.Close 'Fermeture du fichier
Set objTextFile = Nothing
Set objFSO = Nothing
WSHShell.Run Commande
Set WSHShell = Nothing
End If
Lien vers le fichier : cliquez ici
Le script Powershell qui réaliser l'opération
Clear-Host
$VIserver = "NomvCenter"
$VILogin = "domaine\loginvcenter"
$VIMotdepasse = "Motdepasse"
#Definition des variables globales
$global:varNomVM = ""
function main()
{
asnp vmware* #Import modules VMWares
$ErrorActionPreference = "Continue"
if ($verbose) {$VerbosePreference = "Continue"}
if ($debug) {$DebugPreference = "Continue"}
$DebugPreference = "SilentlyContinue"
CheckVIToolKit
Connect-VIServer -Server $VIserver -User $VILogin -Password $VIMotdepasse
Write-Host "Traitement de $varNomVM"
$UneVM = Get-VM -Name $varNomVM
Write-Host $UneVM.PowerState #Juste pour afficher si la VM est allumée ou non
$Error.Clear()
Write-Host "$varNomVM : Effacement des Snapshots"
Get-Snapshot $varNomVM | Remove-Snapshot -confirm:$false
}
function CheckVIToolKit()
{
## Before we do anything we must check to see if the user has the VI toolkit installed.
## If user does not then we prompt the user and exit.
$Error.Clear()
Get-PSSnapin vmware*
if($Error.Count -ne 0)
{
Clear-Host
Write-Host "`n`n`t`t ERROR - To run this script, the VI Toolkit must be installed and registered with Powershell. If the VI Tollkit is installed," -foregroundcolor red -backgroundColor yellow
Write-Host "`t`t go to the Settings menu in Powershell Plus and click on Manage Snapins." -foregroundcolor red -backgroundColor yellow
# Read-Host "`n`n`t Press <Enter> to continue."
break
}
}## EOF: CheckVIToolKit()
Function ParseCommand($oArgs){
#Parses the command line and fills the script variables
#with the appropriate values.
# $test = "-n,test"
# $oArgs = $test.split(",")
$ArgCount = 0
if (!$oArgs.length -gt 0){
write-host "No arguments specified."
}
While ($ArgCount -lt $oArgs.length){
switch ($oArgs[$ArgCount].ToLower()){
"-v"{
$global:varNomVM = $oArgs[($ArgCount+1)]
$ArgCount = ($ArgCount + 2)
write-host "-v : $varNomVM"
break
}
default{
write-host "Invalid command."
# Help
exit
}
}
}
}
#Parse commands
ParseCommand($args)
## Run Main
main
Lien vers le fichier : cliquez ici
Publicité
Téléchargement(s)
| Nom | Site Web d origine | Description |
|---|---|---|
| VMware-PowerCLI-5.1.0-793... | https://my.vmware.com/fr/web/v... | vSphere PowerCli |
Article(s) précédent(s)
Article(s) en relation(s)