Automation

 View Only
Expand all | Collapse all

Couple of PowerShell gremlins..

  • 1.  Couple of PowerShell gremlins..

    Posted Nov 14, 2023 05:52 PM

    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



  • 2.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 06:15 PM

    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?



  • 3.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 06:19 PM

    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)



  • 4.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 06:47 PM

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

    $OriginalVMSettings = @()

      



  • 5.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 06:54 PM

    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



  • 6.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 07:02 PM

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



  • 7.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 07:10 PM

    This is what i am doing after re creating the sctipt

     



  • 8.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 07:19 PM

    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



  • 9.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 07:58 PM
      |   view attached

    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



  • 10.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 09:33 PM

    Looks like this line is missing

    $OriginalVMSettings = @()


  • 11.  RE: Couple of PowerShell gremlins..

    Posted Nov 15, 2023 10:10 AM

    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

    }



  • 12.  RE: Couple of PowerShell gremlins..
    Best Answer

    Posted Nov 15, 2023 12:23 PM

    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
    }
    

     



  • 13.  RE: Couple of PowerShell gremlins..

    Posted Nov 15, 2023 01:42 PM

    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 ... 



  • 14.  RE: Couple of PowerShell gremlins..

    Posted Nov 14, 2023 07:09 PM

     

    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