FOG API add snapin, run task and then delete
-
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 -
Probably the best to ask about the API is @JJ-Fullmer
-
@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 outSet-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.
-
@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.
-
Made a fogapi git issue, so that I hopefully don’t forget about this idea https://github.com/darksidemilk/FogApi/issues/4
-
@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/