Skip to content

Instantly share code, notes, and snippets.

@raulunzue
Created March 6, 2019 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raulunzue/8b4957dbc1b87556e4556edede84c9e4 to your computer and use it in GitHub Desktop.
Save raulunzue/8b4957dbc1b87556e4556edede84c9e4 to your computer and use it in GitHub Desktop.
# EL BLOG DE NEGU
# https://www.maquinasvirtuales.eu/ejemplo-de-script-con-vmware-powercli/
## Obtenemos el nombre del servidor
param( [string] $vCenter, [string] $vSwitch_Name, [string] $PortGroup_Nombre, [string] $PortGroup_vlanid)
## Cargamos las "librerias" de VMware
add-pssnapin VMware.VimAutomation.Core
if (($vCenter -eq "") -or($vSwitch_Name -eq "") -or($PortGroup_Nombre -eq "") -or ($PortGroup_vlanid -eq "")){
    Write-Host
    Write-Host
    Write-Host "Debes introducir el correctamente los parametros necesarios"
    Write-Host " powershell.exe crear_port_groups.ps1 vCenter vSwitch_Name PortGroup_Nombre PortGroup_vlanid"
    Write-Host
    Write-Host
    exit
}
foreach ($Servidor_ESXi in $ServidoresESXi) {
    ## Obtenemos el switch virtual
    $Servidor_ESXi_vs = Get-VirtualSwitch -VMHost $Servidor_ESXi -Name $vSwitch_Name
    ## Comprobamos si existe ese port group
    Get-VirtualPortGroup -VirtualSwitch $Servidor_ESXi_vs | ?{$_.Name -eq $PortGroup_Nombre} | %{$Existe_PG=$TRUE}
    ## Si no existe el PortGroup con ese nombre lo creamos
    if (!$Existe_PG) {
        ## Creamos el grupo de Puertos
        New-VirtualPortGroup -Name $PortGroup_Nombre -VirtualSwitch $Servidor_ESXi_vs -VLanId $PortGroup_vlanid
        Write-Host "Creado el Grupo de Puertos $PortGroup_Nombre en el switch $Servidor_ESXi_vs del servidor $Servidor_ESXi" -ForegroundColor Green
    }
    elseif ($Existe_PG) {
        Write-Host "Ya existe el Grupo de Puertos $PortGroup_Nombre en el switch $Servidor_ESXi_vs del servidor $Servidor_ESXi" -ForegroundColor Cyan
    }
}
## Nos desconectamos del servidor vCenter
DisConnect-VIServer -Server $vCenter -Confirm:$FALSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment