$keyFile = "D:\vm\linux.key"
$key = New-Object Byte[] 32
$key | out-file $keyFile

Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString -key $key | Out-File "D:\vm\linux.txt"

$userName = "root"
$passwdFile = "D:\vm\linux.txt"
$keyFile = "D:\vm\linux.key"
$key = Get-Content $keyFile
$Cred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $userName, (Get-Content $passwdFile | ConvertTo-SecureString -Key $key)

$vms = Get-Content D:\VM\vm2.txt

echo $vms

foreach ($vm in $vms)

{

$st = "hostnamectl set-hostname $vm"

Invoke-VMScript -VM $vm -ScriptType Bash -ScriptText $st -GuestCredential $Cred

}