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

Baoxi Jia 1 Reputation point
2021-06-28T08:47:21.303+00:00

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 ?

Hyper-V
Hyper-V
A Windows technology providing a hypervisor-based virtualization solution enabling customers to consolidate workloads onto a single server.
2,733 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Arkadiusz Kozieł 56 Reputation points
    2022-03-25T09:07:55.93+00:00

    Hello

    Here are my findings:
    .
    https://learn.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
    .

    1 person found this answer helpful.
    0 comments No comments

  2. JiayaoZhu 3,911 Reputation points
    2021-06-29T03:37:35.073+00:00

    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://learn.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://learn.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.

    0 comments No comments

  3. Arkadiusz Kozieł 56 Reputation points
    2022-03-25T09:12:26.967+00:00

    And here are details for Linux including resetting SSH Public Key:
    .
    https://learn.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
    .

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.