Azure Command doesn't create AD User in Powershell - powershell

I am attempting to use the Azure CLI to create a user in my Active Directory. I am using this command:
az login
az ad user create --display-name "John Doe" --password "dfdfsd34!234" --user-principal-name "john#mydomain.com" --force-change-password-next-login true --mail-nickname "Jonny"
(I have obfuscated the UPN name)
If I run that from my command line, it runs exactly how I wish and the user appears in my Active Directory. If I place that command inside a Powershell script it fails, saying the UPN is invalid.
az : ERROR: Property userPrincipalName is invalid.
My version of Powershell is 5.1.14409.1005
Any ideas what I am missing? I originally assumed it's correctly logging into Azure, but then returning to the original shell?

When we use the Azure CLI command az ad user create to create Azure AD user, please ensure you have provide a validated Azure AD domain. If the domain is not validated, you will get the above error. The default domain is ***.onmicrosoft.com For more details, please refer to here and here
For example
az ad user create --display-name "John Doe" --password "dfdfsd34!234" --user-principal-name "john#hanxia.onmicrosoft.com" --force-change-password-next-login true --mail-nickname "Jonny"

Related

import .cert file to azure keyvault issues

I uploaded a .cert certificate as secure file in azure devops
we are using Classic pipelines
my pipeline added two additional tasks
1, download secure file
2, Azure Cli task to import .cert file add below script as inline script
$certFilePath = $(Agent.TempDirectory)\mycert.com.crt
az keyvault certificate import --vault-name "keyvaultname" -n "mycert.com.crt" -f $certFilePath
getting the below error
D:\agent_work_temp\mycert.com.crt : The term 'D:\agent_work_temp\mycert.com.crt' 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.
At D:\agent_work_temp\azureclitaskscript1675572660483_inlinescript.ps1:1 char:12
$inFile = D:\agent_work_temp\mycert.com.crt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ObjectNotFound: (D:\agent_work_temp\mycert.com.crt:String) [], ParentContainsErrorRe
cordException
FullyQualifiedErrorId : CommandNotFoundException
I have tried to reproduce the same in my lab environment and got the below results.
Firstly, I would like to inform you that the file type of the certificate to be imported must be the .pfx or .pem extension. Kindly refer to this link for more details.
I have followed the below steps to upload a .pfx file to the Azure Key Vault.
Step 1: Created a .pfx type certificate.
Step 2: Upload a file to the secure files in Azure DevOps.
Step 3: Add below mentioned tasks to the pipeline.
Command: az keyvault certificate import --vault-name "key-vault-vijay" --file "$(Agent.TempDirectory)\test05.pfx" --name "cert0104" --password "passw0rd#123"
Pass the password parameter if you have created it at the time of certificate file creation.
Step 4: Verify the access policies assigned to the service principle using Azure DevOps automation on the key vault.
Step 5: Run the pipeline and kindly check for the certificate on the Azure portal.
Kindly refer to this link Manage Azure Key Vault using CLI - Azure Key Vault | Microsoft Learn for more details.

Automatically pass string to powershell user input

In my powershell script, I call the following azure function:
az repos import create --git-source-url https://incommunities#dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization
When running, it prompts the user for a Password/PAT token, e.g:
Git Password / PAT:
Is there a way to automatically pass the password/token to the user input without having to enter manually?
I attempted to pipe the value through, however this does not seem to work e.g
my-pat-token | az repos import create --git-source-url https://incommunities#dev.azure.com/my-organisation/Templates/_git/$($Framework) --detect true --project $ProjectAlias --repository $ProjectAlias --requires-authorization
Is this both a) possible and then if so b) how can I do this?
There are two approaches you can use, both come courtesy from this nice blog post which you'll probably want to read, as it talks about a bunch of Azure Devops tasks.
Use an environmental variable
These commands will check for the presence of an environmental variable and will use it instead of prompting.
To do this, set an environment variable called AZURE_DEVOPS_EXT_PAT to the value of your PAT. (More info on how these tokens work here from the Microsoft Docs)
# set environment variable for current process
$env:AZURE_DEVOPS_EXT_PAT = 'xxxxxxxxxx'
When you're automating things, just set this variable before running the Azure commands.
Pipe the value in
I am not as big of a fan of this sort of approach but you can "echo out" the PAT value and pipe that into a command, which might work. IMHO this is more fragile and frunky and I wouldn't advise it.
$pat | az devops login

Access denied error in Azure devops CI CD deployment

Below are the detailed steps which i am performing for CI CD created pipeline to create spkg.sppkg its successful now in release i am doing below steps
Connect to SharePoint App Catalog (Successful)
o365 login https://naxis007.sharepoint.com/sites/Wipro%20App%20Catalog/AppCatalog --authType password --userName $(username) --password $(password)
Add Solution Package to App Catalog **(getting error access denied in this steps)**
o365 spo app add -p $(System.DefaultWorkingDirectory)/Test/drop/drop/sharepoint/solution/spfx.sppkg --overwrite --appCatalogUrl https://naxis007.sharepoint.com/sites/Wipro%20App%20Catalog/AppCatalog --scope sitecollection
It looks like you were running o365 login command and o365 spo app add command in two separate command line tasks.
Each command line task will open a new terminal window which will be closed when the task is finished. So the login information in the first command line task cannot persist in the second command link task. That's why you got the access denied error.
You should run the o365 login command and o365 spo app add command in the same command line task. So that both the commands will be executed in the same terminal.
Or you can use the task SharePoint Files Uploader to upload the spfx.sppkg file .

How to use PowerShell as admin on Hyper.js

I'm trying to use PowerShell with Hyper.js. I have this on the hyper config file:
shell: 'powershell.exe',
shellArgs: ['']
This way PowerShell is loaded as non-admin, and I receive an error because I don't have a PowerShell profile file configured, but I have a profile for admin user, I want to configure Hyper.js to use PowerShell as admin.
It's not the best solution, but you can run Hyper as admin
https://github.com/zeit/hyper/issues/1752#issuecomment-427086434
There are some other solutions in the linked issue.

Powershell script to login RDP session

I have a .rdp file which is downloaded from azure cloud. I am wrote a script which will login to the machine and get some files. But the script is failing.
Can someone please guide me on this.
1). I will provide the machine name (.RDP file) in the script by providing the username & password.
2). Once the script runs, it has to collect the logs from C:\Windows\Logs\IE11_HardwareCheck.log and copy them to my local desktop (c:\windows\logs\machine_name) and change the file name with current date.
Thanks,
-Kalyan