Skip to content
Menu
  • CloudThesis-Home
  • Cloud Management
    • VMware Cloud
    • VCF (VMware Cloud Foundation)
    • vRO
    • vRA
    • vRealize Log Insight
    • vCloud Director
    • Hybrid Cloud Extension (HCX)
    • vCloud
    • Cloud on AWS
  • VMware
    • vCenter
    • vSphere
      • Generic vSphere Commands
    • ESXi
    • VMware vRealize Suite Lifecycle Manager –8 Deployment Guide
    • VMware Site Recovery Manager (SRM)
    • vRealize Network Insight
    • VMware Horizon
  • Power CLI
    • PowerCLI Snippets
  • NSX
  • vSAN
    • vSAN 7
  • Nutanix
  • VOIP
  • Microsoft
    • Azure Cloud
    • Microsoft Windows Server
    • Hypervisor
    • Create Azure VMs (Portal)
    • Create Azure VMs – PowerShell
  • Author
  • Contact

PowerCLI Snippets Tutorial

Posted on June 24, 2020June 24, 2020
PowerCLI – Don't Call Me Captain

Configuration

Import/Export vCenter Roles

#Export Role
$role = Get-VIRole -Name "MyRole" |Get-VIPrivilege; $role.id -join ","

#Import Role (Use the output from the command above)
New-VIRole -Name "MyRole" -Privilege (Get-VIPrivilege -Id System.Anonymous,System.View,System.Read)

Create Profile

New-Item -Path $Profile -ItemType file -Force
notepad $profile

Load VMware Cmdlet

Add-PsSnapin VMware.VimAutomation.Core -ea "SilentlyContinue"

Connect to vCenter Server

Connect-VIServer -server <Server> -user administrator -password <Password>


PowerShell Basics

Where statements

get-vm  | Where-Object PowerState -eq "PoweredOff"
get-vm  | ? PowerState -eq "PoweredOff"


Snippets

Move ESXi Hosts between Datacenters or Clusters

Get-VMHost -Name <ESX> | Move-VMHost -Destination <Cluster or Datacenter>

Create VMKernel Port for vMotion

$esx = Get-VMHost <HOST>
$vs = Get-VirtualSwitch -Name "dvSwitch"
$pg = Get-VirtualPortGroup -Name "dvPortGroup"
New-VMHostNetworkAdapter -VMHost $esx -PortGroup $pg -VirtualSwitch $vs -IP <IP> -SubnetMask <MASK> -VMotionEnabled:$true

Create VMKernel Port for vMotion one-liner

New-VMHostNetworkAdapter -VMHost "<HOST>" -PortGroup "<PORTGROUP>" -VirtualSwitch "dvSwitch Lab" -IP <IP> -SubnetMask <MASK> -VMotionEnabled:$true

Add multiple Hosts to vCenter by Hostname Range

1..20 | Foreach-Object { Add-VMHost esx$_.virten.lab -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true}

Add multiple Hosts to vCenter by IP-Address Range

10..20 | ForEach-Object { [System.Net.Dns]::GetHostbyAddress("192.168.0.$_")  } |
select-object HostName | ForEach-Object { Add-VMHost $_.HostName -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true }

Add multiple Hosts to vCenter by Input Textfile

Get-Content hosts.txt | Foreach-Object { Add-VMHost $_ -Location (Get-Datacenter Lab) -User root -Password <Password> -RunAsync -force:$true}

#Migrate E1000 Adapter to vmxnet3

Get-VM -name "<VM>" | Get-NetworkAdapter | Where { $_.Type -eq "E1000"} | Set-NetworkAdapter -Type "vmxnet3"

Thanks hope you like it.

Rajiv Pandey. 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search Topic

Categories

  • Azure Cloud
  • Cloud Management
  • Cloud on AWS
  • Create Azure VMs – PowerShell
  • Create Azure VMs (Portal)
  • ESXi
  • Generic vSphere Commands
  • Hybrid Cloud Extension (HCX)
  • Hypervisor
  • Microsoft
  • Microsoft Azure Cloud and Services
  • Microsoft Windows Server
  • NSX
  • Nutanix
  • Power CLI
  • PowerCLI Snippets
  • vCenter
  • VCF (VMware Cloud Foundation)
  • vCloud
  • vCloud Director
  • VMware
  • VMware Cloud
  • VMware Horizon
  • VMware Site Recovery Manager (SRM)
  • VMware vRealize Suite Lifecycle Manager – 8.0
  • vRA
  • vRealize Log Insight
  • vRealize Network Insight
  • vRO
  • vSAN
  • vSAN 7
  • vSphere
  • Windows Servers
©2025 | WordPress Theme by Superbthemes.com