Quantcast
Channel: VMware Communities : All Content - All Communities
Viewing all articles
Browse latest Browse all 179681

Interested in script critique

$
0
0

This script:

 

1.     Connects to VC entered by user.

2.     Reads virtual machine names from a csv.

3.     Sets vCPU, memory.

4.     Sets video memory (from function - Thanks Alan).

5.     Sets CPU/MMU to software.

6.     Powers on VM.

7.     Disconnects from VC.

 

Interested in any feedback on if this could be arranged differently or made more efficient.

 

Also, interested in how to get rid of this type feedback in the console during this line.

 

Set-VM -VM $vm.Name -NumCpu 2 -MemoryMB 4096 -confirm:$false -RunAsync

 

---Unwanted output to console---

 

Client                  :
CmdletTaskInfo          :
ExtensionData           :
Id                      :
IsCancelable            : False
Result                  :
Name                    :
Description             :
State                   : Running
PercentComplete         : 0
StartTime               : 4/30/2013 1:29:26 PM
FinishTime              :
ObjectId                :
Uid                     : /Local=/ClientSideTask=79a12755-2588-4699-8765-69c82c762224/
NonTerminatingErrorList : {}
TerminatingError        :

 

------Script------

 

. .\function.ps1

$curvc = read-host -prompt "Enter VC Name"
connect-viserver -server $curvc

$vms = Import-CSV .\name.csv

foreach ($vm in $vms){
Set-VM -VM $vm.Name -NumCpu 2 -MemoryMB 4096 -confirm:$false -RunAsync
Get-VM $vm.Name | Set-VMVideoMemory -MemoryMB 128 -Autodetect $false
Get-VM -Name $vm.Name | Get-View | foreach {
    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $spec.flags = New-Object VMware.Vim.VirtualMachineFlagInfo
    $spec.flags.VirtualMMuUsage = "off"
$spec.flags.virtualExecUsage = "hvOff"
    $taskMoRef = $_.ReconfigVM_Task($spec)
Start-VM -VM $vm.Name -Confirm:$false
}
}

disconnect-viserver * -confirm:$false

 

----Function----

 

function Set-VMVideoMemory {
<# .SYNOPSIS   Changes the video memory of a VM .DESCRIPTION   This function changes the video memory of a VM .NOTES  
#Source: http://virtu-al.net   Author: Alan Renouf   Version: 1.1 .PARAMETER VM   Specify the virtual machine .PARAMETER MemoryMB  
#Specify the memory size in MB .EXAMPLE   PS> Get-VM VM1 | Set-VMVideoMemory -MemoryMB 4 -AutoDetect $false
#>

  Param (
    [parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter a vm entity")]
    [VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl]$VM,
    [int64]$MemoryMB,
[bool]$AutoDetect,
[int]$NumDisplays
   )

  Process {
$VM | Foreach {
  $VideoAdapter = $_.ExtensionData.Config.Hardware.Device | Where {$_.GetType().Name -eq "VirtualMachineVideoCard"}
  $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  $Config = New-Object VMware.Vim.VirtualDeviceConfigSpec
  $Config.device = $VideoAdapter
  If ($MemoryMB) {
   $Config.device.videoRamSizeInKB = $MemoryMB * 1KB
  }
  If ($AutoDetect) {
   $Config.device.useAutoDetect = $true
  } Else {
   $Config.device.useAutoDetect = $false
  }
  Switch ($NumDisplays) {
   1{ $Config.device.numDisplays = 1}
   2{ $Config.device.numDisplays = 2}
   3{ $Config.device.numDisplays = 3}
   4{ $Config.device.numDisplays = 4}
   Default {}
  }
  $Config.operation = "edit"
  $spec.deviceChange += $Config
  $VMView = $_ | Get-View
  Write-Host "Setting Video Display for $($_)"
  $VMView.ReconfigVM($spec)
}
  }
}


Viewing all articles
Browse latest Browse all 179681

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>