Skip to content

Instantly share code, notes, and snippets.

@1RedOne
Created November 10, 2017 14:53
Show Gist options
  • Select an option

  • Save 1RedOne/be577f99f4b667663a8b166d7cf5926b to your computer and use it in GitHub Desktop.

Select an option

Save 1RedOne/be577f99f4b667663a8b166d7cf5926b to your computer and use it in GitHub Desktop.
Persist Credentials
<#
.Synopsis
Use this cmdlet to safely store credentials for your AirWatch account for management via PowerShell
.DESCRIPTION
With one cmdlet, you can connect to your AirWatch account via the REST API. After using this cmdlet, you can use any of the other *AirWatch cmdlets to do things like upload an image or an album, etc (to come later)
.EXAMPLE
Connect-AWAccount -ClientID [String] -ClientSecret [ClientSecret]
.EXAMPLE
Connect-AWAccount -Force
If you need to renew your API key (roughly once a month), then rerun the cmdlet with -Force
#>
Function Connect-AWAccount {
[CmdletBinding()]
param($cred,$clientSecret,[Switch]$force,[Switch]$new)
$basePath = "$Env:AppData\WindowsPowerShell\Modules\PSAirWatch\1.0"
$tenants = New-Object System.Collections.ArrayList
if (-not (Test-Path $basePath) -or $force -or $new){
if ($force){"`$force detected"}
New-item -Force -Path $basePath -ItemType Directory
$values = Show-AWConnectForm
$ThisTenantBase = "$basepath\$($values.Url)"
Write-debug "Check the values back from the registration form"
new-item -Force $ThisTenantBase -ItemType Directory
$tenantPath = "$ThisTenantBase\config_tenantID.ps1xml"
$passwdPath = "$ThisTenantBase\config_password.ps1xml"
$usernmPath = "$ThisTenantBase\config_username.ps1xml"
$tenantId = ConvertTo-SecureString $values.TenantID -AsPlainText -Force
$tenantId | ConvertFrom-SecureString | Export-Clixml $tenantPath -Force
$password = ConvertTo-SecureString $values.password -AsPlainText -Force
$password | ConvertFrom-SecureString | Export-Clixml $passwdPath -Force
$username = ConvertTo-SecureString $values.username -AsPlainText -Force
$username | ConvertFrom-SecureString | Export-Clixml $usernmPath -Force
[void]$tenants.Add([pscustomobject]@{
URL=$values.Url;
TenantID=Get-DecryptedValue $tenantId
username=Get-DecryptedValue $username
password= $password
})
}
else{
#Find all AW Credentials created with this module
$Creds = Get-ChildItem -Path $basePath
Write-Debug "test `$creds here"
ForEach ($ThisCred in $creds){
$credDir = $ThisCred.FullName
Write-Debug "test `$ThisCred here"
try {
$tenantId = Import-Clixml -Path "$credDir\config_tenantID.ps1xml" -ErrorAction STOP | ConvertTo-SecureString
$username = Import-Clixml -Path "$credDir\config_username.ps1xml" -ErrorAction STOP | ConvertTo-SecureString
$password = Import-Clixml -Path "$credDir\config_password.ps1xml" -ErrorAction STOP | ConvertTo-SecureString
}
catch {
Write-Warning "Corrupt Password file found, rerun with -Force to fix this"
BREAK
}
'Found cached Cred'
Write-Verbose "Importing $($ThisCred.Name)"
[void]$tenants.Add([pscustomobject]@{
URL=$ThisCred.Name;
TenantID=Get-DecryptedValue $tenantId
username=Get-DecryptedValue $username
password=$password
})
#EndOfForEach
}
#EndOfCachedCredsFound
}
New-Variable -Scope Global -Name tenants -Value $tenants -Force
#EndOfFunction
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment