Clear-Host param ( [string]$VIserver, [string]$VILogin, [string]$VIMotdepasse, [string]$VMLogin, [string]$VMMotdepasse, [string]$EmplacementFichier, [switch]$verbose, [switch]$debug ) Clear-Host $EmplacementFichier = [string] $VIserver = "Nom vCenter" $VILogin = "domaine\login" #Domaine\login avec droits de connexion au vCenter $VIMotdepasse = "YourPassword" #Mot de passe pour connexion au vCenter $VMLogin = "Login" #Login local pour connexion sur la VM $VMMotdepasse = "Password" #Mot de passe du compte local pour connexion sur la VM $EmplacementFichier = "E:\Chemin_Liste_VM.txt" #Renseignez ce fichier à l'emplacement indiqué avec le nom des VMs à traiter. Mettre une VM par ligne $DatastoreName = "[Nom du DataStore]" #Nom du datastore. Les crochets sont en plus du Datastore. Si votre Datastore se nomme Toto alors cela donnera $DatastoreName = "[Toto]" $isopath = "NomDossier1/NomDossier1/NomDuFichier.iso" #Chemin de l'ISO dans le Datastore $FullIsoPath = "$DatastoreName $isopath" #Création de la variable contenant et le Datastore et le chemin de l'ISO. Ne pas toucher. function Get-ScriptDirectory { $Invocation = (Get-Variable MyInvocation -Scope 1).Value $ScriptFolderPath = Split-Path $Invocation.MyCommand.Path $ScriptFolderPath = $PSScriptRoot return $ScriptFolderPath } function main() { $ErrorActionPreference = "Continue" if ($verbose) {$VerbosePreference = "Continue"} if ($debug) {$DebugPreference = "Continue"} $DebugPreference = "SilentlyContinue" CheckVIToolKit #$EmplacementFichier = Get-ScriptDirectory #$EmplacementFichier = "$EmplacementFichier\$NomFichier" $MonFichier = get-content $EmplacementFichier #Set-PowerCLIConfiguration -InvalidCertificateAction ignore -ProxyPolicy NoProxy -Confirm:$False Connect-VIServer -Server $VIserver -User $VILogin -Password $VIMotdepasse foreach ($UneLigne in $MonFichier){ Write-Host "Traitement de $UneLigne" $UneVM = Get-VM -Name $UneLigne Write-Host $UneVM.PowerState #Juste pour afficher si la VM est allumée ou non $Error.Clear() #Monter une ISO sur le CD/DVD $MyCd = get-cddrive -VM $UneVM | set-cddrive -IsoPath $FullIsoPath -Connected $true -Confirm:$false #En bonus, exécution d'une commande sur la VM $DriveLetter = "D:" $ScriptText = "$DriveLetter\setup64.exe /S /v `"/qn REBOOT=R ADDLOCAL=ALL REMOVE=Hgfs,WYSE`"" Invoke-VMScript -VM $UneVM -ScriptText $ScriptText -ScriptType bat -GuestUser $VMLogin -GuestPassword $VMMotdepasse #Démonter l'ISO Set-CDDrive -CD $MyCd -NoMedia -Confirm:$false #Disconnect the ISO #Si erreur, affichage de cette dernière if($Error.Count -ne 0) { Write-Host "ERROR : $error[0]" } } } 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 to continue." break } }## EOF: CheckVIToolKit() ## Run Main main