build 7bbbddf7 | content blog-content@c8490fa · 338 posts | profiles 20 · corpus 267 | 0 skipped | | format
apiVersion: soultec.ch/v1kind: Postmetadata: name: powercli-automating-vm-skeleton-deployment locale: de labels: author: dario-doerflinger series: scripting capability/automation: 1.78 vendor/vmware: 0.88 annotations: source: blog-content/posts/de/powercli-automating-vm-skeleton-deployment.md route: /de/insights/powercli-automating-vm-skeleton-deployment/ schema: /nerd/schema/posts.json markdown: /de/insights/powercli-automating-vm-skeleton-deployment.mdspec: title: VM-Skelette automatisch ausrollen date: 2017-07-20 author: dario-doerflinger locale: de summary: >- Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut. capabilities: [automation] vendors: [vmware] series: scripting migrated: 2026-08-24 comments: 1 translationReviewed: false draft: false sections: - body: | Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut, und es hatte diese Anforderungen: - ein vorgegebenes Service-Konto für die Interaktion mit dem vCenter Server verwenden - je nach VM-Namen, der als Parameter übergeben wird, unterschiedlich konfigurieren - Testserver gehören in den Resource Pool «test» - Testserver bekommen eine Thin-Provisioned-Disk - Produktionsserver gehören in den Resource Pool «production» - Produktionsserver gehören in den Resource Pool «test» - der Rückgabewert ist die MAC-Adresse der erstellten VM - die VM darf nicht mehr als zwei vCPUs haben - die VM darf nicht mehr als 8 GB RAM haben - die Disk der VM darf nicht grösser als 100 GB sein - heading: "

Die Authentifizierung: VICredentialStore

"
body: | Damit sich das Service-Konto sicher und ohne Interaktion authentifizieren kann, brauchten wir einen Weg dafür. Dieses Script erzeugt eine Datei mit den angegebenen Informationen: - Host (vCenter), an dem angemeldet wird - Benutzername - Passwort Update: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts). ```powershell # import VMware related modules Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null # get server and credential input $server = Read-Host "Please provide the servername (fqdn)" $user_name = Read-Host "please provide the username (domain\user)" $user_password_encrypted = Read-Host "please input the password" -AsSecureString $file_location = Read-Host "where should the login.creds file be stored? (e.g.:c:\script)" # convert password back to plain text for further use with New-VICredentialStoreItem $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($user_password_encrypted) $user_password_decrypted = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) # create credential file New-VICredentialStoreItem -Host $server -User $user_name -Password $user_password_decrypted -File "$file_location\login.creds" # remove VMware related modules Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null ``` - heading:

Die VM-Erstellung automatisieren

body: | In diesem konkreten Fall waren einige Dinge vorgegeben, die wir dann fest ins Script geschrieben haben. Du kannst es leicht anpassen, um alle Variablen als Parameter zu übergeben, oder einfach die Werte deiner Umgebung eintragen. ```powershell ############################################################################################ # Script name: VmAutomatedDeployment.ps1 # Description: For simple virtual machine container provisioning use only. # Version: 1.2 # Date: 02.02.2017 # Author: Bechtle Steffen Schweiz AG # History: 02.02.2017 - First tested release # 19.07.2017 - Replaced Portgroup for Networkname, diskformat and resource pool based on hostname # 19.07.2017 - Added return value: MAC Address of created VM as requested by customer ############################################################################################ # Example: # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A053763 -vm_guestid windows9_64Guest -vm_memory 2048 -vm_cpu 2 -vm_network "Server_2037_L2" param ( [string]$vm_guestid, # GuestOS identifier from VMware e.g. windows_64Guest for Windows 10 x64 [string]$vm_name, # Name of the virtual machine #[int64]$vm_disk, # System disk size in GB [int32]$vm_memory, # Memory size in MB [int32]$vm_cpu, # Amount of vCPUs [string]$vm_network # Portgroup name to connect #[string]$vm_folder # VM Folder location # to get the GuestOS identifier run: [VMware.Vim.VirtualMachineGuestOsIdentifier].GetEnumValues() # Common Windows versions: #------------------------- # windows7_64Guest // Windows 7 (x64) # windows7Server64Guest // Windows Server 2008 R2 # windows8_64Guest // Windows 8 (x64) # windows8Server64Guest // Windows Server 2012 R2 # windows9_64Guest // Windows 10 (x64) # windows9Server64Guest // Windows Server 2016 ) # clear global ERROR variable $Error.Clear() # import vmware related modules, get the credentials and connect to the vCenter server Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null Import-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null $creds = Get-VICredentialStoreItem -file "C:\scripts\VmAutomatedDeployment\login.creds" Connect-VIServer -Server $creds.Host -User $creds.User -Password $creds.Password |Out-Null # define global variables $init_cluster = 'virtualFrogLab' $init_datastore = 'VF_ds_01' $vm_folder = 'VM-Staging' $current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss") $log_file = "C:\Scripts\VmAutomatedDeployment\log_$(Get-Date -format "yyyyMMdd").txt" [int64]$vm_disk = 72 $dvPortGroup = Get-VDPortgroup -Name $vm_network # check if var $VM_NAME already exists in the vCenter inventory $CheckInventoryByVmName = Get-VM -Name $vm_name -ErrorAction Ignore if ($CheckInventoryByVmName) { Write-Host "This virtual machine already exists!" } else { # check the inputs for provisioning sizing of the virtual machine # allowed maximums: 2 vCPU / 8192MB vRAM / 100GB vDISK if ($vm_cpu -gt 2) {Write-Host "You input is invalid! (max. 2 vCPUs allowed)"} elseif ($vm_memory -gt 8192) {Write-Host "You input is invalid! (max. 8GB vRAM allowed)"} elseif ($vm_disk -gt 100) {Write-Host "You input is invalid! (max. 100GB vDisk size allowed)"} else { # create new virtual machine container # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A054108 -vm_guestid windows9_64Guest -vm_disk 72 -vm_memory 2048 -vm_cpu 2 -vm_network "Test 10.10.0.0" if ($vm_name -like "vm-t*") { $diskformat = "Thin" $init_cluster ="Test" }else { $diskformat = "Thick" $init_cluster = "Production" } $create_vm = New-VM -Name $vm_name -GuestId $vm_guestid -Location $vm_folder -ResourcePool $init_cluster -Datastore $init_datastore -DiskGB $vm_disk -DiskStorageFormat $diskformat -MemoryMB $vm_memory -NumCpu $vm_cpu -Portgroup $dvPortGroup -CD -Confirm:$false -ErrorAction SilentlyContinue # check if virtual machine exists if ($create_vm) { # change all network adapters to VMXNET3 $change_vm_network = Get-VM -Name $create_vm | Get-NetworkAdapter | Set-NetworkAdapter -Type Vmxnet3 -Confirm:$false -ErrorAction SilentlyContinue # check if network adapter exists if ($change_vm_network) { Add-Content -Path $log_file -Value "$current_date SCRIPT $message" $macaddress = (get-vm -Name $create_vm |get-networkadapter).MacAddress } else { Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file" } } else { Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file" } } } # cleanup and removal of loaded VMware modules Disconnect-VIServer -Server $creds.Host -Confirm:$false Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null # write all error messages to the log file Add-Content -Path $log_file -Value $Error #return the MAC address return $macaddress ```status: corpus: 267 alsoLike: - {ref: posts/vmware-explore-las-vegas-hackathon-2025, score: 1.00} - {ref: posts/vmware-hackathon-2024-project, score: 1.00} - {ref: solutions/vmware/vmware-cloud-foundation/addon/application-services, score: 0.60}
{ "apiVersion": "soultec.ch/v1", "kind": "Post", "metadata": { "name": "powercli-automating-vm-skeleton-deployment", "locale": "de", "labels": { "author": "dario-doerflinger", "series": "scripting", "capability/automation": "1.78", "vendor/vmware": "0.88" }, "annotations": { "source": "blog-content/posts/de/powercli-automating-vm-skeleton-deployment.md", "route": "/de/insights/powercli-automating-vm-skeleton-deployment/", "schema": "/nerd/schema/posts.json", "markdown": "/de/insights/powercli-automating-vm-skeleton-deployment.md" } }, "spec": { "title": "VM-Skelette automatisch ausrollen", "date": "2017-07-20", "author": "dario-doerflinger", "locale": "de", "summary": "Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut.", "capabilities": [ "automation" ], "vendors": [ "vmware" ], "series": "scripting", "migrated": "2026-08-24", "comments": 1, "translationReviewed": false, "draft": false }, "sections": [ { "body": "Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut, und es hatte diese Anforderungen:\n\n- ein vorgegebenes Service-Konto für die Interaktion mit dem vCenter Server verwenden\n- je nach VM-Namen, der als Parameter übergeben wird, unterschiedlich konfigurieren\n - Testserver gehören in den Resource Pool «test»\n - Testserver bekommen eine Thin-Provisioned-Disk\n - Produktionsserver gehören in den Resource Pool «production»\n - Produktionsserver gehören in den Resource Pool «test»\n- der Rückgabewert ist die MAC-Adresse der erstellten VM\n- die VM darf nicht mehr als zwei vCPUs haben\n- die VM darf nicht mehr als 8 GB RAM haben\n- die Disk der VM darf nicht grösser als 100 GB sein" }, { "heading": "

Die Authentifizierung: VICredentialStore

",
"body": "Damit sich das Service-Konto sicher und ohne Interaktion authentifizieren kann, brauchten wir einen Weg dafür. Dieses Script erzeugt eine Datei mit den angegebenen Informationen:\n\n- Host (vCenter), an dem angemeldet wird\n- Benutzername\n- Passwort\n\nUpdate: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).\n\n```powershell\n# import VMware related modules\nImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n\n# get server and credential input\n$server = Read-Host \"Please provide the servername (fqdn)\"\n$user_name = Read-Host \"please provide the username (domain\\user)\"\n$user_password_encrypted = Read-Host \"please input the password\" -AsSecureString\n$file_location = Read-Host \"where should the login.creds file be stored? (e.g.:c:\\script)\"\n\n# convert password back to plain text for further use with New-VICredentialStoreItem\n$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($user_password_encrypted)\n$user_password_decrypted = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) \n\n# create credential file\nNew-VICredentialStoreItem -Host $server -User $user_name -Password $user_password_decrypted -File \"$file_location\\login.creds\"\n\n# remove VMware related modules\nRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n```" }, { "heading": "

Die VM-Erstellung automatisieren

",
"body": "In diesem konkreten Fall waren einige Dinge vorgegeben, die wir dann fest ins Script geschrieben haben. Du kannst es leicht anpassen, um alle Variablen als Parameter zu übergeben, oder einfach die Werte deiner Umgebung eintragen.\n\n```powershell\n############################################################################################\n# Script name: VmAutomatedDeployment.ps1\n# Description: For simple virtual machine container provisioning use only.\n# Version: 1.2\n# Date: 02.02.2017\n# Author: Bechtle Steffen Schweiz AG\n# History: 02.02.2017 - First tested release\n# 19.07.2017 - Replaced Portgroup for Networkname, diskformat and resource pool based on hostname\n# 19.07.2017 - Added return value: MAC Address of created VM as requested by customer\n############################################################################################\n\n# Example: # e.g.: .\\VmAutomatedDeployment.ps1 -vm_name Test_A053763 -vm_guestid windows9_64Guest -vm_memory 2048 -vm_cpu 2 -vm_network \"Server_2037_L2\"\n\nparam (\n\n [string]$vm_guestid, # GuestOS identifier from VMware e.g. windows_64Guest for Windows 10 x64\n [string]$vm_name, # Name of the virtual machine\n #[int64]$vm_disk, # System disk size in GB\n [int32]$vm_memory, # Memory size in MB\n [int32]$vm_cpu, # Amount of vCPUs\n [string]$vm_network # Portgroup name to connect\n #[string]$vm_folder # VM Folder location\n\n # to get the GuestOS identifier run: [VMware.Vim.VirtualMachineGuestOsIdentifier].GetEnumValues()\n # Common Windows versions:\n #-------------------------\n # windows7_64Guest // Windows 7 (x64)\n # windows7Server64Guest // Windows Server 2008 R2\n # windows8_64Guest // Windows 8 (x64)\n # windows8Server64Guest // Windows Server 2012 R2\n # windows9_64Guest // Windows 10 (x64)\n # windows9Server64Guest // Windows Server 2016\n)\n\n# clear global ERROR variable\n$Error.Clear()\n\n# import vmware related modules, get the credentials and connect to the vCenter server\nImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\nImport-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null\n$creds = Get-VICredentialStoreItem -file \"C:\\scripts\\VmAutomatedDeployment\\login.creds\"\nConnect-VIServer -Server $creds.Host -User $creds.User -Password $creds.Password |Out-Null\n\n# define global variables\n$init_cluster = 'virtualFrogLab'\n$init_datastore = 'VF_ds_01'\n$vm_folder = 'VM-Staging'\n$current_date = $(Get-Date -format \"dd.MM.yyyy HH:mm:ss\")\n$log_file = \"C:\\Scripts\\VmAutomatedDeployment\\log_$(Get-Date -format \"yyyyMMdd\").txt\"\n[int64]$vm_disk = 72\n$dvPortGroup = Get-VDPortgroup -Name $vm_network\n\n# check if var $VM_NAME already exists in the vCenter inventory\n\n$CheckInventoryByVmName = Get-VM -Name $vm_name -ErrorAction Ignore\n\nif ($CheckInventoryByVmName) {\n\n Write-Host \"This virtual machine already exists!\"\n\n} else {\n\n # check the inputs for provisioning sizing of the virtual machine\n # allowed maximums: 2 vCPU / 8192MB vRAM / 100GB vDISK\n\n if ($vm_cpu -gt 2) {Write-Host \"You input is invalid! (max. 2 vCPUs allowed)\"}\n elseif ($vm_memory -gt 8192) {Write-Host \"You input is invalid! (max. 8GB vRAM allowed)\"}\n elseif ($vm_disk -gt 100) {Write-Host \"You input is invalid! (max. 100GB vDisk size allowed)\"}\n else {\n\n # create new virtual machine container\n # e.g.: .\\VmAutomatedDeployment.ps1 -vm_name Test_A054108 -vm_guestid windows9_64Guest -vm_disk 72 -vm_memory 2048 -vm_cpu 2 -vm_network \"Test 10.10.0.0\"\n if ($vm_name -like \"vm-t*\")\n {\n $diskformat = \"Thin\"\n $init_cluster =\"Test\"\n }else {\n $diskformat = \"Thick\"\n $init_cluster = \"Production\"\n }\n $create_vm = New-VM -Name $vm_name -GuestId $vm_guestid -Location $vm_folder -ResourcePool $init_cluster -Datastore $init_datastore -DiskGB $vm_disk -DiskStorageFormat $diskformat -MemoryMB $vm_memory -NumCpu $vm_cpu -Portgroup $dvPortGroup -CD -Confirm:$false -ErrorAction SilentlyContinue\n\n # check if virtual machine exists\n if ($create_vm) {\n # change all network adapters to VMXNET3\n $change_vm_network = Get-VM -Name $create_vm | Get-NetworkAdapter | Set-NetworkAdapter -Type Vmxnet3 -Confirm:$false -ErrorAction SilentlyContinue\n\n # check if network adapter exists\n if ($change_vm_network) {\n Add-Content -Path $log_file -Value \"$current_date SCRIPT $message\"\n $macaddress = (get-vm -Name $create_vm |get-networkadapter).MacAddress\n } else {\n Write-Host \"There was an unexpected error during the provisioning. For more information see log file: $log_file\"\n }\n } else {\n Write-Host \"There was an unexpected error during the provisioning. For more information see log file: $log_file\"\n\n }\n\n }\n\n}\n\n# cleanup and removal of loaded VMware modules\nDisconnect-VIServer -Server $creds.Host -Confirm:$false\nRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null\n\n# write all error messages to the log file\nAdd-Content -Path $log_file -Value $Error\n\n#return the MAC address\nreturn $macaddress\n```" } ], "status": { "corpus": 267, "alsoLike": [ { "ref": "posts/vmware-explore-las-vegas-hackathon-2025", "score": "1.00" }, { "ref": "posts/vmware-hackathon-2024-project", "score": "1.00" }, { "ref": "solutions/vmware/vmware-cloud-foundation/addon/application-services", "score": "0.60" } ] }}
apiVersion = "soultec.ch/v1"kind = "Post"[metadata]name = "powercli-automating-vm-skeleton-deployment"locale = "de"[metadata.labels]author = "dario-doerflinger"series = "scripting""capability/automation" = "1.78""vendor/vmware" = "0.88"[metadata.annotations]source = "blog-content/posts/de/powercli-automating-vm-skeleton-deployment.md"route = "/de/insights/powercli-automating-vm-skeleton-deployment/"schema = "/nerd/schema/posts.json"markdown = "/de/insights/powercli-automating-vm-skeleton-deployment.md"[spec]title = "VM-Skelette automatisch ausrollen"date = 2017-07-20author = "dario-doerflinger"locale = "de"summary = "Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut."capabilities = ["automation"]vendors = ["vmware"]series = "scripting"migrated = 2026-08-24comments = 1translationReviewed = falsedraft = false[[sections]]body = '''Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut, und es hatte diese Anforderungen:- ein vorgegebenes Service-Konto für die Interaktion mit dem vCenter Server verwenden- je nach VM-Namen, der als Parameter übergeben wird, unterschiedlich konfigurieren - Testserver gehören in den Resource Pool «test» - Testserver bekommen eine Thin-Provisioned-Disk - Produktionsserver gehören in den Resource Pool «production» - Produktionsserver gehören in den Resource Pool «test»- der Rückgabewert ist die MAC-Adresse der erstellten VM- die VM darf nicht mehr als zwei vCPUs haben- die VM darf nicht mehr als 8 GB RAM haben- die Disk der VM darf nicht grösser als 100 GB sein'''[[sections]]heading = "

Die Authentifizierung: VICredentialStore

"
body = '''Damit sich das Service-Konto sicher und ohne Interaktion authentifizieren kann, brauchten wir einen Weg dafür. Dieses Script erzeugt eine Datei mit den angegebenen Informationen:- Host (vCenter), an dem angemeldet wird- Benutzername- PasswortUpdate: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).```powershell# import VMware related modulesImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null# get server and credential input$server = Read-Host "Please provide the servername (fqdn)"$user_name = Read-Host "please provide the username (domain\user)"$user_password_encrypted = Read-Host "please input the password" -AsSecureString$file_location = Read-Host "where should the login.creds file be stored? (e.g.:c:\script)"# convert password back to plain text for further use with New-VICredentialStoreItem$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($user_password_encrypted)$user_password_decrypted = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) # create credential fileNew-VICredentialStoreItem -Host $server -User $user_name -Password $user_password_decrypted -File "$file_location\login.creds"# remove VMware related modulesRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null```'''[[sections]]heading = "

Die VM-Erstellung automatisieren

"
body = '''In diesem konkreten Fall waren einige Dinge vorgegeben, die wir dann fest ins Script geschrieben haben. Du kannst es leicht anpassen, um alle Variablen als Parameter zu übergeben, oder einfach die Werte deiner Umgebung eintragen.```powershell############################################################################################# Script name: VmAutomatedDeployment.ps1# Description: For simple virtual machine container provisioning use only.# Version: 1.2# Date: 02.02.2017# Author: Bechtle Steffen Schweiz AG# History: 02.02.2017 - First tested release# 19.07.2017 - Replaced Portgroup for Networkname, diskformat and resource pool based on hostname# 19.07.2017 - Added return value: MAC Address of created VM as requested by customer############################################################################################# Example: # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A053763 -vm_guestid windows9_64Guest -vm_memory 2048 -vm_cpu 2 -vm_network "Server_2037_L2"param ( [string]$vm_guestid, # GuestOS identifier from VMware e.g. windows_64Guest for Windows 10 x64 [string]$vm_name, # Name of the virtual machine #[int64]$vm_disk, # System disk size in GB [int32]$vm_memory, # Memory size in MB [int32]$vm_cpu, # Amount of vCPUs [string]$vm_network # Portgroup name to connect #[string]$vm_folder # VM Folder location # to get the GuestOS identifier run: [VMware.Vim.VirtualMachineGuestOsIdentifier].GetEnumValues() # Common Windows versions: #------------------------- # windows7_64Guest // Windows 7 (x64) # windows7Server64Guest // Windows Server 2008 R2 # windows8_64Guest // Windows 8 (x64) # windows8Server64Guest // Windows Server 2012 R2 # windows9_64Guest // Windows 10 (x64) # windows9Server64Guest // Windows Server 2016)# clear global ERROR variable$Error.Clear()# import vmware related modules, get the credentials and connect to the vCenter serverImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-NullImport-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null$creds = Get-VICredentialStoreItem -file "C:\scripts\VmAutomatedDeployment\login.creds"Connect-VIServer -Server $creds.Host -User $creds.User -Password $creds.Password |Out-Null# define global variables$init_cluster = 'virtualFrogLab'$init_datastore = 'VF_ds_01'$vm_folder = 'VM-Staging'$current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss")$log_file = "C:\Scripts\VmAutomatedDeployment\log_$(Get-Date -format "yyyyMMdd").txt"[int64]$vm_disk = 72$dvPortGroup = Get-VDPortgroup -Name $vm_network# check if var $VM_NAME already exists in the vCenter inventory$CheckInventoryByVmName = Get-VM -Name $vm_name -ErrorAction Ignoreif ($CheckInventoryByVmName) { Write-Host "This virtual machine already exists!"} else { # check the inputs for provisioning sizing of the virtual machine # allowed maximums: 2 vCPU / 8192MB vRAM / 100GB vDISK if ($vm_cpu -gt 2) {Write-Host "You input is invalid! (max. 2 vCPUs allowed)"} elseif ($vm_memory -gt 8192) {Write-Host "You input is invalid! (max. 8GB vRAM allowed)"} elseif ($vm_disk -gt 100) {Write-Host "You input is invalid! (max. 100GB vDisk size allowed)"} else { # create new virtual machine container # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A054108 -vm_guestid windows9_64Guest -vm_disk 72 -vm_memory 2048 -vm_cpu 2 -vm_network "Test 10.10.0.0" if ($vm_name -like "vm-t*") { $diskformat = "Thin" $init_cluster ="Test" }else { $diskformat = "Thick" $init_cluster = "Production" } $create_vm = New-VM -Name $vm_name -GuestId $vm_guestid -Location $vm_folder -ResourcePool $init_cluster -Datastore $init_datastore -DiskGB $vm_disk -DiskStorageFormat $diskformat -MemoryMB $vm_memory -NumCpu $vm_cpu -Portgroup $dvPortGroup -CD -Confirm:$false -ErrorAction SilentlyContinue # check if virtual machine exists if ($create_vm) { # change all network adapters to VMXNET3 $change_vm_network = Get-VM -Name $create_vm | Get-NetworkAdapter | Set-NetworkAdapter -Type Vmxnet3 -Confirm:$false -ErrorAction SilentlyContinue # check if network adapter exists if ($change_vm_network) { Add-Content -Path $log_file -Value "$current_date SCRIPT $message" $macaddress = (get-vm -Name $create_vm |get-networkadapter).MacAddress } else { Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file" } } else { Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file" } }}# cleanup and removal of loaded VMware modulesDisconnect-VIServer -Server $creds.Host -Confirm:$falseRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null# write all error messages to the log fileAdd-Content -Path $log_file -Value $Error#return the MAC addressreturn $macaddress```'''[status]corpus = 267[[status.alsoLike]]ref = "posts/vmware-explore-las-vegas-hackathon-2025"score = "1.00"[[status.alsoLike]]ref = "posts/vmware-hackathon-2024-project"score = "1.00"[[status.alsoLike]]ref = "solutions/vmware/vmware-cloud-foundation/addon/application-services"score = "0.60"
<?xml version="1.0" encoding="UTF-8"?><manifest kind="Post"> <apiVersion>soultec.ch/v1</apiVersion> <metadata> <name>powercli-automating-vm-skeleton-deployment</name> <locale>de</locale> <labels> <author>dario-doerflinger</author> <series>scripting</series> <entry key="capability/automation">1.78</entry> <entry key="vendor/vmware">0.88</entry> </labels> <annotations> <source>blog-content/posts/de/powercli-automating-vm-skeleton-deployment.md</source> <route>/de/insights/powercli-automating-vm-skeleton-deployment/</route> <schema>/nerd/schema/posts.json</schema> <markdown>/de/insights/powercli-automating-vm-skeleton-deployment.md</markdown> </annotations> </metadata> <spec> <title>VM-Skelette automatisch ausrollen</title> <date>2017-07-20</date> <author>dario-doerflinger</author> <locale>de</locale> <summary>Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut.</summary> <capabilities> <item>automation</item> </capabilities> <vendors> <item>vmware</item> </vendors> <series>scripting</series> <migrated>2026-08-24</migrated> <comments>1</comments> <translationReviewed>false</translationReviewed> <draft>false</draft> </spec> <sections> <section> <body>Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut, und es hatte diese Anforderungen:- ein vorgegebenes Service-Konto für die Interaktion mit dem vCenter Server verwenden- je nach VM-Namen, der als Parameter übergeben wird, unterschiedlich konfigurieren - Testserver gehören in den Resource Pool «test» - Testserver bekommen eine Thin-Provisioned-Disk - Produktionsserver gehören in den Resource Pool «production» - Produktionsserver gehören in den Resource Pool «test»- der Rückgabewert ist die MAC-Adresse der erstellten VM- die VM darf nicht mehr als zwei vCPUs haben- die VM darf nicht mehr als 8 GB RAM haben- die Disk der VM darf nicht grösser als 100 GB sein </body> </section> <section> <heading>

Die Authentifizierung: VICredentialStore

</heading>
<body>Damit sich das Service-Konto sicher und ohne Interaktion authentifizieren kann, brauchten wir einen Weg dafür. Dieses Script erzeugt eine Datei mit den angegebenen Informationen:- Host (vCenter), an dem angemeldet wird- Benutzername- PasswortUpdate: Dieses Script liegt inzwischen auf [GitHub](https://github.com/virtualFrog/PowerCLI-Scripts).```powershell# import VMware related modulesImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null# get server and credential input$server = Read-Host "Please provide the servername (fqdn)"$user_name = Read-Host "please provide the username (domain\user)"$user_password_encrypted = Read-Host "please input the password" -AsSecureString$file_location = Read-Host "where should the login.creds file be stored? (e.g.:c:\script)"# convert password back to plain text for further use with New-VICredentialStoreItem$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($user_password_encrypted)$user_password_decrypted = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) # create credential fileNew-VICredentialStoreItem -Host $server -User $user_name -Password $user_password_decrypted -File "$file_location\login.creds"# remove VMware related modulesRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null``` </body> </section> <section> <heading>

Die VM-Erstellung automatisieren

</heading>
<body>In diesem konkreten Fall waren einige Dinge vorgegeben, die wir dann fest ins Script geschrieben haben. Du kannst es leicht anpassen, um alle Variablen als Parameter zu übergeben, oder einfach die Werte deiner Umgebung eintragen.```powershell############################################################################################# Script name: VmAutomatedDeployment.ps1# Description: For simple virtual machine container provisioning use only.# Version: 1.2# Date: 02.02.2017# Author: Bechtle Steffen Schweiz AG# History: 02.02.2017 - First tested release# 19.07.2017 - Replaced Portgroup for Networkname, diskformat and resource pool based on hostname# 19.07.2017 - Added return value: MAC Address of created VM as requested by customer############################################################################################# Example: # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A053763 -vm_guestid windows9_64Guest -vm_memory 2048 -vm_cpu 2 -vm_network "Server_2037_L2"param ( [string]$vm_guestid, # GuestOS identifier from VMware e.g. windows_64Guest for Windows 10 x64 [string]$vm_name, # Name of the virtual machine #[int64]$vm_disk, # System disk size in GB [int32]$vm_memory, # Memory size in MB [int32]$vm_cpu, # Amount of vCPUs [string]$vm_network # Portgroup name to connect #[string]$vm_folder # VM Folder location # to get the GuestOS identifier run: [VMware.Vim.VirtualMachineGuestOsIdentifier].GetEnumValues() # Common Windows versions: #------------------------- # windows7_64Guest // Windows 7 (x64) # windows7Server64Guest // Windows Server 2008 R2 # windows8_64Guest // Windows 8 (x64) # windows8Server64Guest // Windows Server 2012 R2 # windows9_64Guest // Windows 10 (x64) # windows9Server64Guest // Windows Server 2016)# clear global ERROR variable$Error.Clear()# import vmware related modules, get the credentials and connect to the vCenter serverImport-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-NullImport-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null$creds = Get-VICredentialStoreItem -file "C:\scripts\VmAutomatedDeployment\login.creds"Connect-VIServer -Server $creds.Host -User $creds.User -Password $creds.Password |Out-Null# define global variables$init_cluster = 'virtualFrogLab'$init_datastore = 'VF_ds_01'$vm_folder = 'VM-Staging'$current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss")$log_file = "C:\Scripts\VmAutomatedDeployment\log_$(Get-Date -format "yyyyMMdd").txt"[int64]$vm_disk = 72$dvPortGroup = Get-VDPortgroup -Name $vm_network# check if var $VM_NAME already exists in the vCenter inventory$CheckInventoryByVmName = Get-VM -Name $vm_name -ErrorAction Ignoreif ($CheckInventoryByVmName) { Write-Host "This virtual machine already exists!"} else { # check the inputs for provisioning sizing of the virtual machine # allowed maximums: 2 vCPU / 8192MB vRAM / 100GB vDISK if ($vm_cpu -gt 2) {Write-Host "You input is invalid! (max. 2 vCPUs allowed)"} elseif ($vm_memory -gt 8192) {Write-Host "You input is invalid! (max. 8GB vRAM allowed)"} elseif ($vm_disk -gt 100) {Write-Host "You input is invalid! (max. 100GB vDisk size allowed)"} else { # create new virtual machine container # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A054108 -vm_guestid windows9_64Guest -vm_disk 72 -vm_memory 2048 -vm_cpu 2 -vm_network "Test 10.10.0.0" if ($vm_name -like "vm-t*") { $diskformat = "Thin" $init_cluster ="Test" }else { $diskformat = "Thick" $init_cluster = "Production" } $create_vm = New-VM -Name $vm_name -GuestId $vm_guestid -Location $vm_folder -ResourcePool $init_cluster -Datastore $init_datastore -DiskGB $vm_disk -DiskStorageFormat $diskformat -MemoryMB $vm_memory -NumCpu $vm_cpu -Portgroup $dvPortGroup -CD -Confirm:$false -ErrorAction SilentlyContinue # check if virtual machine exists if ($create_vm) { # change all network adapters to VMXNET3 $change_vm_network = Get-VM -Name $create_vm | Get-NetworkAdapter | Set-NetworkAdapter -Type Vmxnet3 -Confirm:$false -ErrorAction SilentlyContinue # check if network adapter exists if ($change_vm_network) { Add-Content -Path $log_file -Value "$current_date SCRIPT $message" $macaddress = (get-vm -Name $create_vm |get-networkadapter).MacAddress } else { Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file" } } else { Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file" } }}# cleanup and removal of loaded VMware modulesDisconnect-VIServer -Server $creds.Host -Confirm:$falseRemove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null# write all error messages to the log fileAdd-Content -Path $log_file -Value $Error#return the MAC addressreturn $macaddress``` </body> </section> </sections> <status> <corpus>267</corpus> <alsoLike> <item> <ref>posts/vmware-explore-las-vegas-hackathon-2025</ref> <score>1.00</score> </item> <item> <ref>posts/vmware-hackathon-2024-project</ref> <score>1.00</score> </item> <item> <ref>solutions/vmware/vmware-cloud-foundation/addon/application-services</ref> <score>0.60</score> </item> </alsoLike> </status></manifest>
Scripting · 2017-07-20

VM-Skelette automatisch ausrollen

Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut.

2017-07-20Datum
Dario DörflingerAutor
4Min. Lesezeit
Themen Automation 1.78
Hersteller VMware 0.88

Dieser Beitrag ist von 2017. Er bleibt online, weil er nach wie vor nachgefragt wird, beschreibt aber einen Produktstand von damals.

Kürzlich brauchten wir für einen Kunden ein Script, das ein VM-Skelett ausrollt. Aufgerufen wird es aus einer Automatisierungslösung, die der Kunde gerade baut, und es hatte diese Anforderungen:

  • ein vorgegebenes Service-Konto für die Interaktion mit dem vCenter Server verwenden
  • je nach VM-Namen, der als Parameter übergeben wird, unterschiedlich konfigurieren
    • Testserver gehören in den Resource Pool «test»
    • Testserver bekommen eine Thin-Provisioned-Disk
    • Produktionsserver gehören in den Resource Pool «production»
    • Produktionsserver gehören in den Resource Pool «test»
  • der Rückgabewert ist die MAC-Adresse der erstellten VM
  • die VM darf nicht mehr als zwei vCPUs haben
  • die VM darf nicht mehr als 8 GB RAM haben
  • die Disk der VM darf nicht grösser als 100 GB sein

Die Authentifizierung: VICredentialStore

Damit sich das Service-Konto sicher und ohne Interaktion authentifizieren kann, brauchten wir einen Weg dafür. Dieses Script erzeugt eine Datei mit den angegebenen Informationen:

  • Host (vCenter), an dem angemeldet wird
  • Benutzername
  • Passwort

Update: Dieses Script liegt inzwischen auf GitHub.

# import VMware related modules
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null

# get server and credential input
$server = Read-Host "Please provide the servername (fqdn)"
$user_name = Read-Host "please provide the username (domain\user)"
$user_password_encrypted = Read-Host "please input the password" -AsSecureString
$file_location = Read-Host "where should the login.creds file be stored? (e.g.:c:\script)"

# convert password back to plain text for further use with New-VICredentialStoreItem
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($user_password_encrypted)
$user_password_decrypted = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) 

# create credential file
New-VICredentialStoreItem -Host $server -User $user_name -Password $user_password_decrypted -File "$file_location\login.creds"

# remove VMware related modules
Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null

Die VM-Erstellung automatisieren

In diesem konkreten Fall waren einige Dinge vorgegeben, die wir dann fest ins Script geschrieben haben. Du kannst es leicht anpassen, um alle Variablen als Parameter zu übergeben, oder einfach die Werte deiner Umgebung eintragen.

############################################################################################
# Script name:     VmAutomatedDeployment.ps1
# Description:     For simple virtual machine container provisioning use only.
# Version:         1.2
# Date:            02.02.2017
# Author:          Bechtle Steffen Schweiz AG
# History:         02.02.2017 - First tested release
#                  19.07.2017 - Replaced Portgroup for Networkname, diskformat and resource pool based on hostname
#                  19.07.2017 - Added return value: MAC Address of created VM as requested by customer
############################################################################################

# Example: # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A053763 -vm_guestid windows9_64Guest -vm_memory 2048 -vm_cpu 2 -vm_network "Server_2037_L2"

param (

    [string]$vm_guestid, # GuestOS identifier from VMware e.g. windows_64Guest for Windows 10 x64
    [string]$vm_name, # Name of the virtual machine
    #[int64]$vm_disk, # System disk size in GB
    [int32]$vm_memory, # Memory size in MB
    [int32]$vm_cpu, # Amount of vCPUs
    [string]$vm_network # Portgroup name to connect
    #[string]$vm_folder # VM Folder location

    # to get the GuestOS identifier run: [VMware.Vim.VirtualMachineGuestOsIdentifier].GetEnumValues()
    # Common Windows versions:
    #-------------------------
    # windows7_64Guest          // Windows 7 (x64)
    # windows7Server64Guest     // Windows Server 2008 R2
    # windows8_64Guest          // Windows 8 (x64)
    # windows8Server64Guest     // Windows Server 2012 R2
    # windows9_64Guest          // Windows 10 (x64)
    # windows9Server64Guest     // Windows Server 2016
)

# clear global ERROR variable
$Error.Clear()

# import vmware related modules, get the credentials and connect to the vCenter server
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null
Import-Module -Name VMware.VimAutomation.Vds -ErrorAction SilentlyContinue |Out-Null
$creds = Get-VICredentialStoreItem -file  "C:\scripts\VmAutomatedDeployment\login.creds"
Connect-VIServer -Server $creds.Host -User $creds.User -Password $creds.Password |Out-Null

# define global variables
$init_cluster = 'virtualFrogLab'
$init_datastore = 'VF_ds_01'
$vm_folder = 'VM-Staging'
$current_date = $(Get-Date -format "dd.MM.yyyy HH:mm:ss")
$log_file = "C:\Scripts\VmAutomatedDeployment\log_$(Get-Date -format "yyyyMMdd").txt"
[int64]$vm_disk = 72
$dvPortGroup = Get-VDPortgroup -Name $vm_network

# check if var $VM_NAME already exists in the vCenter inventory

$CheckInventoryByVmName = Get-VM -Name $vm_name -ErrorAction Ignore

if ($CheckInventoryByVmName) {

    Write-Host "This virtual machine already exists!"

} else {

    # check the inputs for provisioning sizing of the virtual machine
    # allowed maximums: 2 vCPU / 8192MB vRAM / 100GB vDISK

    if ($vm_cpu -gt 2) {Write-Host "You input is invalid! (max. 2 vCPUs allowed)"}
    elseif ($vm_memory -gt 8192) {Write-Host "You input is invalid! (max. 8GB vRAM allowed)"}
    elseif ($vm_disk -gt 100) {Write-Host "You input is invalid! (max. 100GB vDisk size allowed)"}
    else {

        # create new virtual machine container
        # e.g.: .\VmAutomatedDeployment.ps1 -vm_name Test_A054108 -vm_guestid windows9_64Guest -vm_disk 72 -vm_memory 2048 -vm_cpu 2 -vm_network "Test 10.10.0.0"
        if ($vm_name -like "vm-t*")
        {
            $diskformat = "Thin"
            $init_cluster ="Test"
        }else {
            $diskformat = "Thick"
            $init_cluster = "Production"
        }
        $create_vm = New-VM -Name $vm_name -GuestId $vm_guestid -Location $vm_folder -ResourcePool $init_cluster -Datastore $init_datastore -DiskGB $vm_disk -DiskStorageFormat $diskformat -MemoryMB $vm_memory -NumCpu $vm_cpu -Portgroup $dvPortGroup -CD -Confirm:$false -ErrorAction SilentlyContinue

        # check if virtual machine exists
        if ($create_vm) {
            # change all network adapters to VMXNET3
            $change_vm_network = Get-VM -Name $create_vm | Get-NetworkAdapter | Set-NetworkAdapter -Type Vmxnet3 -Confirm:$false -ErrorAction SilentlyContinue

            # check if network adapter exists
            if ($change_vm_network) {
                Add-Content -Path $log_file -Value "$current_date     SCRIPT          $message"
                $macaddress = (get-vm -Name $create_vm |get-networkadapter).MacAddress
            } else {
                Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file"
            }
        } else {
            Write-Host "There was an unexpected error during the provisioning. For more information see log file: $log_file"

        }

    }

}

# cleanup and removal of loaded VMware modules
Disconnect-VIServer -Server $creds.Host -Confirm:$false
Remove-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue | Out-Null

# write all error messages to the log file
Add-Content -Path $log_file -Value $Error

#return the MAC address
return $macaddress

Passt ausserdem