Skip to content

Commit

Permalink
Add Tag action from Tag scripts Repo (#31)
Browse files Browse the repository at this point in the history
* Add import, refactor config

* Cleaned up description, added CmdletBinding

* Refactor read list of serials

* Added logging of tag action and  results

* Refactor POST API call

* Refactored Get and Select Tag

* Refactor Get-DeviceIds

* Cleaned up unused functions

* Remove Refactored function
  • Loading branch information
MrTechGadget committed Oct 26, 2022
1 parent 5dfc682 commit 3e57e73
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
26 changes: 25 additions & 1 deletion PSairwatch.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
.OUTPUTS
None
.NOTES
Version: 2.12.2
Version: 2.13.0
Author: Joshua Clark @MrTechGadget
Source: https://github.com/MrTechGadget/aw-bulkdevices-script
Creation Date: 05/22/2018
Expand Down Expand Up @@ -287,6 +287,30 @@ Function Get-DeviceDetails {

}

Function Get-DeviceIds {
Param([string]$body)

Write-Verbose("------------------------------")
Write-Verbose("List of Serial Numbers")
Write-Verbose $body
Write-Verbose("------------------------------")

$headers = Set-Header $restUserName $tenantAPIKey $version1 "application/json"
$endpointURL = "https://${airwatchServer}/api/mdm/devices?searchby=Serialnumber"
$webReturn = Invoke-RestMethod -Method Post -Uri $endpointURL -Headers $headers -Body $body

$deviceids = @()
foreach ($serial in $webReturn.Devices) {
$deviceids += $serial.Id.Value
}
Write-Verbose("------------------------------")
Write-Verbose("List of Device IDs")
Write-Verbose "$($deviceIds)"
Write-Verbose("------------------------------")

return $deviceids
}

Function Get-TaggedDevice {
Param([string]$SelectedTag)

Expand Down
81 changes: 81 additions & 0 deletions Set-TagOnDevice.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

<# Set-TagOnDevice Powershell Script Help
.SYNOPSIS
This Poweshell script adds a selected tag to a list of devices.
.DESCRIPTION
This script will take an input of serial numbers from a CSV file, converts them to device IDs.
It queries a list of all Tags in the environment, the user selects the Tag to add the devices to and it adds the Tag in AirWatch for each of those devices.
.INPUTS
CSV File with headers
.OUTPUTS
NO OUTPUT CURRENTLY:Outputs a log of actions
.NOTES
Version: 1.4
Author: Joshua Clark @MrTechGadget
Creation Date: 09/06/2017
Update Date: 10/25/2022
Site: https://github.com/MrTechGadget/aw-bulkdevices-script
.EXAMPLE
.\Set-TagOnDevice.ps1 -file "Devices.csv" -fileColumn "SerialNumber"
#>

[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,HelpMessage="Path to file listing SerialNumbers.")]
[string]$file,

[Parameter(HelpMessage="Name of Id column in file, default is SerialNumber")]
[string]$fileColumn = "SerialNumber"
)

Function Set-Action {
$options = [System.Management.Automation.Host.ChoiceDescription[]] @("&Add", "&Remove")
[int]$defaultchoice = 0
$opt = $host.UI.PromptForChoice($Title , $Info , $Options,$defaultchoice)
switch($opt)
{
0 { return "add"}
1 { return "remove"}
}
}



Import-Module .\PSairwatch.psm1
Write-Log -logstring "$($MyInvocation.Line)"

$Logfile = "$PSScriptRoot\TagActivity.log"

<#
Start of Script
#>

$Config = Read-Config
$tenantAPIKey = $Config.awtenantcode
$organizationGroupID = $Config.groupid
$airwatchServer = $Config.host
$list = Read-File $file $fileColumn

Write-Log -logstring "$($MyInvocation.Line)" -logfile $Logfile

<# Get the tags, displays them to the user to select which tag to add. #>
$TagList = Get-Tags
$SelectedTag = Select-Tag $TagList
$TagName = $TagList.keys | Where-Object {$TagList["$_"] -eq [string]$SelectedTag}
Write-Host "Selected Tag: $($TagName)"
Write-Log -logstring "Selected Tag: $($TagName)" -logfile $Logfile

$action = Set-Action
$SerialJSON = Set-AddTagJSON $list
$deviceIds = Get-DeviceIds $SerialJSON
$addTagJSON = Set-AddTagJSON $deviceIds
$endpointURL = "mdm/tags/${SelectedTag}/${action}devices"
$results = Send-Post $endpointURL $addTagJSON

Write-Host("------------------------------")
Write-Host("Results of ${action} Tags Call")
Write-Host("Total Items: " +$results.TotalItems)
Write-Host("Accepted Items: " + $results.AcceptedItems)
Write-Host("Failed Items: " + $results.FailedItems)
Write-Host("------------------------------")
Write-Log -logstring "Results of ${action} Tags Call, Total Items: $($results.TotalItems), Accepted Items: $($results.AcceptedItems), Failed Items: $($results.FailedItems)" -logfile $Logfile

0 comments on commit 3e57e73

Please sign in to comment.