I created a powershell module for using the Fog API
https://www.powershellgallery.com/packages/FogApi
Install instructions are found at that link.
You can also use powershellget https://www.powershellgallery.com/packages/PowerShellGet the command Install-Module -Name FogApi;
*
Importing that module will help you to set up a quick and easy and crossplatform way to manage fog from a powershell prompt.
It is structured based on the api documentation found here https://news.fogproject.org/simplified-api-documentation/
It even autocompletes the parameter options based on that documentation.
So if you read that documentation and see that you can get an ‘object’ you can then take that to the command by saying Get-FogObject -type object -CoreObject objecttype
that object type validates/autocompletes to the list of available core objects found in the documentation.
If you install the module and run help Invoke-FogApi
it will display a bit more verbose help and documentation on how it all works.
There are a few main functions to use that all make calling the Invoke-FogApi
function a bit easier with autocompletion fun times
- For GET api calls :
Get-FogObject
- For POST api calls :
New-FogObject
- For PUT api calls :
Update-FogObject
- For DELETE api calls :
Remove-FogObject
Each of these return powershell objects. If you’re unfamiliar with powershell and powershell objects, then this is a good way to learn.
They make it so you can take information and easily find and manipulate their properties.
i.e. if you did a $hosts = Get-FogObject - type Object -CoreObject host
$hosts
would contain 2 properties, a count of returned objects and an array of all your fog hosts, each with all the information fog has on them. So lets say you want to see all your hosts that have a intel cpu, you can search all the hosts for where the inventory’s cpu manufacturer has ‘intel’ in its value. $intelPCs = $hosts.hosts | ? { $_.inventory.cpuman -match 'intel' }
Then maybe you just want the hostids, names, and mac addresses. $intelPCList = $intelPCs | Select-Object id,name,primac; $intelPCList;
PS Objects can also easily be turned into json by piping them into a ConvertTo-Json
command. Meaning that you can just change the values of an object’s properties, such as a host’s name, image, etc. And then convert that to json to use as the jsondata in any other command.
I also included a Install-FogService function in the module for good measure that downloads the latest version of the client msi installer from your server and then silently installs it. In theory, you could use Invoke-Command to run that command on remote computers (though you would also have to import the module on each computer).
There is a settings.json file that the module pulls from to get your api keys and servername. It needs to be set manually, but automatically opens in an appropriate editor for your OS if it finds that the settings are still set to default. The default settings are explanations of where to find the values on your server.
Help Info from function code Will be updated overtime, putting here as it is the help info uri listed in module manifest
Invoke-FogApi
<#
.SYNOPSIS
a cmdlet function for making fogAPI calls via powershell
.DESCRIPTION
Takes a few parameters with some pulled from settings.json and others are put in from the wrapper cmdlets
Makes a call to the api of a fog server and returns the results of the call
The returned value is an object that can then be easily filtered, processed,
and otherwise manipulated in poweshell.
The defaults for each setting explain how to find or a description of the property needed.
fogApiToken = "fog API token found at https://fog-server/fog/management/index.php?node=about&sub=settings under API System";
fogUserToken = "your fog user api token found in the user settings https://fog-server/fog/management/index.php?node=user&sub=list select your api enabled used and view the api tab";
fogServer = "your fog server hostname or ip address to be used for created the url used in api calls default is fog-server or fogServer";
.PARAMETER serverSettings
this variable pulls the values from settings.json and assigns the values to
the associated params. The defaults explain how to get the needed settings
fogApiToken = "fog API token found at https://fog-server/fog/management/index.php?node=about&sub=settings under API System";
fogUserToken = "your fog user api token found in the user settings https://fog-server/fog/management/index.php?node=user&sub=list select your api enabled used and view the api tab";
fogServer = "your fog server hostname or ip address to be used for created the url used in api calls default is fog-server or fogServer";
.PARAMETER fogApiToken
a string of your fogApiToken gotten from the fog web ui.
this value is pulled from the settings.json file
.PARAMETER fogUserToken
a string of your fog user token gotten from the fog web ui in the user section.
this value is pulled from the settings.json file
.PARAMETER fogServer
The hostname or ip address of your fogserver,
defaults to the default name fog-server
this value is pulled from the settings.json file
.PARAMETER uriPath
Put in the path of the apicall that would follow http://fog-server/fog/
i.e. 'host/1234' would access the host with an id of 1234
This is filled by the wrapper commands using parameter validation to
help ensure using the proper object names for the url
.PARAMETER Method
Defaults to 'Get' can also be Post, put, or delete, this param is handled better
by the wrapper functions
get is Get-fogObject
post is New-fogObject
delete is Remove-fogObject
put is Update-fogObject
.PARAMETER jsonData
The jsondata string for including data in the body of a request
.EXAMPLE
#if you had the api tokens set as default values and wanted to get all hosts and info you could run this, assuming your fogserver is accessible on http://fog-server
Invoke-FogApi;
.Example
#if your fogserver was named rawr and you wanted to put rename host 123 to meow
Invoke-FogApi -fogServer "rawr" -uriPath "host/123" -Method "Put" -jsonData "{ `"name`": meow }";
.Link
https://news.fogproject.org/simplified-api-documentation/
.NOTES
The online version of this help takes you to the fog project api help page
#>