Clear-Host param ( [string]$VIserver, [string]$VILogin, [string]$VIMotdepasse, [string]$VMName, [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 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" $UneLigne = $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 $UneVM.PowerState if ($UneVM.PowerState -eq "PoweredOn") { Write-Host "Shutdown guest de $UneLigne" Shutdown-VMGuest -VM $UneVM -Confirm:$false } while ($UneVM.PowerState -eq "PoweredOn") { sleep 4 $UneVM = Get-VM -Name $UneLigne #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" } write-host "$VMName est maintenant eteint" } 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