{"id":3163,"date":"2020-08-07T22:45:08","date_gmt":"2020-08-07T20:45:08","guid":{"rendered":"https:\/\/www.mariotti.de\/?p=3163"},"modified":"2020-08-07T22:45:08","modified_gmt":"2020-08-07T20:45:08","slug":"powershell-beispiele-zur-vmware-powercli","status":"publish","type":"post","link":"https:\/\/dev.mariotti.de\/?p=3163","title":{"rendered":"PowerShell Beispiele zur VMware PowerCLI"},"content":{"rendered":"<p>Einige PowerShell Beispiele zur VMware PowerCLI<\/p>\n<pre><code class=\"language-powershell\"># https:\/\/www.vmware.com\/support\/developer\/PowerCLI\/\n\n# Import module\nImport-Module VMware.VimAutomation.Core\n\n# Connect to vCenter\nConnect-VIServer -Server 'vcenter.mydomain.local' -Force -ErrorAction Stop -WarningAction SilentlyContinue \n\n# List all VMs\nGet-VM\n\n# Retrive some info from VM\nGet-VM -Name 'TestVM01'\n\n# Query VMware Tools Version from all VMs\nGet-VM | Get-VMGuest | Select VMName, ToolsVersion\n\n# Test if VM exists\nif (Get-VM -Name 'TestVM01' -ea SilentlyContinue) { Write-Host \"VM found!\" } else { Write-Host \"VM NOT found!\" }\n\n# Create a new empty VM with some parameters\nNew-VM -Name 'TestVM01' -ResourcePool 'Host01' -Datastore 'Datastore01' -NumCPU 2 -MemoryGB 4 -DiskGB 40 -NetworkName 'Network01' -Floppy -CD -DiskStorageFormat Thin -GuestID winNetDatacenterGuest\n\n# Create a temporary OS customization specification (for use in the next example)\n$OSCustomSpec = New-OSCustomizationSpec -OStype Windows -NamingScheme VM -ChangeSid -Workgroup 'Workgroup' -AdminPassword 'Password' -OrgName 'Company Name' -FullName 'Company Name' -ProductKey 'AAAAA-BBBBB-CCCCC-DDDDD-EEEEE' -GuiRunOnce 'C:\\temp\\start.cmd' -AutoLogonCount 1 -TimeZone 110\n\n# Create a new VM via cloning from template with OS customization specification\n# Note: The parameter ResourcePool can be used te specify a ResourcePool, Clustoer or Host\nNew-VM -Name 'TestVM01' -ResourcePool 'Host01' -Template $(Get-Template 'TemplateVM01') -OSCustomizationSpec $OSCustomSpec -Location 'Folder01\\Folder02' -Datastore 'Datastore01'\n\n# Change disk size\/CPU\/Memory\nGet-HardDisk -VM 'TestVM01' | Set-HardDisk -CapacityGB 100 -Confirm:$false\nSet-VM -VM 'TestVM01' -NumCpu 2 -Confirm:$false\nSet-VM -VM 'TestVM01' -MemoryGB 4 -Confirm:$false\n\n# Add\/Remove CD-ROM drive\/FlopyDrive\nNew-CDDrive -VM 'TestVM01' -StartConnected:$true\nGet-CDDrive 'TestVM01' | Remove-CDDrive -Confirm:$false\nNew-FloppyDrive -VM 'TestVM01' -StartConnected:$true\nGet-FloppyDrive 'TestVM01' | Remove-FloppyDrive -Confirm:$false\n\n# Advanced settings\nNew-AdvancedSetting -Entity 'TestVM01' -Name isolation.tools.copy.disable -Value true -Confirm:$false -force\nNew-AdvancedSetting -Entity 'TestVM01' -Name isolation.tools.paste.disable -Value true -Confirm:$false -force\nNew-AdvancedSetting -Entity 'TestVM01' -Name isolation.tools.dnd.disable -Value true -Confirm:$false -force\n\n# Get PowerState, PowerOff, PowerOn, etc.\n(Get-VM 'TestVM01').PowerState\nStop-VM -VM 'TestVM01' -Confirm:$false\nStart-VM -VM 'TestVM01' -Confirm:$false\n\n# Remove VM\nRemove-VM -VM 'TestVM01' -DeletePermanently -Confirm:$false -ErrorAction Stop\n\n# Get network adapter details\nGet-VM 'TestVM01' | Get-NetworkAdapter\n\n# Remove existing network adapter, add a new Vmxnet3 NIC to a specific network and with static MAC address\nGet-VM 'TestVM01' | Get-NetworkAdapter | Remove-NetworkAdapter -Confirm:$false\nGet-VM 'TestVM01' | New-NetworkAdapter -NetworkName 'Network01' -StartConnected:$true -Confirm:$false -Type Vmxnet3\nSet-VMMACAddress -VMname 'TestVM01' -MACAddress '00:50:56:88:88:88' -ErrorAction Stop\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Einige einfacherer PowerShell Beispiele zur VMware PowerCLI<\/p>\n","protected":false},"author":2,"featured_media":2425,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-3163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/3163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3163"}],"version-history":[{"count":0,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/posts\/3163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=\/wp\/v2\/media\/2425"}],"wp:attachment":[{"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.mariotti.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}