• Recent
  • Unsolved
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login
  • Recent
  • Unsolved
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login

FOG API add snapin, run task and then delete

Scheduled Pinned Locked Moved
FOG Problems
fogapi-psmodule api help api
3
6
927
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C
    Chris Whiteley
    last edited by JJ Fullmer Mar 31, 2023, 6:51 AM Mar 16, 2021, 9:07 PM

    Through the API, I am looking to be able to add a snapin task to a host and then run the task and then delete the snapin from the host all via powershell. Is there a way to do this? Here is what I have so far:

    
    #######################################################################################################################
    # Configure the variables below for the Fog Server
    #######################################################################################################################
        #FogServer
            $fogApiToken = 'ABCDE'
            $fogUserToken = 'FGHIJ'
            $fogServer = "192.168.1.100"
            $headers = @{};
            $headers.Add('fog-api-token', $fogApiToken);
            $headers.Add('fog-user-token', $fogUserToken);
            $baseUri = "http://$fogServer/fog"
           
           #Shows the Deploy Task ID
    
             $TasktypesURL = $baseUri + "/tasktype"
             $TasktypesResult = Invoke-RestMethod -Uri $TaskTypesURL -Method GET -Headers $headers -ContentType "application/json"
             $TasktypeID = ($TasktypesResult.tasktypes | Select-object name,id | Where-object {$_.name -eq "Single Snapin"}).id
    
    
           #Get Snapin List From Fog Server
    
            $SnapinURL = $baseUri+"/snapin/"
            $SnapinResult = Invoke-RestMethod -Uri $SnapinURL -Method GET -Headers $headers -ContentType "application/json"
            $Snapin = $SnapinResult.snapins | Select-object id,name | Where-object {$_.name -eq "Remote Settings"}
    
           #GetHost List from fog Server
    
            $HostURL = $baseUri +"/host/"
            $HostResult = Invoke-RestMethod -Uri $HostURL -Method GET -Headers $headers -ContentType "application/json"
    
           #Get Specific Host from FOG Server
            $HostURL = $baseUri +"/host/"
            $Results = Invoke-RestMethod -Uri $HostURL -Method GET -Headers $headers -ContentType "application/json" 
            $Hosts = $Results.hosts
            $SpecificHost = $Hosts | where name -match "Computer Name"
    
    
            #Start an Image
            $SnapinID = $Snapin.id
            $HostID = $specifichost.id  
    
            $TaskURL = $baseUri + "/host/" + $HostID + "/task"
            $TaskdataSet = @{
                "taskTypeID" = $TasktypeID
                "deploySnapins" = $Snapin
                }
            $taskdataToSend = ConvertTo-JSON($TaskdataSet)
            $TaskResult = Invoke-RestMethod -Method Post -Uri $TaskURL -Headers $headers -Body $taskdataToSend -ContentType "application/json"
    

    All it does is start an image. At this point, I am just trying to get it to deploy the single snapin. From everything I read, it is supposed to be tasktypeID 13. Any help would be greatly appreciated!

    FOG 1.5.9.34
    CentOS 7

    J 1 Reply Last reply Mar 18, 2021, 1:54 PM Reply Quote 1
    • S
      Sebastian Roth Moderator
      last edited by Mar 16, 2021, 9:13 PM

      Probably the best to ask about the API is @JJ-Fullmer

      Web GUI issue? Please check apache error (debian/ubuntu: /var/log/apache2/error.log, centos/fedora/rhel: /var/log/httpd/error_log) and php-fpm log (/var/log/php*-fpm.log)

      Please support FOG if you like it: https://wiki.fogproject.org/wiki/index.php/Support_FOG

      J 1 Reply Last reply Mar 18, 2021, 1:45 PM Reply Quote 1
      • J
        JJ Fullmer Testers @Sebastian Roth
        last edited by Mar 18, 2021, 1:45 PM

        @sebastian-roth Hey that’s me!

        @Chris-Whiteley I do this all the time. So 100% yes you can do this. I even have some pre-built helper functions for it in the published powershell module FogApi.
        So step one is install and setup the fogapi powershell module (check the links in my signature)

        I actually do pretty much that exact process in my provisioning of machines.
        Check out

        • Set-fogsnapins for setting a list of snapins to a host https://fogapi.readthedocs.io/en/latest/commands/Set-FogSnapins/
          (I just noticed I forgot to remove the $dept parameter in that function, that was for my internal use, you can just ignore it)
        • Start-FogSnapins for starting the deploy all snapins task on a host
          https://fogapi.readthedocs.io/en/latest/commands/Start-FogSnapins/
        • Remove-FogObject for removing the snapins from a host, looks like I haven’t published a helper function for removing the snapins just yet.
          https://fogapi.readthedocs.io/en/latest/commands/Remove-FogObject/
          • here’s an example of how you would remove the snapins. To remove them you have to remove the snapinassociation object.
        #create a list/array of the snapins you want to remove
        $snapinsToRemove = @('snapin','names','here'); 
        
        #get all the snapin associaion objects
        $AllAssocs = (Get-FogObject -type object -coreObject snapinassociation).snapinassociations
        
        # Get the snapins associated with your host
        $AllHostSnapins = Get-FogAssociatedSnapins -hostId $hostID;
        
        #get the ids of the snapins you want to remove, from the list of snapins attached to your host.
        $snapinIds = ($AllHostSnapins | Where-Object name -in $snapinsToRemove).id
        
        #Get a list of the snapinassociation ids that match your host id and are in the list of snapin ids attached to your host
        $assocsToRemove = $allAssocs | Where-Object { $_.hostID -eq $hostID -AND $_.snapinID -in $snapinIds}
        
        # loop through the found associations and remove them
        $assocsToRemove | ForEach-Object {
            Remove-FogObject -type object -coreObject snapinassociation -IDofObject $_.id;
        }
        

        That should do the trick.

        Have you tried the FogApi powershell module? It's pretty cool IMHO
        https://github.com/darksidemilk/FogApi
        https://fogapi.readthedocs.io/en/latest/
        https://www.powershellgallery.com/packages/FogApi
        https://forums.fogproject.org/topic/12026/powershell-api-module

        1 Reply Last reply Reply Quote 2
        • J
          JJ Fullmer Testers @Chris Whiteley
          last edited by Mar 18, 2021, 1:54 PM

          @chris-whiteley I just re-read your post and realized you want to deploy a single snapin task, not the all snapins task.

          I’d have to test it but I believe what you want is

          $json = (@{
            "taskTypeID"='13';
            "deploySnapins"='snapinName';
          } | ConvertTo-Json);
          New-FogObject -type objecttasktype -coreTaskObject host -jsonData $json -IDofObject $hostid;
          

          When doing the all snapins taks the tasktypeid is 12 and you set the deploySnapins to ‘-1’. @Tom-Elliott can help with confirming the syntax of the json required for a single snapin. But if your using my powershell module, then the New-FogObject command will take care of creating the task.

          If you look at the code for start-fogsnapins here https://github.com/darksidemilk/FogApi/blob/master/FogApi/Public/Start-FogSnapins.ps1 you’ll note that I also have some checks for existing snapin tasks on the host. You’d probably want to do something like that. I should probably make it so that little segment is its own function, like Test-SnapinTasks and should probably make a separate start-fogsnapin function for single snapin tasks. But hey you could always do a pull request if you want to give writing it a try. I don’t know when I’ll have time to do all that myself.

          Have you tried the FogApi powershell module? It's pretty cool IMHO
          https://github.com/darksidemilk/FogApi
          https://fogapi.readthedocs.io/en/latest/
          https://www.powershellgallery.com/packages/FogApi
          https://forums.fogproject.org/topic/12026/powershell-api-module

          J 1 Reply Last reply Mar 18, 2021, 2:12 PM Reply Quote 1
          • J
            JJ Fullmer Testers @JJ Fullmer
            last edited by Mar 18, 2021, 2:12 PM

            Made a fogapi git issue, so that I hopefully don’t forget about this idea https://github.com/darksidemilk/FogApi/issues/4

            Have you tried the FogApi powershell module? It's pretty cool IMHO
            https://github.com/darksidemilk/FogApi
            https://fogapi.readthedocs.io/en/latest/
            https://www.powershellgallery.com/packages/FogApi
            https://forums.fogproject.org/topic/12026/powershell-api-module

            J 1 Reply Last reply Aug 23, 2022, 4:28 PM Reply Quote 1
            • J
              JJ Fullmer Testers @JJ Fullmer
              last edited by Aug 23, 2022, 4:28 PM

              @Chris-Whiteley I finally had some time to work on the module and created a Start-FogSnapin function that can deploy a single snapin task.

              See also https://fogapi.readthedocs.io/en/latest/commands/Start-FogSnapin/

              Have you tried the FogApi powershell module? It's pretty cool IMHO
              https://github.com/darksidemilk/FogApi
              https://fogapi.readthedocs.io/en/latest/
              https://www.powershellgallery.com/packages/FogApi
              https://forums.fogproject.org/topic/12026/powershell-api-module

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post

              146

              Online

              12.0k

              Users

              17.3k

              Topics

              155.2k

              Posts
              Copyright © 2012-2024 FOG Project