Clear-Host param ( [string]$VIserver, [string]$VILogin, [string]$VIMotdepasse, [string]$VMName, [string]$varErrorCount, [switch]$verbose, [switch]$debug ) Clear-Host #Definition de 2 variables globales $global:MyVar01 = "" $global:MyVar02 = "" $VIserver = "Nom vCenter" $VILogin = "domaine\login" #Domaine\login avec droits de connexion au vCenter $VIMotdepasse = "YourPassword" #Mot de passe pour connexion au vCenter $VMLogin = "LoginCompteLocalDeLaVM" $VMMotdepasse = "MotDePasseLocalSurVM" 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 $VMName" $UneVM = Get-VM -Name $VMName Write-Host $UneVM.PowerState #Juste pour afficher si la VM est allumée ou non $Error.Clear() Write-Host "Démarrage de la VM : $VMName" Start-VM -VM $UneVM while ($UneVM.PowerState -ne "PoweredOn") { Write-Host "La Vm $VMName n'est pas encore allumée. Attente de 4 secondes" sleep 4 $UneVM = Get-VM -Name $VMName #On est obligé de refaire un Get-VM sur la machine pour avoir un statut de PowerState à jour. C'est très con. Write-Host $UneVM.PowerState } #Attente tant que l'on n'arrive pas à contacter les VMTools write-host "Attente de la disponibilité des VMTools" do { $toolsStatus = (Get-VM $UneVM | Get-View).Guest.ToolsStatus write-host $toolsStatus sleep 3 } until ( ($toolsStatus -eq ‘toolsOk’) -or ($toolsStatus -eq ‘toolsOld’)) Write-Host "Tools de $VMName Ok" #Attente de la possibilité du lancement d'une commande auprès des VMTools write-host "Test de lancement d'une commande via les VMTools" write-host "Ce test echoue a chaque fois si le login et mdp est faux donc attention" do { sleep 10 $Error.Clear() $Mycommand = "dir.exe" Invoke-VMScript -ScriptText $Mycommand -VM $UneVM -ScriptType Bat -GuestUser $VMLogin -GuestPassword $VMMotdepasse $varErrorCount = [string]$($Error.Count) write-host "Error count : $($Error.Count)" write-host "varErrorCount : $varErrorCount" } while ($varErrorCount -ne "0") write-host "$VMName est maintenant allumee et les VMTools peuvent executer une commande" } 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() 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:MyVar01 = $oArgs[($ArgCount+1)] $ArgCount = ($ArgCount + 2) write-host "-v : $MyVar01" } default{ write-host "Invalid command." # Help exit } } } } #Parse commands ParseCommand($args) $VMName = $MyVar01 write-host "VMName : $VMName" ## Run Main main