VMware Cloud Community
Paulk99
Enthusiast
Enthusiast
Jump to solution

Couple of PowerShell gremlins..

Hi All

Can someone point me in the correct direction re-the below if poss i thought it was good. 

$OriginalVMSettings = @()

$AllLSVMs = G et-VM -Name "LAB_VM" | where {$_.ExtensionData.Config.LatencySensitivity.Level -eq "normal"}

$AllLSVMs

foreach ($LSVM in $AllLSVMs)

{

  $OriginalVMSettings += [PSCustomObject]@{VMName = $LSVM.Name;

                                             CpuReservationMhz = $LSVM.ExtensionData.Config.CpuAllocation.Reservation;

                                             CpuLimitMhz = $LSVM.ExtensionData.Config.CpuAllocation.Limit;

                                             MemReservationMB = $LSVM.ExtensionData.Config.MemoryAllocation.Reservation;

                                             MemLimitMB = $LSVM.ExtensionData.Config.MemoryAllocation.Limit

                                            }

}

$OriginalVMSettings | ConvertTo-Json -Depth 100 | Out-File ".\LSVMOriginalSettings.json"

The JSON file is created, but when opened in Notepad looks to be empty tho....

The Error points to an Array issue but i cant quite see it

Any Thoughts... ...! 🙂

Kr

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

To get the 'Unlimited' limit you have to provide a $null value.
You can do that with a simple if-then-else.

Also, by using splatting the code becomes much more readable and you can limit the number of calls to the Set-VMResourceConfig cmdlet to 1.

Something like this

$OriginalVMSettings = Get-Content -Raw -Path ".\LSVMOriginalSettings.json" | ConvertFrom-Json
foreach ($LSVM in $OriginalVMSettings)
{
    Write-Output $LSVM.Name
    $CurrentVM = Get-VM -Name $LSVM.VMName
    $CurrentVMResConfig = $CurrentVM | Get-VMResourceConfiguration
    Write-Output $CurrentVMResConfig
    $sConfig = @{
      CpuReservationMhz = $LSVM.CPUReservationMHz
      CpuLimitMhz = if ($LSVM.CpuLimitMhz -eq -1) { $null }else { $LSVM.CpuLimitMhz }
      MemReservationMB = $LSVM.MemReservationMB
      MemLimitMB = if ($LSVM.MemLimitMB -eq -1) { $null }else { $LSVM.MemLimitMB }
    }
    $CurrentVMResConfig | Set-VMResourceConfiguration @sConfig

    Write-Output "After restoration..."
    $CurrentVM | Get-VMResourceConfiguration
}

 


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

View solution in original post

13 Replies
LucD
Leadership
Leadership
Jump to solution

Seems to work for me, it creates a JSON file with content.

Which error are you referring to?
Can you show the full error message?


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

Reply
0 Kudos
Paulk99
Enthusiast
Enthusiast
Jump to solution

sure .

I dont have copy paste from the remote system.

But

Method Invocation Failed because [System.Management.Automation.PSObject] does not contain a method named `OP_Addition`

(Please bear in mind that due to access issues ive needed to re-type this code out on the the server so i could of overlooked a typo)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The error seems to indicate that $OriginalVMSettings is not an array.
Looks like something went wrong with the following line

$OriginalVMSettings = @()

  


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

Reply
0 Kudos
Paulk99
Enthusiast
Enthusiast
Jump to solution

Hiya yeah thats kind of where i was too but it seemed ok (i thought)

 

The format of than line is ok isnt it without any typos

v Odd as ive had this script working elsewhere nyself

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can't you store the code in a .ps1 file on the remote system, and run it from the .ps1 file?


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

Reply
0 Kudos
Paulk99
Enthusiast
Enthusiast
Jump to solution

 

Odd..

If i remove the + as below

$OriginalVMSettings = [PSCustomObject]@{VMName = $LSVM.Name;

Now the script turns over without warning however still does not write to file as expected

Reply
0 Kudos
Paulk99
Enthusiast
Enthusiast
Jump to solution

This is what i am doing after re creating the sctipt

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Taking away that '+' sign is just creating the PSObject, not adding the PSObject to an array.

You will have to check that .ps1 file line by line, there must be a typo or something missing in there.
I would need to see the actual content you have on the remote server


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

Reply
0 Kudos
Paulk99
Enthusiast
Enthusiast
Jump to solution

Sure

My eyes are tired and im frustrated with this so i could well be overlooking something stupid.

Anyways please see uploaded direct from the server

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like this line is missing

$OriginalVMSettings = @()


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

Reply
0 Kudos
Paulk99
Enthusiast
Enthusiast
Jump to solution

Hiya Luc

No that was there just not in the snippy.

On a fresh day with fresh eyes i spotted one typo which was of course it..

I knew this code worked on the previous environment however now for the real reason i wanted to post here in the first place.

Having exported to JSON for the Values

CPUReservation

CPULimits

MemReservation

Mem Limits

I want to be reapplying the settings from the JSON back up to the original VM

-- My code changes the CPU\Mem Reservations just fine but fails to set the value back for Limits.

-(Where the value should be "Unlimited and in the JSON file it is shown to be as -1)

Code is below.

$OriginalVMSettings = Get-Content -Raw -Path ".\LSVMOriginalSettings.json" | ConvertFrom-Json

foreach ($LSVM in $OriginalVMSettings)

{

    Write-Output $LSVM.Name

    $CurrentVM = Get-VM -Name $LSVM.VMName

    $CurrentVMResConfig = $CurrentVM | Get-VMResourceConfiguration

    Write-Output $CurrentVMResConfig

    $CurrentVMResConfig | Set-VMResourceConfiguration -CpuReservationMhz $LSVM.CPUReservationMHz `

   $CurrentVMResConfig | Set-VMResourceConfiguration -CpuLimitMhz $LSVM.CPULimitMhz `

   $CurrentVMResConfig | Set-VMResourceConfiguration -MemReservationMB $LSVM.MemReservationMB `

   $CurrentVMResConfig | Set-VMResourceConfiguration -MemLimitMB $LSVM.MemLimitMB

    Write-Output "After restoration..."

    $CurrentVM | Get-VMResourceConfiguration

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To get the 'Unlimited' limit you have to provide a $null value.
You can do that with a simple if-then-else.

Also, by using splatting the code becomes much more readable and you can limit the number of calls to the Set-VMResourceConfig cmdlet to 1.

Something like this

$OriginalVMSettings = Get-Content -Raw -Path ".\LSVMOriginalSettings.json" | ConvertFrom-Json
foreach ($LSVM in $OriginalVMSettings)
{
    Write-Output $LSVM.Name
    $CurrentVM = Get-VM -Name $LSVM.VMName
    $CurrentVMResConfig = $CurrentVM | Get-VMResourceConfiguration
    Write-Output $CurrentVMResConfig
    $sConfig = @{
      CpuReservationMhz = $LSVM.CPUReservationMHz
      CpuLimitMhz = if ($LSVM.CpuLimitMhz -eq -1) { $null }else { $LSVM.CpuLimitMhz }
      MemReservationMB = $LSVM.MemReservationMB
      MemLimitMB = if ($LSVM.MemLimitMB -eq -1) { $null }else { $LSVM.MemLimitMB }
    }
    $CurrentVMResConfig | Set-VMResourceConfiguration @sConfig

    Write-Output "After restoration..."
    $CurrentVM | Get-VMResourceConfiguration
}

 


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

Paulk99
Enthusiast
Enthusiast
Jump to solution

Hiya Luc

Bang on first time round...!

Sir that is a sweet section of code.

Many thanks for your help my college and I were really a bit stuck on that one ... 

Reply
0 Kudos