Created
September 1, 2016 22:37
-
-
Save mikebranstein/59d65a8e12d07c5622a8e0f932e99b88 to your computer and use it in GitHub Desktop.
Full code for disabling softAP on Windows 10 iot core with powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param ( | |
[string][Parameter(Mandatory = $true)] $deviceName | |
) | |
function Invoke-AuthenticatedWebRequest { | |
param ( | |
[string] $uri, | |
[string] $userName, | |
[string] $password, | |
[string] $method, | |
[string] $contentType | |
) | |
$credentials = "$($userName):$($password)" | |
$encodedCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($credentials)) | |
$basicAuthValue = "Basic $encodedCredentials" | |
$headers = @{ | |
Authorization = $basicAuthValue | |
} | |
Invoke-WebRequest -Uri $uri -Headers $headers -Method $method -ContentType $contentType -DisableKeepAlive | |
} | |
$deviceEndpoint = "$deviceName`:8080" | |
$apiRoot = "http://$deviceEndpoint/api" | |
#get the secret credentials | |
Write-Host "Creating Credentials..." | |
$username = "[Username]" | |
$password = "[Password]" | |
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force | |
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $securePassword | |
# Trust the remote pi and connect to it | |
Write-Host "Configuring WinRM TrustedHosts..." | |
$trustedHosts = Get-Item WSMan:\localhost\Client\TrustedHosts | |
if ($trustedHosts.Value -eq "") { | |
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $deviceName -Force | |
} | |
elseif (-not $trustedHosts.Value.Contains($deviceName)) { | |
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "$($trustedHosts.Value),$($deviceName)" -Force | |
} | |
Write-Host "Establishing PS Session to $deviceName..." | |
$pstimeout = New-PSSessionoption -OperationTimeout (1000*60*5) | |
$session = New-PSSession -computer $deviceName -Credential $cred -ErrorAction Stop -SessionOption $pstimeout | |
$retryLimit = 10 | |
$retryCount = 1 | |
$noError = $false | |
while ($noError -eq $false -and $retryCount -le $retryLimit) | |
{ | |
#Check for a 500 error on /api/iot/iotonboarding/softapsettings call | |
try{ | |
$responseSoftAP = Invoke-AuthenticatedWebRequest -uri "$apiRoot/iot/iotonboarding/softapsettings" -userName $username -password $password -method "GET" -contentType "" | |
} | |
catch { | |
Write-Host "There was an error... Retrying:" | |
Write-Host "Retry Count: $retryCount" | |
# Restart Device | |
Write-Host "Restarting Device $deviceName" | |
Invoke-Command -Session $session -FilePath .\Reboot-Device.ps1 | |
# wait for reboot of the device | |
Write-Host "Sleeping for 120 seconds while the device reboots..." | |
Start-Sleep -Seconds 120 | |
# re-establish the session | |
Write-Host "Done sleeping, resuming..." | |
Write-Host "Re-establishing remote powershell session..." | |
$session = New-PSSession -computer $deviceName -Credential $cred -ErrorAction Stop -SessionOption $pstimeout | |
$retryCount += 1 | |
Continue | |
} | |
$noError = $true | |
} | |
Write-Host "No More Errors!" | |
#This happens because we believe it provisions the SoftAP and needs a reboot for it to be set up correctly | |
Write-Host "Disabling SoftAP" | |
$softApEnabled = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("false")) | |
$softApSsid = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("SoftAPSsid")) | |
$softApPassword = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("p@ssw0rd")) | |
Invoke-AuthenticatedWebRequest -uri "$apiRoot/iot/iotonboarding/softapsettings?SoftAPEnabled=$softApEnabled&SoftApSsid=$softApSsid&SoftApPassword=$softApPassword" -userName $username -password $password -method "POST" -contentType "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment