question

BaoxiJia-3817 avatar image
0 Votes"
BaoxiJia-3817 asked ArkadiuszKozie-6872 answered

How to create a new user with powershell command during or after New-VM command

On my local machine, I am trying to create a new Linux VM with the existing vhdx file through New-VM PowerShell command.

Issue: I cannot find any command option to create a VM user during/after the VM creation with New-VM command, but for Azure VM PowerShell, we can find commands to create user or add SSH public keys when creating the Azure VM.

Question: is it possible that we can create a new VM user through PowerShell command during or after we create the VM with New-VM ?

windows-server-hyper-v
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,

I would like to check if the reply in this blog could be of help? If yes, please help accept answer, so that others meet a similar issue can find useful information quickly. If you have any other concerns or questions, please feel free to feedback. Your support is really important to our work.

Best Regards,
Joan

0 Votes 0 ·
jiayaozhu-MSFT avatar image
0 Votes"
jiayaozhu-MSFT answered

Hi,

Thanks for posting on our forum!

After my research, I can find Add-VMGroupMember, which can add members to a virtual machine group. Groups can contain either virtual machines or groups of virtual machines. Here is the article:

https://docs.microsoft.com/en-us/powershell/module/hyper-v/add-vmgroupmember?view=windowsserver2019-ps

However, if this command cannot meet your demand, based on your description, you can just login your Linux VM and then run useradd command to create and add a new user to your Linux VM:

https://linuxize.com/post/how-to-create-users-in-linux-using-the-useradd-command/

(Please note: Information posted in the given link is hosted by a third party. Microsoft does not guarantee the accuracy and effectiveness of information.)

In addition, here is a list of Hyper-V related commands, if there is no command that can fit your need, then, you should consider my way to directly run commands on your VM. Or, if you really need to run commands on your computer, for instance, if you are in a remote environment that you cannot directly login to your Linux VM. You can go to Github to try to find out certain commands:
https://github.com/

(Please note: Information posted in the given link is hosted by a third party. Microsoft does not guarantee the accuracy and effectiveness of information.)

https://docs.microsoft.com/en-us/samples/browse/?redirectedfrom=TechNet-Gallery

Thanks for your support! Have a nice day! : )

BR,
Joan


If the Answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ArkadiuszKozie-6872 avatar image
0 Votes"
ArkadiuszKozie-6872 answered

Hello

Here are my findings:
.
https://docs.microsoft.com/en-us/troubleshoot/azure/virtual-machines/reset-rdp
.
https://seniordba.wordpress.com/2021/02/08/reset-the-azure-vm-administrator-password-2/
.
$SubID = "<SUBSCRIPTION ID>"
$RgName = "<RESOURCE GROUP NAME>"
$VmName = "<VM NAME>"
$Location = "<LOCATION>"

Connect-AzAccount
Select-AzSubscription -SubscriptionId $SubID
Set-AzVMAccessExtension -ResourceGroupName $RgName -Location $Location -VMName $VmName -Credential (get-credential) -typeHandlerVersion "2.0" -Name VMAccessAgent
.
If you enter a different name than the current local administrator account on your VM, the VMAccess extension will add a local administrator account with that name, and assign your specified password to that account. If the local administrator account on your VM exists, the VMAccess extension will reset the password. If the account is disabled, the VMAccess extension will enable it.
.
Best Regards
Arek
.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ArkadiuszKozie-6872 avatar image
0 Votes"
ArkadiuszKozie-6872 answered

And here are details for Linux including resetting SSH Public Key:
.
https://docs.microsoft.com/en-us/troubleshoot/azure/virtual-machines/troubleshoot-ssh-connection
.
Reset SSH credentials for a user
The following example uses az vm user update to reset the credentials for myUsername to the value specified in myPassword, on the VM named myVM in myResourceGroup. Use your own values as follows:
.
Azure CLI:
.
az vm user update --resource-group myResourceGroup --name myVM \
--username myUsername --password myPassword
.
If using SSH key authentication, you can reset the SSH key for a given user. The following example uses az vm access set-linux-user to update the SSH key stored in ~/.ssh/id_rsa.pub for the user named myUsername, on the VM named myVM in myResourceGroup. Use your own values as follows:
.
Azure CLI:
.
az vm user update --resource-group myResourceGroup --name myVM \
--username myUsername --ssh-key-value ~/.ssh/id_rsa.pub
.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.