[英]How do I restart a windows service on an Azure VM remotely from my office desktop using powershell?
I have a few Windows Server 2019 Core VMs in Azure all running the same windows service.我在 Azure 中有几个 Windows Server 2019 Core VM,它们都运行相同的 windows 服务。
I would like to run a powershell script from my computer that loops through all the VMs and restarts the service on each.我想从我的计算机上运行一个 powershell 脚本,该脚本循环遍历所有虚拟机并在每个虚拟机上重新启动服务。
I have a PS script on each VM which does this so how do I invoke them remotely?我在每个 VM 上都有一个 PS 脚本来执行此操作,那么如何远程调用它们? Is this possible without using Cloud Shell or Automation, with just a few lines of code?
如果不使用 Cloud Shell 或自动化,只需几行代码,这是否可行? I have Azure powershell tools installed on my computer too.
我的电脑上也安装了 Azure powershell 工具。
Please provide answers with a working example.请提供一个工作示例的答案。
Thank you谢谢
For your requirement, I think the simplest way is to use the Azure PowerShell command to execute the PowerShell script inside the VM, the example command here:对于您的要求,我认为最简单的方法是使用 Azure PowerShell 命令在 VM 中执行 PowerShell 脚本,示例命令如下:
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter @{param1 = "var1"; param2 = "var2"}
To execute the script in each VM, you need to make a loop with PowerShell yourself.要在每个 VM 中执行脚本,您需要自己使用 PowerShell 进行循环。 By the way, you can also use the Custom Extension for Windows .
顺便说一句,您还可以使用Windows 的自定义扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.