I am trying to get a small script together that will change the resources on a series of VM's on a hyper-v host. I need to change the CPU count and the Memory allocation to whatever the variables are imported from the CSV. However, when I try to pull the variable for CPU and Memory, I get a series of errors. But if I change those variables to a number, they work just fine. Any help is greatly appreciated.
$VMList = Import-csv "C:\temp\vmspecs_test.csv"
foreach-object {
$VMName = $VMList.Name
$CPUCount = "4"
$MaxMemory = 1GB*("4")
Stop-VM -Name $VMName -Force
Set-VMProcessor $VMName -Count $CPUCount
Set-VMMemory $VMName -DynamicMemoryEnabled $false -StartupBytes $MaxMemory
Start-VM -Name $VMName
}
However, if I change "$CPUCount = $VMList.NewCPU" (which is just a column in csv with a numeric 4 in it), I get the error
"Set-VMProcessor : Cannot convert 'System.Object[]' to the type 'System.Nullable`1[System.Int64]' required by parameter 'Count'.
Specified method is not supported.
At C:\Users\Administrator\Documents\VirtualMachineResourceModification.ps1:10 char:32
+ Set-VMProcessor $VMName -Count $CPUCount
+ ~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VMProcessor], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.HyperV.PowerShell.Commands.SetVMProcessor"
And if I change the value of $MaxMemory to $MaxMemory = 1GB*($VMList.MaxMem) OR [int64]$MaxMemory = 1GB*($VMList.MaxMem), it errors with
Method invocation failed because [System.Object[]] does not contain a method named 'op_Multiply'.
At C:\Users\Administrator\Documents\VirtualMachineResourceModification.ps1:8 char:1
+ $MaxMemory = 1GB*($VMList.MaxMem)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (op_Multiply:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
Can anyone please tell me what I am doing wrong? I have tried using a singular Set-VM option and ran into conversion of int32 and int64 errors. I have tried using a byte amount (no mathematical operator for memory, just 4294967296) and it still won't work from csv. I appreciate any help in advance.