0 votes

Hi team,

I am trying these two scripts but always getting an error.

  1. https://www.adaxes.com/script-repository/start-azure-virtual-machine-s442.htm
  2. https://www.adaxes.com/script-repository/restart-azure-virtual-machine-s443.htm

For debugging I opended a new Powershell window and tried to run step by step.

  • "Import-AzurePublishSettingsFile" is fine
  • "Start-AzureVM" is failing due to "BadRequest: The hosted service name is invalid."
  • I tried also "Get-AzureVM" but I dont get any VM shown ...

Is the sript maybe outdated or missing I am something?

PS: Script to Stop VM shows 404 not found

by (1.5k points)

1 Answer

0 votes
by (288k points)

Hello,

The thing is that the cmdlets used in the scripts are deprecated. At the same time, the new ones are only supported in PowerShell 7 which is not available in Adaxes. Thank you for pointing out the issue. The scripts were removed from our repository.

0

Ok, Thanks for clarification : )

But can I use Adaxes to Azure to manage virtual machines at all? Or do I need to authenticate within Powershell via Username/Password file?

0

Hello,

But can I use Adaxes to Azure to manage virtual machines at all?

Currently, Adaxes does not support managing Azure AD devices. However, the feature is in our TODO lsit.

Or do I need to authenticate within Powershell via Username/Password file?

As per our check, it is possible to use PowerShell, however, the corresponding cmdlets only support PowerShell 7. At the same time, Adaxes does not support it.

0

So, I installed the Az.Accounts module and tried this script on ISE Editor on Adaxes server, working fine.

As soon as I try same code within Adaxes PS Editor I get error: image.png Error Message Entry point was not found. Could not find tenant id for provided tenant domain 'yyy'. Please ensure that the provided service principal 'xxx' is found in the provided tenant domain. Stack trace: at <ScriptBlock>, <No file>: line 20

Line 20 is Connect-AzAccount command

Import-Module Az.Accounts

$vmName = "%cn%"
$Context.LogMessage("vmName $vmName", "Information")

$v = $PSVersionTable.PSVersion.major
$Context.LogMessage("$v", "Information")

# Define your Azure credentials
$clientId = "xxx"
$tenantId = "yyy"
$clientSecret = "zzz"

# Sign in to Azure using the service principal
try {
    $creds = [System.Management.Automation.PSCredential]::new($clientId, (ConvertTo-SecureString $clientSecret -AsPlainText -Force))
    $con = Connect-AzAccount -Tenant $tenantId -Credential $creds -ServicePrincipal
    $Context.LogMessage("Connect-AzAccount", "Information")

    # Set the context to the desired subscription
    try {
        $sub = Select-AzSubscription -SubscriptionId $subscriptionId -ErrorAction Stop
        $Context.LogMessage("Select-AzSubscription", "Information")

        try {
            $resourceGroup = (Get-AzVM -Name $vmName).ResourceGroupName
            $Context.LogMessage("ResourceGroupName $resourceGroup", "Information")       

            try {
                #$action = Start-AzVM -ResourceGroupName $resourceGroup -Name $vmName -ErrorAction Stop
                $action = Stop-AzVM -ResourceGroupName $resourceGroup -Name $vmName -ErrorAction Stop -Force

                $Context.LogMessage("Starting machine", "Information") 
            } catch {
                $Context.LogMessage("Starting machine failed due to `r`n$($_.Exception.Message)", "Error")
            }
        } catch {
            $Context.LogMessage("ResourceGroupName failed due to `r`n$($_.Exception.Message)", "Error")
        }
    } catch {
        $Context.LogMessage("Select-AzSubscription failed due to `r`n$($_.Exception.Message)", "Error")
    }

    # Disconnect from Azure
    $dis = Disconnect-AzAccount
} catch {
    $Context.LogMessage("Connect-AzAccount failed due to `r`n$($_.Exception.Message)", "Error")
}
0

Hello,

The error has nothing to do with Adaxes itself. For some reason executing the cmdlet fails due to the entry point not being found. As per our check this has been a known issue with the Az.Accounts module. For details, see https://github.com/Azure/azure-powershell/issues/21952.

0

I saw this also, but I was confused, as it is working fine with normal PowerShell window or ISE. No issues with 10+ runs.

Only running this code within Adaxes directly is causing issues.

I will check further and let you know.

Thanks for your response and check.

0

Hello,

According to the article we referenced, it looks like the issue is not present in all the versions of the module. If you have multiple versions installed it might happen so that running in Windows PowerShell ISE picks a version without the issue while Adaxes gets one with the issue. This might be the root cause of the behaviour you face.

0

Hi,

I uninstalled now every version of Az and Az.Accounts and installed the latest version again.

Still no luck ... Even with "Login-AzAccount" I get the same issue.

Now I tried to call my script within Adaxes and this is working well

# Define the script path and parameters
$scriptPath = "C:\Temp\AzureStartStopVM.ps1"
$scriptParameters = ""

# Start the PowerShell process
Start-Process -FilePath "c:\windows\system32\WindowsPowerShell\v1.0\powershell.exe" -ArgumentList "-File $scriptPath $scriptParameters" -Wait

Is Adxes using a specific PowerShell environment and not the default windows one?

0

Hello,

Adaxes is not using any special environment. It is using Windows PowerShell. Most probably, there is something in your environment (e.g. UAC) that is preventing Adaxes service account from loading the module which results into the issue you are facing.

+1

I am also getting this error. There is no way Adaxes is using a standard Powershell runspace if multiple people are getting this error within Adaxes but not in standard Powershell windows on the same server.

The latest versions of the Az modules, specifically the Az.Accounts module needed to login, are compatible with PSv5.1. Please quit telling people they are only compatible with PSv7. From the PSGallery page for Az.Accounts,

Az.Accounts 2.13.1 Microsoft Azure PowerShell - Accounts credential management cmdlets for Azure Resource Manager in Windows PowerShell and PowerShell Core.

For more information on account credential management, please visit the following: https://learn.microsoft.com/powershell/azure/authenticate-azureps

Minimum PowerShell version 5.1

If there really is no way to get this working with a script stored in Adaxes, we will also go the route of calling an external script, but that feels wasteful and harder to manage in the long run.

0

Hello,

Sorry for the confusion, but Microsoft articles say that PowerShell 7 is recommended for all Az PowerShell module. At the same time you are right and they are compatible with PowerShell 5. We were able to reproduce the issue. It seems to be related to some peculiarity in the module itself and thus we cannot be sure that we will be able to fix the issue. As of now, there is no workaround we can suggest. Sorry for the inconvenience.

Related questions

0 votes
1 answer

We have multiple business rules related to the modification of user properties which are specifically tailored for certain tasks. I'm trying to add another business rule which ... want the first rule to trigger. Hopefully this makes sense. Thanks in advance!

asked Apr 14, 2023 by Homelander90 (350 points)
0 votes
1 answer

Hi, We would like to run an AD sync (Start-ADSyncSyncCycle -PolicyType Delta) after a new user is created. Unfortunately, it looks like this script only works for servers that ... in any of the domains. What is the recommended way to achieve this? Thanks, Max

asked Sep 7, 2023 by mcutlyp (40 points)
0 votes
1 answer

If we want to obtain the Object ID for the corresponding 365 object of a user, we would use the logic: [Guid]$Context.TargetObject.Get("adm-O365ObjectId") -if ... definitively belongs to an on-premises Active Directory domain or a cloud Azure AD domain?

asked Dec 12, 2022 by Viajaz (210 points)
0 votes
1 answer

As part of offboarding a user I need to generate a report of all AD groups, Entra groups and all Azure / M365 roles and licenses the user has before they ... about keeping a record of the leavers configured profile to simplify cloning them onto new starters.

asked Jun 24 by dhardyuk (20 points)
0 votes
1 answer

what version of powershell is needed with modern auth and connect to 0365/azure

asked Sep 30, 2022 by sra98a (120 points)
3,538 questions
3,229 answers
8,224 comments
547,747 users