# VMware.PowerCLI - Dialoguer avec Vsphere via Powershell

<span style="text-decoration: underline;">**Lancer un conteneur avec Powershell :** </span>

```bash
docker run -it mcr.microsoft.com/powershell
```

<span style="text-decoration: underline;">**Installer le module VMware.PowerCLI :** </span>

```Powershell
Install-Module -Name VMware.PowerCLI -Confirm:$false
```

<span style="text-decoration: underline;">**Accepter les certificats SSL invalides :** </span>

```Powershell
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore  -Confirm:$false
```

<span style="text-decoration: underline;">**Etablir une conexion au VCENTER :** </span>

```Powershell
Connect-VIServer -Server "vsphere.mon-entreprise.fr" -User "utilisateur" -Password "mot-de-passe"
```

<span style="text-decoration: underline;">**Récupérer la liste des VM :** </span>

```Powershell
$vm = Get-VM
```

<span style="text-decoration: underline;">**Récupérer une VM précise :** </span>

```Powershell
$vm = Get-VM -Name "srv-web-01"
```

<span style="text-decoration: underline;">**Eteindre la VM :** </span>

```Powershell
Stop-VM -VM "$vm" -Confirm:$false
```

<span style="text-decoration: underline;">**Démarrer la VM :**</span>

```Powershell
Start-VM -VM "$vm" -Confirm:$false
```

<span style="text-decoration: underline;">**Créer une snapshot :**</span>

```Powershell
New-Snapshot -VM "$vm" -Name "Snapshot_1" -Description "Avant MAJ OS"
```

<span style="text-decoration: underline;">**Récupérer les snapshots d'une VM :** </span>

```Powershell
$snapshots = Get-Snapshot -VM $vm
```

<span style="text-decoration: underline;">**Récupérer une snapshots d'une VM :** </span>

```Powershell
$snapshot = Get-Snapshot -VM "$vm" -Name "ma-snapshot"
```

<span style="text-decoration: underline;">**Appliquer une snapshot d'une vm :**</span>

```Powershell
Set-VM -VM 'ma-vm' -Snapshot (Get-Snapshot -VM 'ma-vm' -Name 'ma-snapshot') -Confirm:$false
```

<span style="text-decoration: underline;">**Déconnecter le disque CD d'une vm :**</span>

```Powershell
$cd = Get-CDDrive -VM $vm
Set-CDDrive -CD $cd -Connected:$false
```

<span style="text-decoration: underline;">**Blocs de code intéréssant :**</span>

**Créer une snapshot sur toutes les vm :**

```Powershell
foreach ($vm in Get-VM)
{
New-Snapshot -VM "$vm" -Name "$vm-before" -Description "Before update"
}
```

**Stop / Start toutes les vm :**

```Powershell
foreach ($vm in Get-VM)
{
    Stop-VM -VM "$vm" -Confirm:$false
}

foreach ($vm in Get-VM)
{
    Start-VM -VM "$vm" -Confirm:$false
}
```

**Appliquer une snapshot à toutes les vm :**

```Powershell
foreach ($vm in Get-VM)
{
    Set-VM -VM "$vm" -Snapshot (Get-Snapshot -VM "$vm" -Name "$vm-fresh") -Confirm:$false
}
```

**Détacher tous les CD des lecteurs CD virtuels de toutes les vm :**

```Powershell
foreach ($vm in Get-VM)
{
    $cd = Get-CDDrive -VM $vm 
    Set-CDDrive -CD $cd -Connected:$false
}
```

**Afficher toutes les snapshots de toutes les vm :**

```Powershell
foreach ($vm in Get-VM)
{
    Write-Host "[$vm] :"
    $snapshots = Get-Snapshot -VM "$vm" 
    foreach ($snapshot in $snapshots)
    {
        Write-Host "$snapshot"
    }
}
```