frhling1 frhling1 - 4 months ago 17
PowerShell Question

Change some VM properties via PowerShell

I want to change some of my VM Properties according to a CSV.

I have problem with Assigning memory to my VM.

What is wrong in my code?
The Error says: Set-VM does not find any Paramether that matches "MemoryAssigned". what I have in my CSV ist called MmeoryGB.

$VMProperties = Import-Csv $PathTocsv -Delimiter ";";
foreach ($Property in $VMProperties){
$NumberOfCPU = $Property.NumberOfCPU;
$MemoryGB = $Property.MemoryGB;
$MachineMacAddress = $Property.MachineMacAddress;

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

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

$OldProcessorCount = (Get-VM -Name $vmname).ProcessorCount
$OldMemoryAssined = (Get-VM -Name $vmname).MemoryAssigned
Write-Host "OldProcessorCount is $OldProcessorCount";
Write-Host "OldMemoryAssined is $OldMemoryAssined";
Write-Host "OldProcessorCount is $NewProcessorCount";
Set-VM –MemoryAssigned $MemoryGB -ProcessorCount $NumberOfCPU;
$NewProcessorCount = (Get-VM -Name $vmname).ProcessorCount
$NewMemoryAssined = (Get-VM -Name $vmname).MemoryAssigned

}

Start-VM -Name $VMName

Answer

I have changed the code and this works:

$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";

  }