3
我编写了一个简单的powershell脚本来修改远程计算机上的hosts文件,但出了问题。如何将param值传递给Invoke-Command cmdlet?
脚本:
param(
[string]$value
)
$username = 'username'
$password = 'password'
$hosts = "172.28.30.45","172.28.30.46"
$pass = ConvertTo-SecureString -AsPlainText $password -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $username,$pass
ForEach ($x in $hosts){
echo "Write in $x , value: $value"
Invoke-Command -ComputerName $x -ScriptBlock {Add-Content -Path "C:\Windows\system32\drivers\etc\hosts" -Value $value} -Credential $cred
echo "Finish writing."
}
echo "End of PS script."
运行时,它写入每个主机文件的新空行。此行echo "Write in $x , value: $value"
显示$值值。 我做错了什么?
谢谢Martin。 – GVArt