Function Find-CoreAttributeValues { <# .SYNOPSIS Function to parse attributes values in Core .DESCRIPTON Function will parse a set of attribute values into a nice Powershell object If ID or name provided will then search the values for either or both of them and return the item found Can also return the whole list of the ReturnAll switch is used Example for Ticket Queues: https://ws.core.rackspace.com/ctkapi/values/Computer.Status Used in Find-CoreComputer to search a list of statuses dynamically and return the ID !!!!!!!! Dev notes !!!!!!!! Its possible to use the JSON to parse the values to make less typing for parameter validation sets in functions as well as switch statements to convert core attribute name values to their IDs. Take this for example for Ticket.Classification: Get the JSON of the values from the CORE documentation: $JSON = (Invoke-WebRequest -Method Post -Uri 'https://ws.core.rackspace.com/ctkapi/values/Ticket.Classification' -Header @{"X-Auth"="$(Get-PoshCoreToken)"}).Content Obviously change Ticket.Classification to whatever Now you can print out the name values in the formate for a param ValidateSet $json | ForEach-Object {write-host "`"$($_[1])`"," -NoNewline} And also print a switch statement to convert name to ID: write-host "switch (`$ClassificationName)" write-host "{" $json | ForEach-Object {write-host " `"$($_[1])`" {`$ClassificationId = `"$($_[0])`"}" } write-host "}" .PARAMETER Class Mandatory The top level class for the values you are seaching for For example Computer in Computer.Status .PARAMETER SubClass Mandatory The sub level class for the values you are seaching for For example Status in Computer.Status .PARAMETER ReturnAll Cannot be used with SearchId or SearchName Returns all values for the attribtue class selected .PARAMETER SearchId Cannot be used with ReturnAll Can be used with SearchName Searches the IDs on the returned values and returns matching ID Returns false if not found .PARAMETER SearchName Cannot be used with ReturnAll Can be used with SearchId Searches the names ofthe returned values and returns matching name Returns false if not found .EXAMPLE Return all SKUs with "HP Memory" as their name Find-CoreAttributeValues -Class "Product" -SubClass "Product" -SearchName "HP Memory" .EXAMPLE Return all computer statuses Find-CoreAttributeValues -Class "Computer" -SubClass "Status" -ReturnAll .EXAMPLE Find the Status ID for the status "Decommission" Find-CoreAttributeValues -Class "Computer" -SubClass "Status" -SearchName "Decommission" .EXAMPLE Find the Computer status for the status ID "5" Find-CoreAttributeValues -Class "Computer" -SubClass "Status" -SearchId 5 .OUTPUTS Object with name,id and description of values .NOTES Written by Martin Howlett #> [CmdletBinding(DefaultParameterSetName="Search")] Param ( [Parameter(ParameterSetName="Search")] [Parameter(ParameterSetName="ReturnAll")] [Parameter(Mandatory=$true)] [ValidatePattern("^[A-Z]{1}")] [String]$Class, [Parameter(ParameterSetName="Search")] [Parameter(ParameterSetName="ReturnAll")] [Parameter(Mandatory=$true)] [ValidatePattern("^[A-Z]{1}")] [String]$Subclass, [Parameter(ParameterSetName="ReturnAll", Mandatory=$true)] [Parameter()] [ValidateNotNullOrEmpty()] [Switch]$ReturnAll, [Parameter(ParameterSetName="Search")] [Int]$SearchId, [Parameter(ParameterSetName="Search")] [ValidatePattern("\w")] [String]$SearchName ) Begin { Write-Verbose "Start of $($MyInvocation.MyCommand)" } Process { #Make sure only first letter is uppercase on class and subclass #$Class = $Class.substring(0,1).toupper()+$Class.substring(1).tolower() #$SubClass = $SubClass.substring(0,1).toupper()+$SubClass.substring(1).tolower() #commented out code above to make sure class and subclass are first letter upercase and rest #lowercase as this dones't apply to all attrubites lists in core for example "https://ws.core.rackspace.com/ctkapi/values/QueueView.QvQueueView" #I used write-verbose to help with troubleshooting as don't want to write a warning in all cases. Try{ #get the values from CTKAPI $Result = Invoke-CoreQuery -Url "https://ws.core.rackspace.com/ctkapi/values/$Class.$SubClass" -Method Get } Catch { #If last error was a 404 check the case of calls and subclass and flag a warning. If ($error[0] -like "*Invoke-Query :: 404 URL*") { If (($Class -cnotmatch "^[A-Z]{1}[a-z]{1,}$") -or ($SubClass -cnotmatch "^[A-Z]{1}[a-z]{1,}$")) { Write-warning "Class or SubClass specified doesn't match first letter uppercase, all other letters lowercase." } Write-warning "Check URL is valid https://ws.core.rackspace.com/ctkapi/values/$Class.$SubClass" #throw last error from Invoke-CoreQuery Throw "$($error[0])" } Else { #throw last error from Invoke-CoreQuery Throw "$($error[0])" } } #convert to decent object [System.Collections.ArrayList]$List = @() ForEach($Item in $Result) { $ListObject = New-Object PSObject -Property @{ Id = $Item[0] Name = $Item[1] Description = $Item[2] } $List.Add($ListObject) | Out-Null } Switch ($PSCmdlet.ParameterSetName) { "Search" { #search based in requirements if($SearchId -and $SearchName) { $SearchResult = $List | Where-Object {($_.Name -eq "$SearchName") -and ($_.Id -eq "$SearchId")} } elseif($SearchId) { $SearchResult = $List | Where-Object {$_.Id -eq "$SearchId"} } elseif($SearchName) { $SearchResult = $List | Where-Object {$_.Name -eq "$SearchName"} } else { $Reason = "Find-CoreAttributeValues - You must provide a search ID or search name if you do not select the ReturnAll switch" $Params = @{ FunctionName = "$($MyInvocation.MyCommand)" CallingScript = "$($MyInvocation.ScriptName)" Line = "$($_.invocationInfo.ScriptLineNumber)" ErrorMsg = "$($_.exception.message)" CommandName = "$($_.exception.CommandName)" Variables = $(Get-Variable -Scope Local -ErrorAction SilentlyContinue) Version = "$version" Reason = "$Reason" } Export-PowershellError @Params Throw "$Reason" } #return result or false if not found if($SearchResult) { Return $SearchResult } else { Return $false } } "ReturnAll" { #if user wants everything just return the list Return $List } } } End { #Log to appstats Set-LogtoAppstats -FunctionID "Find-CoreAttributeValues" -AppID "PoshCore" Write-Verbose "End of $($MyInvocation.MyCommand)" } }