FogApi.psm1
<#
----------------------------------------------------------------------------- Script Name: FogApi Original Author: JJ Fullmer Original Created Date: 2018-06-04 Version: 1.6 ----------------------------------------------------------------------------- #> function Get-DynmicParam { [CmdletBinding()] param ( [Parameter(Position=0)] [ValidateSet('coreObject','coreTaskObject','coreActiveTaskObject','stringToSearch')] [string]$paramName, $position=1 ) begin { #initilzie objects $attributes = New-Object Parameter; #System.Management.Automation.ParameterAttribute; $attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute] # $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary # Set attributes $attributes.Position = $position; $attributes.Mandatory = $true; $attributeCollection.Add($attributes) $coreObjects = @( "clientupdater", "dircleaner", "greenfog", "group", "groupassociation", "history", "hookevent", "host", "hostautologout", "hostscreensetting", "image", "imageassociation", "imagepartitiontype", "imagetype", "imaginglog", "inventory", "ipxe", "keysequence", "macaddressassociation", "module", "moduleassociation", "multicastsession", "multicastsessionsassociation", "nodefailure", "notifyevent", "os", "oui", "plugin", "powermanagement", "printer", "printerassociation", "pxemenuoptions", "scheduledtask", "service", "snapin", "snapinassociation", "snapingroupassociation", "snapinjob", "snapintask", "storagegroup", "storagenode", "task", "tasklog", "taskstate", "tasktype", "usercleanup", "usertracking", "virus" ); $coreTaskObjects = @("group", "host", "multicastsession", "snapinjob", "snapintask", "task"); $coreActiveTaskObjects = @("multicastsession", "scheduledtask", "snapinjob", "snapintask", "task"); } process { switch ($paramName) { coreObject { $attributeCollection.Add((New-Object ValidateSet($coreObjects)));} coreTaskObject {$attributeCollection.Add((New-Object ValidateSet($coreTaskObjects)));} coreActiveTaskObject {$attributeCollection.Add((New-Object ValidateSet($coreActiveTaskObjects)));} } $dynParam = New-Object System.Management.Automation.RuntimeDefinedParameter($paramName, [string], $attributeCollection); # $paramDictionary.Add($paramName, $dynParam); } end { return $dynParam; } } function Set-DynamicParams { [CmdletBinding()] param ($type) $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary; # Sub function for setting function Set-Param($paramName) { $param = Get-DynmicParam -paramName $paramName; $paramDictionary.Add($paramName, $param); } switch ($type) { object { Set-Param('coreObject');} objectactivetasktype { Set-Param('coreActiveTaskObject');} objecttasktype {Set-Param('coreTaskObject');} search {Set-Param('stringToSearch');} } return $paramDictionary; } function Get-FogServerSettings { [CmdletBinding()] param () begin { Write-Verbose "Pulling settings from settings file" $settingsFile = "$PSScriptRoot\settings.json"; } process { $serverSettings = (Get-Content $settingsFile | ConvertFrom-Json); } end { return $serverSettings; } } function Set-FogServerSettings { [CmdletBinding()] param ( [string]$fogApiToken, [string]$fogUserToken, [string]$fogServer, [switch]$interactive = $false ) begin { $settingsFile = "$PSScriptRoot\settings.json"; $serverSettings = Get-FogServerSettings; Write-Verbose "Current/old Settings are $($serverSettings)"; $helpTxt = @{ 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"; } } process { # If given paras are null just pulls from settings file # If they are not null sets the object to passed value $serverSettings.psobject.properties | % { $var = (Get-Variable -Name $_.Name); if ($var.Value -eq $null -OR $var.Value -eq "") { if($interactive) { Set-Variable -name $var.Name -Value (Read-Host -Prompt "Enter the $($var.name), help message: $($helpTxt.($_.name)) "); } else { Write-Verbose "Not interactive and no $($var.Name) specificed, pulling from settings file"; Set-Variable -name $var.Name -Value ($serverSettings.$($_.Name)); } } Set-Variable -name $serverSettings.$($_.Name) -Value $var.Value; } Write-Verbose "making sure all settings are set"; if ( $fogApiToken -eq $helpTxt.fogApiToken -OR $fogUserToken -eq $helpTxt.fogUserToken -OR $fogServer -eq $helpTxt.fogServer) { Write-Host -BackgroundColor Yellow -ForegroundColor Red -Object "a fog setting is still set to its default help text, opening the settings file for you to set the settings" Write-Host -BackgroundColor Yellow -ForegroundColor Red -Object "This script will close after opening settings in notepad, please re-run command after updating settings file"; if ($isLinux) { $editor = 'nano'; } elseif($IsMacOS) { $editor = 'TextEdit'; } else { $editor = 'notepad.exe'; } Start-Process -FilePath $editor -ArgumentList "$SettingsFile" -NoNewWindow -PassThru; return; } } end { Write-Verbose "Writing new Settings"; $serverSettings | ConvertTo-Json | Out-File -FilePath $settingsFile -Encoding oem -Force; return; } } function 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 #> [CmdletBinding()] param ( [string]$uriPath, [string]$Method, [string]$jsonData ) begin { Write-Verbose "Pulling settings from settings file" Set-FogServerSettings; $serverSettings = Get-FogServerSettings; [string]$fogApiToken = ($serverSettings.fogApiToken); [string]$fogUserToken = ($serverSettings.fogUserToken); [string]$fogServer = ($serverSettings.fogServer); $baseUri = "http://$fogServer/fog"; # Create headers Write-Verbose "Building Headers..."; $headers = @{}; $headers.Add('fog-api-token', $fogApiToken); $headers.Add('fog-user-token', $fogUserToken); # Set the Uri Write-Verbose "Building api call URI..."; $uri = "$baseUri/$uriPath"; $apiCall = @{ Uri = $uri; Method = $Method; Headers = $headers; Body = $jsonData; ContentType = "application/json" } if ($apiCall.Body -eq $null -OR $apiCall.Body -eq "") { Write-Verbose "removing body from call as it is null" $apiCall.Remove("Body"); } } process { Write-Verbose "$Method`ing $jsonData to/from $uri"; $result = Invoke-RestMethod @apiCall; } end { Write-Verbose "finished api call"; return $result; } } function Get-FogObject { [CmdletBinding()] param ( # The type of object being requested [Parameter(Position=0)] [ValidateSet("objectactivetasktype","object","search")] [string]$type, # The json data for the body of the request [Parameter(Position=2)] [Object]$jsonData, # The id of the object to get [Parameter(Position=3)] [string]$IDofObject ) DynamicParam { $paramDict = Set-DynamicParams $type; return $paramDict;} begin { $paramDict | % { New-Variable -Name $_.Keys -Value $($_.Values.Value);} Write-Verbose "Building uri and api call for $($paramDict.keys) $($paramDict.values.value)"; switch ($type) { objectactivetasktype { $uri = "$coreActiveTaskObject/current"; } object { if($IDofObject -eq $null -OR $IDofObject -eq "") { $uri = "$coreObject"; } else { $uri = "$coreObject)/$IDofObject"; } } search { $uri = "$type/$stringToSearch"; } } Write-Verbose "uri for get is $uri"; $apiInvoke = @{ uriPath=$uri; Method="GET"; jsonData=$jsonData; } if ($apiInvoke.jsonData -eq $null -OR $apiInvoke.jsonData -eq "") { $apiInvoke.Remove("jsonData"); } } process { $result = Invoke-FogApi @apiInvoke; } end { return $result; } } function New-FogObject { [CmdletBinding()] param ( # The type of object being requested [Parameter(Position=0)] [ValidateSet("objecttasktype","object")] [string] $type, # The json data for the body of the request [Parameter(Position=2)] [Object]$jsonData, # The id of the object when creating a new task [Parameter(Position=3)] [string]$IDofObject ) DynamicParam { $paramDict = Set-DynamicParams $type; return $paramDict;} begin { $paramDict | % { New-Variable -Name $_.Keys -Value $($_.Values.Value);} Write-Verbose "Building uri and api call"; switch ($type) { objecttasktype { $uri = "$CoreTaskObject/$IDofObject/task"; } object { $uri = "$CoreObject/create"; } } $apiInvoke = @{ uriPath=$uri; Method="POST"; jsonData=$jsonData; } } process { $result = Invoke-FogApi @apiInvoke; } end { return $result; } } function Update-FogObject { [CmdletBinding()] param ( # The type of object being requested [Parameter(Position=0)] [ValidateSet("object")] [string]$type, # The json data for the body of the request [Parameter(Position=2)] [Object]$jsonData, # The id of the object to remove [Parameter(Position=3)] [string]$IDofObject ) DynamicParam { $paramDict = Set-DynamicParams $type; return $paramDict;} begin { $paramDict | % { New-Variable -Name $_.Keys -Value $($_.Values.Value);} Write-Verbose "Building uri and api call"; $uri = "$CoreTaskObject/$IDofObject/edit"; $apiInvoke = @{ uriPath=$uri; Method="PUT"; jsonData=$jsonData; } } process { $result = Invoke-FogApi @apiInvoke; } end { return $result; } } function Remove-FogObject { [CmdletBinding()] param ( # The type of object being requested [Parameter(Position=0)] [ValidateSet("objecttasktype","objectactivetasktype","object")] [string]$type, # The json data for the body of the request [Parameter(Position=2)] [Object]$jsonData, # The id of the object to remove [Parameter(Position=3)] [string]$IDofObject ) DynamicParam { $paramDict = Set-DynamicParams $type; return $paramDict;} begin { $paramDict | % { New-Variable -Name $_.Keys -Value $($_.Values.Value);} Write-Verbose "Building uri and api call"; switch ($type) { objecttasktype { $uri = "$CoreTaskObject/$IDofObject/cancel"; } object { $uri = "$CoreObject/$IDofObject/delete"; } objectactivetasktype { $uri = "$CoreActiveTaskObject/cancel" } } $apiInvoke = @{ uriPath=$uri; Method="DELETE"; jsonData=$jsonData; } if ($apiInvoke.jsonData -eq $null -OR $apiInvoke.jsonData -eq "") { $apiInvoke.Remove("jsonData"); } } process { $result = Invoke-FogApi @apiInvoke; } end { return $result; } } function Install-FogService { param ( $fogServer = ((Get-FogServerSettings).fogServer) ) $fileUrl = "https://$fogServer/fog/client/download.php?newclient"; Write-Host "Making temp download dir"; mkdir C:\fogtemp; Write-Host "downloading installer"; Invoke-WebRequest $fileUrl -OutFile 'C:\fogtemp\fog.msi'; Write-Host "installing fog service"; Start-Process -FilePath msiexec -ArgumentList @('/i','C:\fogtemp\fog,msi','/quiet','/qn','/norestart') -NoNewWindow -Wait; Write-Host "removing download file and temp folder"; Remove-Item -Force -Recurse C:\fogtemp; Write-Host "Starting fogservice"; Start-Service FOGService; return; } Export-ModuleMember -Function *; # SIG # Begin signature block # MIILGQYJKoZIhvcNAQcCoIILCjCCCwYCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU6+vgiwgsT6u46XyKI1jAcaA8 # NpWgggeAMIIHfDCCBWSgAwIBAgITXQAAAEy4LtSLbbnUTQAAAAAATDANBgkqhkiG # 9w0BAQsFADBLMRMwEQYKCZImiZPyLGQBGRYDY29tMR8wHQYKCZImiZPyLGQBGRYP # YXJyb3doZWFkZGVudGFsMRMwEQYDVQQDEwpBcnJvd1NlY0NhMB4XDTE4MDYwMTE2 # NTkwOVoXDTIwMDYwMTE3MDkwOVowRTEd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obHTWZ7DfOV34owsyngRKtGCYUZvpX26fbppl0BFY3SJSGlkCTheTVvkRW # awX84AEZr9YFRvNMeDL1rgU692Sv1C1PLBgkgvdl2qigKKqPnhretjOJlnSj2YDa # H3axI3XI7wbdohxqmoKnYv/jUaWLHdi4TTHdybYezXoPjyqjwbnpoBGwfe7hXHA+ # +jRfrpfHhbEmnBCOng== # SIG # End signature block |