通过PowerShell更改一些VM属性

时间:2016-07-31 08:43:41

标签: powershell virtual-machine hyper-v

我想根据CSV更改部分VM属性。
我在为VM分配内存时遇到问题。
我的代码有什么问题? 错误说:Set-VM没有找到任何匹配" MemoryAssigned"的参数。我在CSV中的内容叫做MmeoryGB。

Site Activity, Promotional Activity, Fisclal Week, Week, Date
MI Report Outage 14:30 - 15:30, 0, 18, 31,31/07/2016
03:00 - 09:00 - Level 0 Diverter Installation - SCS Loop, 0, 18, 31, 01/08/2016
03:00 - 09:00 - Level 1 Diverter Installation - SCS Loop, 0, 18, 31, 02/08/2016
etc etc

2 个答案:

答案 0 :(得分:1)

检查Show-Help Set-VM没有属性–MemoryAssigned,您应该使用以下组合:

-DynamicMemory
-MemoryMaximumBytes 
-MemoryMinimumBytes
-MemoryStartupBytes

答案 1 :(得分:0)

我已经更改了代码,这有效:

$VMProperties = Import-Csv $PathTocsv -Delimiter ";";


foreach ($Property in $VMProperties){
   # GB to Byte
   $MemoryByte = [Double]$MemoryGB*(1024*1024*1024)

   $NumberOfCPU = $Property.NumberOfCPU;
   $MemoryGB = $Property.MemoryGB;
   $MachineMacAddress = $Property.MachineMacAddress;


   Write-Host $NumberOfCPU;
   Write-Host "$MemoryGB GB";
   Write-Host $MachineMacAddress;

  $OldmacAddress = (Get-VM -Name $VMName | Get-VMNetworkAdapter).MacAddress
  Write-Host "-> OldMacAddress was $OldmacAddress";
  Get-VM -Name $VMName | Set-VMNetworkAdapter -StaticMacAddress  $MachineMacAddress
  $NewMacAddress = (Get-VM -Name $VMName | Get-VMNetworkAdapter).MacAddress
  Write-Host "-> NewMacAddress is $NewMacAddress";

  $OldProcessorCount = (Get-VM -Name $vmname).ProcessorCount
  $OldMemoryAssined = (Get-VM -Name $vmname).MemoryStartup
  Write-Host "--> OldProcessorCount was $OldProcessorCount";
  Write-Host "---> OldMemory was $OldMemoryAssined Byte";

  Set-VM -Name $VmName –MemoryStartupBytes $MemoryByte -ProcessorCount $NumberOfCPU -StaticMemory;
  $NewProcessorCount = (Get-VM -Name $vmname).ProcessorCount
  $NewMemory = (Get-VM -Name $vmname).MemoryStartup
  Write-Host "--> New ProcessorCount is $NewProcessorCount";
  Write-Host "---> NewMemory is $NewMemory Byte";

  }