Friday, April 7, 2023

Orchestrator fails to run powershell cmdlets

Scenario: After you add a runbook activity which runs a powershell script. The activity fails with error.

e.g connecting to Azure using an app id.

The term 'Connect-AzAccount' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

 

If you open the runbook using runbook tester it may work fine.

But when you click run in the Designer it will fail.

This happens because the required modules are not installed in the runbook service context.

To do this add a new  Run . Net script activity and add the lines to install those modules in the it.

You might also get the following  error.


 Install-NuGetClientBinaries -CallerPSCmdlet $PSCmdlet -Proxy $Proxy -ProxyCredential $ProxyCredential
 Exception: Exception calling "ShouldContinue" with "2" argument(s): "A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files (x86)\PackageManagement\ProviderAssemblies' or 'C:\Windows\system32\config\systemprofile\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?"

 

 

Giving the lines here for the lazy ones.


Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201  -scope CurrentUser  -Force

Install-module Az -scope CurrentUser -Force

Tuesday, September 6, 2022

Powershellscript to get details of Azure users who have synced to Azure with diferent upn

 # Connecting to Azure Parameters
$tenantID = "mytenantid"
$ClientId = "myapplicationid"
$ClientSecret = "myclientsecret"



# Create a hashtable for the body, the data needed for the token request
# The variables used are explained above
$Body = @{
    'tenant' = $TenantId
    'client_id' = $ClientId
    'scope' = 'https://graph.microsoft.com/.default'
    'client_secret' = $ClientSecret
    'grant_type' = 'client_credentials'
}

# Assemble a hashtable for splatting parameters, for readability
# The tenant id is used in the uri of the request as well as the body
$Params = @{
    'Uri' = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
    'Method' = 'Post'
    'Body' = $Body
    'ContentType' = 'application/x-www-form-urlencoded'
}

$AuthResponse = Invoke-RestMethod @Params

$Headers = @{
    'Authorization' = "Bearer $($AuthResponse.access_token)"
}

#$url = "https://graph.microsoft.com/v1.0/users/?$select=displayName,givenName,postalCode,UserPrincipalName,onPremisesDistinguishedName,onPremisesUserPrincipalName"

$url = 'https://graph.microsoft.com/v1.0/users/?$select=UserPrincipalName,onPremisesDistinguishedName,onPremisesUserPrincipalName'

$userPurpose = @()

$UserData = Invoke-RestMethod -Method GET -headers $headers -Uri $url


$nexturl = $UserData.'@odata.nextLink'
$userPurpose+= $UserData
$count = 0
while($nexturl -ne "")
{

$nexturl = $UserData.'@odata.nextLink'
Write-host $nexturl -ForegroundColor Yellow
if(!$nexturl) {break;}
$UserData = Invoke-RestMethod -Method GET -headers $headers -Uri $nexturl


$userPurpose+= $UserData
$count++

}

 Write-host "Count of url: $count" -ForegroundColor Yellow

 $userPurpose.count

 $Users = @()
 Foreach($userdata in $userPurpose)
 {
   $Users+=$userdata.value
 }

$users | Export-csv -Path c:\temp\Azureusers1.csv -NoClobber -NoTypeInformation -Append