Update-FogObject / Powershell module use



  • Not sure if this is the correct place for this post but I am using the PowerShell module created by: JJ Fullmer
    https://github.com/FOGProject/fog-community-scripts/tree/master/PowershellModules/FogApi

    Can someone look over this code for me? Trying to move hosts from one group to another:

    Import-Module FogApi
    
    $Groupnames = Get-FogObject -type object -coreObject group
    $groupassoc = (Get-FogObject -type object -coreObject groupassociation).groupassociations
    $queue = $Groupnames.groups | where {$_.Name -eq "Queued"}
    $Hostnames = (Get-FogObject -type object -coreObject host).hosts
    $sys_queue = $groupassoc | where {$_.groupID -eq $queue.id}
    $free_sys = $groupassoc  | where {$_.groupID -eq "0"}
    
    if($free_sys.Count -gt 0){
        Write-host I found $free_sys.count hosts to move to Queued.
        foreach($freehost in $free_sys){
        #command to move $free_sys to queued group.
        $HostJson = (@{hosts = $free_sys.ID} | ConvertTo-Json)
        write-host Update-FogObject ...
        Update-FogObject -type object -coreObject group -jsonData $HostJson -IDofObject $queue.ID
        
        }
        
    }
    

    output:

    Update-FogObject ...
    Invoke-RestMethod : The remote server returned an error: (501) Not Implemented.
    At C:\Program Files\WindowsPowerShell\Modules\FogApi\FogApi.psm1:270 char:19
    +         $result = Invoke-RestMethod @apiCall;
    +                   ~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
     
    
    PS C:\Users\Administrator> $HostJson
    {
        "hosts":  [
                      "34",
                      "35",
                      "36",
                      "37",
                      "38",
                      "39",
                      "40",
                      "41",
                      "42",
                      "43"
                  ]
    }
    
    PS C:\Users\Administrator> $queue.ID
    35
    


  • @JJ-Fullmer I love your PS module, great work! I am still hammering away on things. and I will let you know if I run across anything else like this. I have been able to make a neat script in less than 200 lines that does a lot based off of a CSV input file.



  • Starting in Windows PowerShell 3.0, installed modules are automatically imported to the session when you use any commands or providers in the module. … A module is a package that contains members that can be used in PowerShell. Members include cmdlets, providers, scripts, functions, variables, and other tools and files. Subway Surfers Psiphon Hill Climb Racing


  • Testers

    @mparlette Also, also, thank you for using my module! I’m glad to see that others are finding use of it!
    Also also also, another failsafe is to just run commands through invoke-fogapi if the other functions aren’t working. It requires a bit more understanding of the uri paths and using the api. But all the other functions are really just helper functions that make it easier to call that one master function. It’s also the function that actually has some info in it if you run help invoke-fogapi -full


  • Testers

    @mparlette also, ps. If you run that same code with a -verbose on update-fogobject you’ll get a lot more information.

    Another quick update is that I’m also working on a better settings system where the module’s saved settings are stored in the fog service program file folder so that they aren’t overridden when you update the module.

    All of these updates may take a while to get done and published as I have a lot of other projects at the moment and I tend to put my work in an internal git repo and just publish stable things to the fog community git and psgallery.

    If you’re in a bit of a rush and are ok with beta code to play with, I can zip up my current working version and post it here


  • Testers

    @mparlette I just saw this and just today was finding some issue with Update-fogobject
    The issue appears to be with the dynamic parameter variable I have in the function for the coreobjecttype.
    For some reason it is working when you call the function and brings up all the coreobject type choices but then the variable is being set to null when the function is running.
    Meaning that when function builds the uri it only gets
    http://fogserver/fog//id/edit
    instead of
    http://fogserver/fog/coreObjectType/id/edit

    So one workaround I will try to publish by the end of the day is adding an optional uri parameter to that function so that you can manually override it when neccesarry.
    Also I should really add more documentation to each of the functions instead of just having it all under Invoke-fogapi

    I also will add a try/catch block to invoke-fogapi for when invoke-restmethod fails and have it try invoke-webrequest.


  • Developer

    @mparlette Nice you figured this one out! I have had a quick look when you first posted but didn’t seem to be able to say what’s wrong. Maybe you are keen to find out why Update-FogObject PS module is failing and help us improve it?



  • I have this figured out. IT seems like the Update-FogObject PS module isn’t handling this correctly (Content type issue?). Just used Invoke-RestMethod instead.

    Please resolve this.
    Thanks.


 

549
Online

5.4k
Users

12.6k
Topics

118.9k
Posts