authentication to azure subscription with out prompting credentialss

Varma 1,250 Reputation points
2024-04-24T08:31:37.1+00:00

how we run connect-azconnect with out asking for credentials as i have to run in target machine? This is the script I am trying to execute, what need to amend?


$subscriptions = Get-AzSubscription 
Connect-AzAccount -AccountID 'username@domain.com'
 
$a=@()
 
# Loop through each subscription
 
foreach ($subscription in $subscriptions) {
 
    # Set the current subscription
 
    Set-AzContext -SubscriptionId $subscription.SubscriptionId
 
    $a+=Search-AzGraph -Query "patchinstallationresources| where type has ""softwarepatches"" and properties !has ""version""| extend machineName = tostring(split(id, ""/"", 8)), resourceType = tostring(split(type, ""/"", 0)), tostring(rgName = split(id, ""/"", 4)), tostring(RunID = split(id, ""/"", 10))| extend prop = parse_json(properties)| extend lTime = todatetime(prop.lastModifiedDateTime), patchName = tostring(prop.patchName), kbId = tostring(prop.kbId), installationState = tostring(prop.installationState), classifications = tostring(prop.classifications)| where lTime > ago(30d)| project lTime, RunID, machineName, rgName, resourceType, patchName, kbId, classifications, installationState| sort by RunID" 
 
}
 
$a | Select-Object -Property lTime,RunID,machineName,rgName,resourceType,patchName,kbId,classifications,installationState | Export-Csv ./testgraph.csv

Azure Cloud Services
Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
665 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Andreas Baumgarten 101.5K Reputation points MVP
    2024-04-24T08:54:25.2133333+00:00

    Hi @Varma ,

    storing credentials as plain text is not recommended for use in script, but for authentication you need userID & password.

    This being said it means you have to request the credentials at least once in your script.

    If you are not using MFA for this login account you can use this code snippet:

    $Credential = Get-Credential
    Connect-AzAccount -Credential $Credential
    # .....
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    1 person found this answer helpful.