Difference between revisions of "PowerCLI"

From HackerNet
Jump to: navigation, search
Line 25: Line 25:
 
Ett script för att skapa vm. New-VM.ps1
 
Ett script för att skapa vm. New-VM.ps1
 
<syntaxhighlight lang="powershell">
 
<syntaxhighlight lang="powershell">
$VSPHERE = Read-host "Skriv IP/DNS-namn till vCenter/ESXi"
+
#List functions first
Connect-VIServer $VSPHERE
 
  
$VMName = Read-host "Enter the name of the VM you wish to create"
+
function Gather-Info {
 +
[string]$script:VMName = Read-host "Enter the name of the VM you wish to create"
 
Get-VMHost | Format-Wide
 
Get-VMHost | Format-Wide
$HOSTName = Read-host "Enter host"
+
[string]$script:HOSTName = Read-host "Enter host"
Get-Datastore | Format-Wide
+
Get-Datastore -VMHost $HOSTName | Format-Wide
$DSName = Read-Host "Enter datastore"
+
[string]$script:DSName = Read-Host "Enter datastore"
$VERSION = Read-Host "Enter VM-Version, exempel v9"
+
[string]$script:VERSION = Read-Host "Enter VM-Version, exempel v9"
$NUMCPU =  Read-Host "Antal CPU-cores"  
+
[int]$script:NUMCPU =  Read-Host "Antal CPU-cores"  
$MEMORYMB =  Read-Host "RAM, Antal MB"  
+
[int]$script:MEMORYMB =  Read-Host "RAM, Antal MB"  
$DiskGB =  Read-Host "Disk, Antal GB"
+
[int]$script:DiskGB =  Read-Host "Disk, Antal GB"
 
Get-VirtualPortGroup -VMHost $HOSTName
 
Get-VirtualPortGroup -VMHost $HOSTName
$NETWORK = Read-Host "Select Network"
+
[string]$script:NETWORK = Read-Host "Select Network"
$NOTES = Read-Host "Notes"
+
[string]$script:NOTES = Read-Host "Notes"
 
[System.Enum]::GetNames([VMware.Vim.VirtualMachineGuestOsIdentifier]) | Format-Wide
 
[System.Enum]::GetNames([VMware.Vim.VirtualMachineGuestOsIdentifier]) | Format-Wide
$GUESTID =  Read-Host "Select that guestid"  
+
[string]$script:GUESTID =  Read-Host "Select that guestid"  
 +
}
  
 +
 +
function CreateVM {
 
Write-Host -ForegroundColor Green "Now the magic happens"
 
Write-Host -ForegroundColor Green "Now the magic happens"
 
Write-Host "Creating VM"
 
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
 
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?"
 
Write-Host -ForegroundColor Green "Convert to vmxnet3?"
 
Get-VM $VMName | Get-NetworkAdapter | Set-NetworkAdapter -Type vmxnet3 | out-null
 
Get-VM $VMName | Get-NetworkAdapter | Set-NetworkAdapter -Type vmxnet3 | out-null
Line 54: Line 56:
 
Start-Sleep -Seconds 1;
 
Start-Sleep -Seconds 1;
 
Write-Host -ForegroundColor Green "Power On Complete"
 
Write-Host -ForegroundColor Green "Power On Complete"
 +
}
 +
 +
 +
#Connect to vSphere
 +
$VSPHERE = Read-host "Skriv IP till vCenter eller ESXi-host, tryck enter"
 +
Connect-VIServer $VSPHERE
 +
 +
 +
#Use functions and verify input
 +
Do {
 +
Gather-Info
 +
Write-Host $VMName, $HOSTName, $DSName, HW $VERSION, $NUMCPU,vCPU $MEMORYMB,MB RAM $DiskGB,GB $NETWORK, $GUESTID, $NOTES
 +
$confirmation = Read-Host "Proceed y/n"
 +
}
 +
until ($confirmation -eq 'y')
 +
 +
CreateVM
 +
break
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 11:21, 1 November 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

#List functions first

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


function CreateVM {
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"
}


#Connect to vSphere
$VSPHERE = Read-host "Skriv IP till vCenter eller ESXi-host, tryck enter"
Connect-VIServer $VSPHERE


#Use functions and verify input
Do {
Gather-Info
Write-Host $VMName, $HOSTName, $DSName, HW $VERSION, $NUMCPU,vCPU $MEMORYMB,MB RAM $DiskGB,GB $NETWORK, $GUESTID, $NOTES
$confirmation = Read-Host "Proceed y/n"
}
until ($confirmation -eq 'y') 

CreateVM 
break