简体   繁体   中英

Spinning up VM from Powershell with multiple admins

I have a script which spins up an Azure VM and specifies an admin username and password.

Is it possible to have the script setup a second admin? The reason for this is so that more than one user can be on the machine at the same time.

Do you have access to the vm with Invoke-Command ?
If yes, might this helps: How to Manage Local Users and Groups using PowerShell

According to my research, two users can access Azure windows VM concurrently. A maximum of two concurrent connections are supported unless the server is configured as a Remote Desktop Services session host. Regarding how to add local user to Azure VM, you use the the VM Access extension in Azure PowerShell. For more details, please refer to the document在此处输入图像描述 For example

Connect-AzAccount 

$vm = Get-AzVM -ResourceGroupName jimtest -Name jimtest

$name = "jimtest1"
$password = "Pass***!"
$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$mycred= New-Object System.Management.Automation.PSCredential ($name, $secpasswd)
Set-AzVMAccessExtension -Credential $mycred -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Location $vm.Location -Name VMAccessAgent -TypeHandlerVersion "2.0"

在此处输入图像描述 在此处输入图像描述

You can use this PowerShell command below to add an admin account to your VM:

$adminName = "testadmin"
$passstr = "password123!"
$Password =  ConvertTo-SecureString -String $passstr -AsPlainText -Force
New-LocalUser $adminName -Password $Password -FullName $adminName -Description "test admin account"
Add-LocalGroupMember -Group "Administrators" -Member $adminName

And you can use the Powershell command below to run your custom Powershell command on your Azure VMs(get started with azure powershell see here ):

Connect-AzAccount
$vm = Get-AzVM -Name "<your vm name>" -ResourceGroupName "<your vm resource group>"
Invoke-AzVMRunCommand -VM $vm  -CommandId 'RunPowerShellScript' -ScriptPath "<path of adding admin account command>"

so just save the first part command as a.ps1 file, and copy the path as value of you can add an local admin account to your VM.

Result: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM