Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save williamoconnorme/ffd88e2206abedee4a436cee7c416d39 to your computer and use it in GitHub Desktop.
Save williamoconnorme/ffd88e2206abedee4a436cee7c416d39 to your computer and use it in GitHub Desktop.
This script will allow you to use DefaultAzureCredential locally with your service fabric cluster. It uses PsExec to login the NETWORK SERVICE Account using the AZ CLI. This caches the token credential for the service account
<#
.SYNOPSIS
This script uses PsExec to run a PowerShell command for Azure CLI login as the NETWORK SERVICE account using device code authentication.
#>
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
$isAdmin = (New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($isAdmin) {
Write-Warning "This script will install set an Azure CLI login token for the NETWORK SERVICE account. Continue?" -WarningAction Inquire
if (Get-Command az -ErrorAction SilentlyContinue) {
Write-Host "Azure CLI is available."
}
else {
$installAzureCLI = Read-Host "Azure CLI is not installed. Would you like to install it? (Y/N)"
if ($installAzureCLI -eq "Y" -or $installAzureCLI -eq "y") {
Write-Host "Installing Azure CLI..."
$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; Remove-Item .\AzureCLI.msi
Write-Host "Azure CLI installed successfully."
}
else {
Write-Host "Azure CLI installation declined. Exiting..."
Exit
}
}
if (Get-Command psexec -ErrorAction SilentlyContinue) {
Write-Host "PsExec is available."
}
else {
$installAzureCLI = Read-Host "PsExec is not installed. Would you like to install it? (Y/N)"
if ($installAzureCLI -eq "Y" -or $installAzureCLI -eq "y") {
Write-Host "Installing Sysinternals with winget..."
winget install sysinternals --accept-package-agreements
Write-Host "SysInternals installed successfully."
}
else {
Write-Host "SysInternals installation declined. Exiting..."
Exit
}
}
Write-Host "Executing Azure CLI login as NETWORK SERVICE..."
psexec -i -nobanner -u "nt authority\network service" powershell -command az login --use-device-code
}
else {
Write-Host "You need to run this script as administrator before running this script."
exit
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment