Powershell, PowerCLI, and cloning - jwells24/Tech-Journal GitHub Wiki
Powershell, PowerCLI, & Cloning
Install Dependencies
- First, we are going to install the dependencies for PowerCLI & Ansible onto xubuntu-wan, using the commands below.
sudo apt install sshpass python3-paramiko git
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
ansible --version
cat >> ~/.ansible.cfg << EOF
[defaults]
host_key_checking = false
EOF
sudo snap install powershell --classic
pwsh
Write-Host $PSVersionTable
Install-Module VMware.PowerCLI -Scope CurrentUser
Get-Module VMware.PowerCLI -ListAvailable
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
Interacting with Vcenter from Powershell
- Now, we want to interact with our vcenter object in powershell on our xubuntu-wan box.
- Create some variables in powershell to be used, and enter powershell with the first command below. Enter your credentials to connect to the server.
pwsh
$vcenter="vcenter.jack.local"
Connect-VIServer -Server $vcenter
- Use the command below to see the active virtual machines in Vcenter, once you have successfully connected. Use the second command below to view the datastores available to you.
Get-VM
Get-DataStore
- Set a variable for your DC1 virtual machine and set a variable for that grabs the snapshot 'base' of DC1 using the commands below. Also, set a variable for the vmhost using the last command below.
$vm = Get-VM -Name DC1-super7
$snapshot = Get-Snapshot -VM $vm -Name "Base"
$vmhost=Get-VMHost -Name "192.168.7.17"
- Set a variable for a datastore using the command below. Also, create a variable for the linked clone we want to create using the second command below.
$ds = Get-DataStore -Name "datastore1-super7"
$linkedClone = "{0}.linked" -f $vm.name
- Now, create a new linked virtual machine and then a new, fully operational base vm with the second command.
$linkedvm = New-VM -LinkedClone -Name $linkedClone -VM $vm -ReferenceSnapshot $snapshot -VMHost $vmhost -Datastore $ds
$newvm = New-VM -Name "server.2019.gui.base" -VM $linkedvm -VMHost $vmhost -Datastore $ds
- Take a snapshot of the vm we just created using the command below. Also, removed the DC1-linked vm using the second command below.
$newvm | New-Snapshot -Name "Base"
$linkedvm | Remove-VM