[英]How to run powershell code in every virtual machine in the host
I am newbie to powershell, I have a powershell script That should be run on every virtual machine in my host at same time. 我是Powershell的新手,我有一个Powershell脚本,该脚本应同时在主机中的每个虚拟机上运行。 One of my friend told me that there is a tool which is developed by Phillips but i searched for it but unable to find. 我的一个朋友告诉我,有一种工具是菲利普斯(Phillips)开发的,但我进行了搜索,但找不到。 can some one help me with solution are there is any tool for that. 有人可以帮我解决问题的方法吗?
Thanks in advance 提前致谢
You can create your script inside a script block and run it while enumerating the VMs. 您可以在脚本块中创建脚本并在枚举VM的同时运行它。
# On the host, Get-VM get's you the available VMs.
$runniungVMs = $Get-VM -State 'Running'
foreach ($vm in $runningVMs) {
Enter-PSSession $vm
# Write Some code to execute after this line
}
Invoke-Command –ComputerName <computerName> -Credential $Cred –ScriptBlock {
# Write some code to execute
}
Update on additional questions 有关其他问题的更新
The Get-VM command gets the list of virtual machines available on the host where the command is running. Get-VM命令获取运行命令的主机上可用的虚拟机列表。
If your VM host is not the machine you are running on you can specify the computer name of the VM host. 如果您的VM主机不是正在运行的计算机,则可以指定VM主机的计算机名称。 Here is how to get the list of running VMs on Server1: 以下是获取Server1上正在运行的VM的列表的方法:
Get-VM -ComputerName Server1 | Where-Object {$_.State -eq 'Running'}
Posh also comes with great help. 时髦也有很大的帮助。 If you need to find out more about any command you can use: 如果您需要更多有关任何命令的信息,可以使用:
get-help Get-VM
or 要么
get-help Get-VM -examples
get-help Get-VM -detailed
get-help Get-VM -full
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.