VMware Cloud Community
kopper27
Hot Shot
Hot Shot

Set some Vms parameters

hi guys

Right now I am using this powercli script to create VMs the information in taken from cvs file and creates VMs. (script taken from here)

$worksheet = Import-CSV "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Win7.csv"
$worksheet | ForEach-Object {
      new-VM `
          -Name $_.VM_Name `
          -VMHost $_.Host `
          -Template $_.Template `
          -Datastore $_.Datastore `
          -OSCustomizationspec $_.Customization `
          -Location $_.Location `
          -DiskStorageFormat $_.DiskStorageFormat |`
          Set-VM -NumCpu $_.CPU -MemoryMB $_.Memory -Confirm:$false
          Start-VM -VM $_.VM_Name
         }

cvs form

Name,Template,Host,Datastore,Customization,Location,DiskStorageFormat,Memory,CPU

that works OK

1. now I need to set some other parameters but I dont know if it's possible using this script

I need to Set

IP
Subnet Mask
Gateway
Primey DNS
Secondary DNS

doing it the same way using this cvs o a new one no matter.

2. and the last question very silly one in the cvs above I have some VMs which Folder path is like this

Admin VMs

-> Windows Test

-> Windows Deployment

I am now able to set the location of those new VMs when

location = Admin VMs\Windows Test

should I use " ' ` to group that variable?

I get the error "Could not finf folder with 'folder_path'"

thanks a lot guys

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership

1. You can set the network configuration in the OSCustomization spec with the New-OSCustomizationNicMapping and Set-OSCustomizationNicMapping cmdlets.

2. I'm afraid you can't use complex folder paths for the Location parameter.

But you can solve it like this

$folders = @($_.Location.Split('\'))

if($folders.Count -gt 1){

     0..$folders.Count | %{

          if($_ -eq 0){

               $location = Get-Folder -Name $folders[$_]

          }

          else{

               $location = Get-Folder -Name $folders[$_] -Location $location

     }    

}

else{

  $location = Get-Folder -Name $folders[0]

}

In the New-VM cmdlet you can now do

New-VM ... -Location $location ...


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
kopper27
Hot Shot
Hot Shot

thanks LucD

I am trying this to set IP....

$worksheet = Import-CSV "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\NewVMs_v2.csv"
$worksheet | ForEach-Object {
      new-VM `
          -Name $_.VM_Name `
          -VMHost $_.Host `
          -Template $_.Template `
          -Datastore $_.Datastore `
          -OSCustomizationspec $_.Customization `
          -Location $_.Location `
          -DiskStorageFormat $_.DiskStorageFormat |`
      Set-VM -NumCpu $_.CPU -MemoryMB $_.Memory -Confirm:$false
      Start-VM -VM $_.VM_Name
      New-HardDisk -VM $_.VM_Name -CapacityKB $_.New_HDD
      Get-OSCustomizationSpec -OSCustomizationSpec $_.Customization | Set-OSCustomizationNicMapping -IpMode UseStaticIP `

      -IpAddress 10.6.26.12 -SubnetMask 255.255.255.0 -DefaultGateway 10.6.26.1 -Dns 10.6.25.53
      }

but I am getting this

Get-OSCustomizationSpec : A parameter cannot be found that matches parameter na
me 'OSCustomizationSpec'.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\BatchVMs.ps1:14 char
:48
+       Get-OSCustomizationSpec -OSCustomizationSpec <<<<  $_.Customization | S
et-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 10.6.26.12 `
    + CategoryInfo          : InvalidArgument: (:) [Get-OSCustomizationSpec],
   ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,VMware.VimAutomation.ViCo
   re.Cmdlets.Commands.GetOSCustomizationSpec

any idea thanks a lot

Reply
0 Kudos
GrantOrchardVMw
Commander
Commander

Try this:

Get-OSCustomizationSpec -Name $_.Customization | Set-OSCustomizationNicMapping `

-IpMode UseStaticIP -IpAddress 10.6.26.12 -SubnetMask 255.255.255.0 `

-DefaultGateway 10.6.26.1 -Dns 10.6.25.53

EDIT: Also, you'll want to define your OSCustomizationSpec *before* you call "New-VM".

Grant

Grant http://grantorchard.com
Reply
0 Kudos
kopper27
Hot Shot
Hot Shot

Now I get this

I just saw your Edit update.I thunk thats the point I don't get define your OSCustomizationSpec *before* you call "New-VM".

I have to create a new one?

just to let you

Profile_Win2008_R2_x64 is the customization I use to create the templates in this case Windows 2008

Set-OSCustomizationNicMapping : The input object cannot be bound to any paramet

ers for the command either because the command does not take pipeline input or

the input and its properties do not match any of the parameters that take pipel

ine input.

At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\BatchVMs.ps1:14 char

:82

+       Get-OSCustomizationSpec -Name $_.Customization | Set-OSCustomizationNic

Mapping <<<<  -IpMode UseStaticIP -IpAddress 10.6.26.12 `

    + CategoryInfo          : InvalidArgument: (Profile_Win2008_R2_x64:PSObjec

   t) [Set-OSCustomizationNicMapping], ParameterBindingException

    + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.

   Cmdlets.Commands.SetOSCustomizationNicMapping

just to let you
Profile_Win2008_R2_x64 is the customization I use to create the templates in this case Windows 2008

Set-OSCustomizationNicMapping : The input object cannot be bound to any paramet
ers for the command either because the command does not take pipeline input or
the input and its properties do not match any of the parameters that take pipel
ine input.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\BatchVMs.ps1:14 char
:82
+       Get-OSCustomizationSpec -Name $_.Customization | Set-OSCustomizationNic
Mapping <<<<  -IpMode UseStaticIP -IpAddress 10.6.26.12 `
    + CategoryInfo          : InvalidArgument: (Profile_Win2008_R2_x64:PSObjec
   t) [Set-OSCustomizationNicMapping], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.
   Cmdlets.Commands.SetOSCustomizationNicMapping

Reply
0 Kudos
GrantOrchardVMw
Commander
Commander

Apologies, that wasn't particularly clear. Here's what I was suggesting you do:

$worksheet = Import-CSV "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\NewVMs_v2.csv"
$worksheet | ForEach-Object {
     Get-OSCustomizationSpec -Name $_.Customization | Get-OSCustomizationNicMapping `
     | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress 10.6.26.12 `
     -SubnetMask 255.255.255.0 -DefaultGateway 10.6.26.1 -Dns 10.6.25.53
      new-VM `
          -Name $_.VM_Name `
          -VMHost $_.Host `
          -Template $_.Template `
          -Datastore $_.Datastore `
          -OSCustomizationspec $_.Customization `
          -Location $_.Location `
          -DiskStorageFormat $_.DiskStorageFormat |`
      Set-VM -NumCpu $_.CPU -MemoryMB $_.Memory -Confirm:$false
      Start-VM -VM $_.VM_Name
      New-HardDisk -VM $_.VM_Name -CapacityKB $_.New_HDD
     }

Let me know how you go.

Grant

Grant http://grantorchard.com
Reply
0 Kudos
kopper27
Hot Shot
Hot Shot

Now is working just fine

the first time I run it it complain about this

WARNING: 'Description' property is obsolete. Use 'Notes' instead.
WARNING: 'HardDisks' property is obsolete. Use 'Get-HardDisk' cmdlet instead.
WARNING: 'NetworkAdapters' property is obsolete. Use 'Get-NetworkAdapter' cmdlet instead.
WARNING: 'UsbDevices' property is obsolete. Use 'Get-UsbDevice' cmdlet instead.
WARNING: 'CDDrives' property is obsolete. Use 'Get-CDDrive' cmdlet instead.
WARNING: 'FloppyDrives' property is obsolete. Use 'Get-FloppyDrive' cmdlet instead.
WARNING: 'Host' property is obsolete. Use 'VMHost' instead.
WARNING: 'HostId' property is obsolete. Use 'VMHostId' instead.

But now runs without problems

thanks a lot

Reply
0 Kudos