Since your code is running on a server, a popup will try to appear on the local system (server), and this is obviously not possible because you are not logged in.
The proper way to do this is to use the Credential object according to the code posted at the end of this answer. You will need to create your own mechanism (form) to capture the username and password.
Unfortunately, at the time of publication, Microsoft IDs do not support non-interactive flow (what are you trying to do). You will need to run your script on your client machine, log in to your server, or set up an Azure AD account with the appropriate credentials.
Using the Credential Object to Log in to Azure:
$yourPassword = ConvertTo-SecureString "<Your Password>" -AsPlainText –Force
$yourCredential =
New-Object -TypeName pscredential –ArgumentList "<Your UserName>", $yourPassword
Login-AzureRmAccount -Credential $yourCredential
-ServicePrincipal –TenantId <Your TenantId>
source
share