Difference between revisions of "PowerCLI"

From HackerNet
Jump to: navigation, search
Line 24: Line 24:
 
==Script==
 
==Script==
 
Ett script för att skapa vm. New-VM.ps1
 
Ett script för att skapa vm. New-VM.ps1
$VSPHERE = Read-host "Skriv IP/DNS-namn till vCenter/ESXi"
+
<syntaxhighlight lang="powershell">
Connect-VIServer $VSPHERE
+
$VSPHERE = Read-host "Skriv IP/DNS-namn till vCenter/ESXi"
+
Connect-VIServer $VSPHERE
$VMName = Read-host "Enter the name of the VM you wish to create"
+
 
Get-VMHost | Format-Wide
+
$VMName = Read-host "Enter the name of the VM you wish to create"
$HOSTName = Read-host "Enter host"
+
Get-VMHost | Format-Wide
Get-Datastore | Format-Wide
+
$HOSTName = Read-host "Enter host"
$DSName = Read-Host "Enter datastore"
+
Get-Datastore | Format-Wide
$VERSION = Read-Host "Enter VM-Version, exempel v9"
+
$DSName = Read-Host "Enter datastore"
$NUMCPU =  Read-Host "Antal CPU-cores"  
+
$VERSION = Read-Host "Enter VM-Version, exempel v9"
$MEMORYMB =  Read-Host "RAM, Antal MB"  
+
$NUMCPU =  Read-Host "Antal CPU-cores"  
$DiskGB =  Read-Host "Disk, Antal GB"
+
$MEMORYMB =  Read-Host "RAM, Antal MB"  
Get-VirtualPortGroup -VMHost $HOSTName
+
$DiskGB =  Read-Host "Disk, Antal GB"
$NETWORK = Read-Host "Select Network"
+
Get-VirtualPortGroup -VMHost $HOSTName
$NOTES = Read-Host "Notes"
+
$NETWORK = Read-Host "Select Network"
[System.Enum]::GetNames([VMware.Vim.VirtualMachineGuestOsIdentifier]) | Format-Wide
+
$NOTES = Read-Host "Notes"
$GUESTID =  Read-Host "Select that guestid"  
+
[System.Enum]::GetNames([VMware.Vim.VirtualMachineGuestOsIdentifier]) | Format-Wide
+
$GUESTID =  Read-Host "Select that guestid"  
Write-Host -ForegroundColor Green "Now the magic happens"
+
 
Write-Host "Creating VM"
+
Write-Host -ForegroundColor Green "Now the magic happens"
New-VM -Name $VMName -VMHost $HOSTName -Datastore $DSName -DiskStorageFormat Thin -version $VERSION -GuestId $GUESTID -NumCpu $NUMCPU -MemoryMB $MEMORYMB -Notes $NOTES -DiskGB $DiskGB -NetworkName $NETWORK | out-null
+
Write-Host "Creating VM"
+
New-VM -Name $VMName -VMHost $HOSTName -Datastore $DSName -DiskStorageFormat Thin -version $VERSION -GuestId $GUESTID -NumCpu $NUMCPU -MemoryMB $MEMORYMB -Notes $NOTES -DiskGB $DiskGB -NetworkName $NETWORK | out-null
Write-Host -ForegroundColor Green "Convert to vmxnet3?"
+
 
Get-VM $VMName | Get-NetworkAdapter | Set-NetworkAdapter -Type vmxnet3 | out-null
+
Write-Host -ForegroundColor Green "Convert to vmxnet3?"
Start-Sleep -Seconds 1;
+
Get-VM $VMName | Get-NetworkAdapter | Set-NetworkAdapter -Type vmxnet3 | out-null
Write-Host "Starting the VM"
+
Start-Sleep -Seconds 1;
Start-vm -VM $VMName -runAsync
+
Write-Host "Starting the VM"
Start-Sleep -Seconds 1;
+
Start-vm -VM $VMName -runAsync
Write-Host -ForegroundColor Green "Power On Complete"
+
Start-Sleep -Seconds 1;
 +
Write-Host -ForegroundColor Green "Power On Complete"
 +
</syntaxhighlight>

Revision as of 22:30, 23 July 2015

PowerCLI är ett Windows Powershell-gränssnitt för hantering av VMware vSphere. PowerCLI distribueras som en Powershell snapin, och innehåller över 370 Powershell-cmdlets för att hantera och automatisera vSphere och vCloud.

"a powerful command-line tool that lets you automate all aspects of vSphere management, including network, storage, VM, guest OS and more" - VMware

Installation

Ladda ner installationsfilen från VMwares hemsida. Anslut till din ESXi/vCenter med:

Connect-VIServer

Exempel

Flytta alla vms från en datastore till en annan.

Get-Datastore
Get-VM -Datastore Datastore1 | Move-VM -Datastore Datastore2 -DiskStorageFormat thin

Flytta alla vms från en host till en annan.

Get-VMHost 172.20.0.2 | Get-VM | Move-VM -destination 172.20.0.8

Visa alla notes

Get-VM | Select-Object -ExpandProperty Notes

Lista hardware version för alla vms

Get-VM | Select Name, Version

Script

Ett script för att skapa vm. New-VM.ps1

$VSPHERE = Read-host "Skriv IP/DNS-namn till vCenter/ESXi"
Connect-VIServer $VSPHERE

$VMName = Read-host "Enter the name of the VM you wish to create"
Get-VMHost | Format-Wide
$HOSTName = Read-host "Enter host"
Get-Datastore | Format-Wide
$DSName = Read-Host "Enter datastore"
$VERSION = Read-Host "Enter VM-Version, exempel v9"
$NUMCPU =  Read-Host "Antal CPU-cores" 
$MEMORYMB =  Read-Host "RAM, Antal MB" 
$DiskGB =  Read-Host "Disk, Antal GB"
Get-VirtualPortGroup -VMHost $HOSTName
$NETWORK = Read-Host "Select Network"
$NOTES = Read-Host "Notes"
[System.Enum]::GetNames([VMware.Vim.VirtualMachineGuestOsIdentifier]) | Format-Wide
$GUESTID =  Read-Host "Select that guestid" 

Write-Host -ForegroundColor Green "Now the magic happens"
Write-Host "Creating VM"
New-VM -Name $VMName -VMHost $HOSTName -Datastore $DSName -DiskStorageFormat Thin -version $VERSION -GuestId $GUESTID -NumCpu $NUMCPU -MemoryMB $MEMORYMB -Notes $NOTES -DiskGB $DiskGB -NetworkName $NETWORK | out-null

Write-Host -ForegroundColor Green "Convert to vmxnet3?"
Get-VM $VMName | Get-NetworkAdapter | Set-NetworkAdapter -Type vmxnet3 | out-null
Start-Sleep -Seconds 1;
Write-Host "Starting the VM"
Start-vm -VM $VMName -runAsync
Start-Sleep -Seconds 1;
Write-Host -ForegroundColor Green "Power On Complete"