Updating text file on Azure Windows VM from local text file with PowerShell
In order for NFS identity mapping to work accross both platforms (Linux and Windows), pending either AD integration or fixing Windows NFS client integration with OpenLDAP server (neither of which is currently in place), we are currently maintaining a passwd file for Windows that maps the Windows identity to the corresponding UID/GID.
To update the file, from PowerShell, assuming the updated file is presently available in the current directory and called passwd
:
$passwd_content=$( (Get-Content passwd) -Join "`n" )
az vm run-command invoke --name "myvm" --resource-group "my-RG" --command-id RunPowerShellSCript --scripts "
Set-Content C:\\Windows\\System32\\drivers\\etc\\passwd '$passwd_content'
"
It took some fiddling with Get-Content
and quotes to get the correct line-endings on the VM (a stright copy of the file does not work, depending on the format of the input file). This seems to work regardless of Unix (\n
) or Windows (\r\n
) style endings in the local passwd
.
Do a number of VMs with sequential digits at the end of their name:
$passwd_content=$( (Get-Content passwd) -Join "`n" )
# Update myvm-[18-23]
for($i=18; $i -lt 24; $i++) {
az vm run-command invoke --name "myvm-$i" --resource-group "my-RG" --command-id RunPowerShellSCript --scripts "
Set-Content C:\\Windows\\System32\\drivers\\etc\\passwd '$passwd_content'
"
}