Hi here you can find some examples using vsphere terraform modules
#===============================================================================
# vSphere Resources
#===============================================================================
# Create a vSphere VM in the folder #
resource "vsphere_virtual_machine" "VBR-PROXY" {
# VM placement #
count = "${var.vsphere_proxy_number}"
name = "${var.vsphere_vm_name}-W${random_integer.priority.result}-${count.index + 1}"
resource_pool_id = "${data.vsphere_resource_pool.resource_pool.id}"
datastore_id = "${data.vsphere_datastore.datastore.id}"
folder = "${var.vsphere_vm_folder}"
tags = ["${data.vsphere_tag.tag.id}"]
# VM resources #
num_cpus = "${var.vsphere_vcpu_number}"
memory = "${var.vsphere_memory_size}"
# Guest OS #
guest_id = "${data.vsphere_virtual_machine.template.guest_id}"
scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"
firmware = "${var.vsphere_vm_firmware}"
scsi_controller_count = "4"
# VM storage #
disk {
label = "${var.vsphere_vm_name}.vmdk"
size = "${data.vsphere_virtual_machine.template.disks.0.size}"
thin_provisioned = "${data.vsphere_virtual_machine.template.disks.0.thin_provisioned}"
eagerly_scrub = "${data.vsphere_virtual_machine.template.disks.0.eagerly_scrub}"
}
network_interface {
network_id = "${data.vsphere_network.network.id}"
adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}"
}
clone {
template_uuid = "${data.vsphere_virtual_machine.template.id}"
customize {
windows_options {
computer_name = "${var.vsphere_vm_name}-W${random_integer.priority.result}-${count.index + 1}"
admin_password = "${var.vsphere_vm_password}"
auto_logon = true
auto_logon_count = 1
run_once_command_list = [
"cmd.exe /C Powershell.exe -ExecutionPolicy Bypass -File c:\\first.ps1",
]
}
network_interface {
ipv4_address = "${var.vsphere_ipv4_address_proxy_network}${"${var.vsphere_ipv4_address_proxy_host}" + count.index}"
ipv4_netmask = "${var.vsphere_ipv4_netmask}"
}
ipv4_gateway = "${var.vsphere_ipv4_gateway}"
dns_server_list = ["${var.vsphere_dns_server1}", "${var.vsphere_dns_server2}"]}
}
}
Original Message:
Sent: Mar 22, 2024 09:07 AM
From: NoUserNamePlz
Subject: vSphere - Custom Spec - Run once - Powershell - Execute script with arguments
Hi,
I'd like to run scripts with args using the run once command.
This is working once the OS is deployed but will not work using the run once commands frome the custom spec in vSphere
cmd.exe /c powershell.exe -Executionpolicy bypass -command "& {c:\temp\Test.ps1 -Obj ([PSCustomObject]@{Field1=`$true;Field2='Valeur1';Field3='True'})}"
The script is just saving the fields of the pscustomobject into a file at the moment.
I cannot find documentation about this. Only examples using a simple file without args.
If someone struggled with it already and has a working solution, i'm in.
Kind regards.