Skip to content

Commit

Permalink
Added Set-CheckoutDevice.ps1 to assign a staged MacOS device to a use…
Browse files Browse the repository at this point in the history
…r. (#16)

* Added Set-CheckoutDevice.ps1

* Fixed error with Send-Patch
  • Loading branch information
MrTechGadget committed Jan 20, 2021
1 parent 53277ed commit f1c03bf
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 2 deletions.
24 changes: 22 additions & 2 deletions PSairwatch.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
.OUTPUTS
None
.NOTES
Version: 2.8.0
Version: 2.9.0
Author: Joshua Clark @MrTechGadget
Source: https://github.com/MrTechGadget/aw-bulkdevices-script
Creation Date: 05/22/2018
Update Date: 01/09/2021
Update Date: 01/20/2021
.EXAMPLE
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path -Parent
Expand Down Expand Up @@ -385,6 +385,26 @@ Function Send-Put {
}
}

Function Send-Patch {
Param(
[Parameter(Mandatory=$True,HelpMessage="Rest Endpoint for Patch, after https://airwatchServer/api/")]
[string]$endpoint,
[Parameter(HelpMessage="Version of API")]
[string]$version = $version1
)
$headers = Set-Header $restUserName $tenantAPIKey $version "application/json"
try {
$endpointURL = "https://${airwatchServer}/api/${endpoint}"
$webReturn = Invoke-RestMethod -Method Patch -Uri $endpointURL -Headers $headers

return $webReturn
}
catch {
Write-Warning "Error submitting PUT. $($_.Exception.Message) "
return $webReturn
}
}

Function Send-Delete {
Param(
[Parameter(Mandatory=$True,HelpMessage="Rest Endpoint for Delete, after https://airwatchServer/api/")]
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,18 @@ EXAMPLE
**Delete-Profile.ps1** - Deletes Profiles given a list of Profile IDs.
file parameter (REQUIRED) is the path to a CSV file with a list of Profile IDs.
fileColumn parameter (OPTIONAL, with a default value of "ProfileId") is the Column title in CSV file containing ProfileId values.

EXAMPLE
Delete-Profile.ps1 -file .\ProfilesTest.csv -fileColumn "ProfileId"

**Set-CheckoutDevice.ps1** - Assigns staged device to user via checkout API.
file parameter - Path of a CSV file with a list of DeviceId and desired UserId. This is required.
deviceColumn parameter - Column title in CSV file containing DeviceId values. This is optional, with a default value of "DeviceId".
userColumn parameter - Column title in CSV file containing UserId values. This is optional, with a default value of "UserId".

EXAMPLE
Set-CheckoutDevice.ps1 -file "Devices.csv" -deviceColumn "DeviceId" -userColumn "UserId"

## Compatibility

These PowerShell scripts are PowerShell Core (PS 6+) compliant and were written with Visual Studio Code on a Mac.
Expand Down
86 changes: 86 additions & 0 deletions Set-CheckoutDevice.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<#
.SYNOPSIS
Assigns staged device to user via checkout API.
.DESCRIPTION
Assigns staged device to user via checkout API. Requires a CSV file with DeviceId and desired UserId columns
.PARAMETER file
Path of a CSV file with a list of DeviceId and desired UserId. This is required.
.PARAMETER deviceColumn
Column title in CSV file containing DeviceId values. This is optional, with a default value of "DeviceId".
.PARAMETER userColumn
Column title in CSV file containing UserId values. This is optional, with a default value of "UserId".
.INPUTS
AirWatchConfig.json
CSV File with headers
.OUTPUTS
NO OUTPUT CURRENTLY:Outputs a CSV log of actions
.NOTES
Version: 1.0.1
Author: Joshua Clark @MrTechGadget
Creation Date: 01/11/2021
Update Date: 01/20/2021
Site: https://github.com/MrTechGadget/aw-bulkdevices-script
.EXAMPLE
.\Set-CheckoutDevice.ps1 -file "Devices.csv" -deviceColumn "DeviceId" -userColumn "UserId"
#>


[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=0,ValueFromPipeline=$true,HelpMessage="Path to file listing DeviceIds and UserIds.")]
[string]$file,
[Parameter(HelpMessage="Name of Id column in file, default is DeviceId")]
[string]$deviceColumn = "DeviceId",
[Parameter(HelpMessage="Name of desired UserId column in file, default is UserId")]
[string]$userColumn = "UserId"
)

Import-Module .\PSairwatch.psm1

$Logfile = "$PSScriptRoot\Checkout.log"

Function Write-Log {
Param ([string]$logstring)

$logstring = ((Get-Date).ToString() + " - " + $logstring)
Add-content $Logfile -value $logstring
}

$list = Read-FileWithData $file $deviceColumn $userColumn

Write-Log "$($MyInvocation.Line)"

$decision = $Host.UI.PromptForChoice(
"Attention! If you proceed, " + @($list).count + " devices will be assigned to the corresponding user identified in this file in AirWatch",
"",
@('&Yes', '&No'), 1)

if ($decision -eq 0) {
Write-Log "Assigning to users on $($list.count) devices in AirWatch"
$i = 0
foreach ($item in $list) {
$i++
Write-Progress -Activity "Assigning Devices..." -Status "$($i) of $($list.Count)" -CurrentOperation "$($item.$deviceColumn) : $($item.$userColumn)" -PercentComplete ((($i)/(@($list).Count))*100)
$endpointURL = "mdm/devices/$($item.$deviceColumn)/enrollmentuser/$($item.$userColumn)"
try {
$result = Send-Patch -endpoint $endpointURL -version $version2
if ($result -ne "") {
$err = ($Error[0].ErrorDetails.Message | ConvertFrom-Json)
Write-Warning ("Error Assigning DeviceName: $($item.$deviceColumn) : Error", $err.errorCode, $err.message)
Write-Log ("Error Assigning DeviceName: $($item.$deviceColumn) : Error", $err.errorCode, $err.message)
} else {
Write-Host "$($item.$deviceColumn) assigned to $($item.$userColumn) $result"
Write-Log "$($item.$deviceColumn) assigned to $($item.$userColumn) $result"
}
}
catch {
$err2 = ($Error[0].ErrorDetails.Message | ConvertFrom-Json)
Write-Warning "Error Assigning DeviceName: $($item.$deviceColumn) to $($item.$userColumn) $err2"
Write-Log "Error Assigning DeviceName: $($item.$deviceColumn) to $($item.$userColumn) $err2"
}
}
} else {
Write-Host "Action Cancelled"
Write-Log "Action Cancelled"
}

0 comments on commit f1c03bf

Please sign in to comment.