【问题标题】:Start and stop windows service on a Azure VM using powershell使用 powershell 在 Azure VM 上启动和停止 Windows 服务
【发布时间】:2018-08-24 07:31:04
【问题描述】:

使用 powershell,我需要执行以下步骤。
1.登录Azure账号。
2. 从映像创建 VM。
3.镜像中有windows服务。所以当我们创建VM的时候,那个windows服务已经在上面运行了。停止那个服务。
4. 将一些二进制文件复制到 VM 中的特定文件夹中。
5. 再次启动服务。

在所有这些中,我能够完成第 1 步和第 2 步。而且我找不到其他步骤的任何相关程序。下面是我用来做 1 和 2 的脚本。

Login-AzureRMAccount    

$UserName = "username@organization.com"
$Password = ConvertTo-SecureString "password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)    
New-AzureRmVm `
    -ResourceGroupName "RGN" `
    -Name "VMName" `
    -ImageName "ImageName" `
    -Location "West US" `
    -Credential $psCred

那么任何人都可以帮助我或指导我如何完成其​​余步骤。
提前感谢您的宝贵时间。

EDIT : 停止服务的命令是
Stop-Service -Name "CPS Dev UniSimServices" -Force

但是,如果我登录到 VM 并运行此命令,这将有效。我想从远程机器本身通过powershell连接到新创建的VM(不登录),然后停止服务。我该怎么做?

【问题讨论】:

    标签: azure powershell windows-services azure-virtual-machine


    【解决方案1】:

    您可以使用扩展程序与虚拟机交互,而无需连接到它。您可以将 PowerShell 命令与自定义脚本一起使用:

    Set-AzureRmVMExtension -ResourceGroupName "myResourceGroupAutomate" `
        -ExtensionName "IIS" `
        -VMName "myVM" `
        -Location "EastUS" `
        -Publisher Microsoft.Compute `
        -ExtensionType CustomScriptExtension `
        -TypeHandlerVersion 1.8 `
        -SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
    

    更新

    如果你真的想为扩展使用自定义脚本,你可以使用如下命令:

    Set-AzureRmVMCustomScriptExtension -ResourceGroupName myResourceGroup `
        -VMName myVM `
        -Location myLocation `
        -FileUri myURL `
        -Run 'myScript.ps1' `
        -Name DemoScriptExtension
    

    更多详情请见Set-AzureRmVMCustomScriptExtension

    【讨论】:

    • 感谢您的回复。在这里,计算机名和虚拟机名是一样的吗?
    • @CrazyCoder 是的,虚拟机内的计算机名称与您创建的虚拟机名称相同。
    • @CrazyCoder 如果答案有帮助或需要更多帮助,请告诉我。
    • 我应该在哪里编写脚本来停止服务?我无法理解这段代码到底在做什么。如果可能的话,你能详细解释一下吗。谢谢
    • @CrazyCoder 您可以使用 PowerShell cmdlet Set-AzureRmExtensioncommandToExecute 中添加 cmdlet。或者,您可以将自定义脚本与 PowerShell Set-AzureRmVMCustomScriptExtension 一起使用。你可以看看这个link
    【解决方案2】:

    要停止服务,请使用以下命令 Stop-Service -Name "NameOfService" -Force,如有任何问题,请查看 Stop-Service 的文档。然后通过 robocopy、Copy-Item 或其他任何方式以及 Start-Service -Name "NameOfService" 复制您需要的文件。如果您在将文件从本地或远程计算机复制到 VM 时遇到任何问题,只需发布​​即可。

    正如另一个答案中所说,您可以使用Set-AzureRmVMCustomScriptExtension,以下示例在 VM 中执行脚本上传到 Blob 存储帐户

    Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroup -Location $LocationName -VMName $ServersName -Name $scriptName -StorageAccountName $NameStorageAccount -FileName $fileName -Run $fileName -ContainerName $NameCOntainerStorage -Argument "-param1 $param1" 
    

    另一个选项是使用命令Invoke-AzureRmVMRunCommand,例如

    Invoke-AzureRmVMRunCommand -ResourceGroupName $ResourceGroup -Name $ServersName -CommandId 'RunPowerShellScript' -ScriptPath "pathToFileExecute" -Parameter @{"Param" = "$Param"}
    

    注意:参数 -ScriptPath 可以是共享路径,例如 \VM\e$\script.ps1 或本地路径。

    最后一个选项是使用Invoke-Command,例如:

    Invoke-command -ComputerName $server -crdential $objectcredential -scriptblock {Stop-Service -Name "nameService" -force}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 2022-10-07
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多