#Autentification et connection au vCenter

$credentialvCenter = Get-Credential administrator@vsphere.local
$vc = Connect-VIServer -Server vcsa.vmware.lab -Credential $credentialvCenter

Import-Module .\Module\vSphere.psm1

#################Fonctions PowerShell######################
function New-FolderFromPath
{
param(
  [Parameter(Mandatory=$true)]
  [String] $Path 
)
  $splittedPaths = $Path.Split('\')

  $location = Get-Folder $splittedPaths[0] -ErrorAction Stop

  $splittedPaths[1..$splittedPaths.Length] | % {
    $nouveauDossier = Get-Folder -Name $_ -Location $location `
            -ErrorAction Ignore
    if($nouveauDossier -eq $null) { 
        Write-Verbose "Création du répertoire $_"
        $nouveauDossier = New-Folder -Name $_ -Location $location
    }else{
        Write-Warning "Le dossier $_ existe déjà"
    }
    $location = $nouveauDossier
  }
  
  return $nouveauDossier
}

function Get-VMFromFolder
{
param(
  [Parameter(Mandatory=$true)]
  [String] $FolderPath
)  
  (Get-View -VIObject (Get-FolderFromPath $FolderPath)).ChildEntity | `
  ? {$_.Type -eq 'VirtualMachine'} | `
  % {Get-VM -Id "$($_.Type)-$($_.Value)"}
}
function Get-FolderFromPath
{
param(
    [String] $Path
)
    $morceaux = $Path.Split('\')
    $racine = Get-View -VIObject (Get-Folder -Name $morceaux[0])
    if (-not $?){return}
 
    $morceaux[1..$morceaux.Count] | % {
        $morceau = $_
        $child = $racine.ChildEntity | ? {$_.Type -eq 'Folder'} | `
                ? { (Get-Folder -id ("{0}-{1}" `
                -f ($_.Type, $_.Value))).Name -eq $morceau}
        if ($child -eq $null) { throw "Dossier '$morceau' non trouvé"}
        $racine = Get-View -VIObject `
                (Get-Folder -Id ("{0}-{1}" -f ($child.Type, $child.Value)))
        if (-not $?){return}
    }

    return (Get-Folder -Id ("{0}-{1}" `
              -f ($racine.MoRef.Type, $racine.MoRef.Value)))
}


#################Fin des Fonctions#########################

Get-Content .\Arborescence.txt| ForEach-Object { `
New-FolderFromPath -Path $_}

pause

#on deplace les VM dans les Dossiers
Write-Host "On déplace les VM des Clients dans les Dossiers " -ForegroundColor Green
20..30 | % {
  $name = "VM_Client{0:00}" -f $_
 Move-VM -VM $name -Destination `
    (Get-FolderFromPath 'vm\Formation\Postes de travail')
 }


Write-Host "On déplace les VM Clients dans le Dossiers du Client1" -ForegroundColor Green
31..40 | % {
  $name = "VM_Client{0:00}" -f $_
 Move-VM -VM $name -Destination `
    (Get-FolderFromPath 'vm\Hébergment\Client1\IaaS\Postes de travail')
 }


Write-Host "On déplace les VM Serveurs dans le Dossiers du Developpement" -ForegroundColor Green
128..143 | % {
  $name = "VM_DEV{0:000000}" -f $_
 Move-VM -VM $name -Destination `
    (Get-FolderFromPath 'vm\Developpement\Serveurs')
 }

 #on creer des Tag et catégories

 $categorieDeTag = 'Developpement'
New-TagCategory -Name $categorieDeTag

$tag = 'SRV_DEV'
New-Tag -Name $tag -Category $categorieDeTag
Get-VMFromFolder "vm\Developpement\Serveurs" | New-TagAssignment $tag
