简体   繁体   English

如何在主机中的每个虚拟机中运行Powershell代码

[英]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
}

You can also use a script block inside a loop if you can get the actual computer name of the VM: 如果可以获得虚拟机的实际计算机名称,也可以在循环内使用脚本块:

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.

相关问题 如何从 C# 中的 PowerShell 脚本运行 Azure 虚拟机 - How to run Azure virtual machine from PowerShell script in C# 如何使用 Powershell 从我的本地计算机在 Azure Windows 虚拟机 (VM) 中运行“sysprep /generalize”? - How to run "sysprep /generalize” in Azure Windows Virtual Machine (VM) from my local machine using Powershell? PowerShell如何将vswitch附加到虚拟机 - PowerShell How to attach a vswitch to virtual machine 通过Powershell创建Azure虚拟机后如何运行Windows Update? - How to run windows update after creation of azure virtual machine via powershell? Azure python SDK 在虚拟机内部运行 powershell 脚本 - Azure python SDK run powershell script inside a Virtual Machine 在 System Center Virtual Machine Manager 上执行 powershell 代码时出错 - Error while execution powershell code on System Center Virtual Machine Manager 如何在未安装PowerShell的计算机上部署Powershell 2.0定制主机应用程序? - How to deploy a Powershell 2.0 custom host application on a machine with no PowerShell installed? 如何使用Powershell为虚拟机远程扩展分区 - how to extend partition remotely for a virtual machine using powershell PowerShell:如何使用WMI在远程计算机上运行powershell脚本 - PowerShell: How to run a powershell script on a remote machine using WMI 如何获取Powershell域内每台机器的Windows版本? - How to get the Windows version of every machine on a domain in Powershell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM