简体   繁体   English

如何连接到Azure Windows VM并使用PowerShell运行远程脚本?

[英]How Connect to a Azure Windows VM and run a remote script with PowerShell?

I am familiar with Linux envs and using SSH to run remote scripts and programs and automatic scripts from my desktop. 我熟悉Linux env,并使用SSH从桌面运行远程脚本和程序以及自动脚本。

I would like to have a similar workflow with Windows VMs that I have on my Azure Account. 我希望在Azure帐户上拥有与Windows VM类似的工作流。 However, I can´t find a straight forward instructions on how to build my local PowerShell scripts. 但是,我找不到关于如何构建本地PowerShell脚本的直接说明。

I need only to connect to a VM and call some scripts within it. 我只需要连接到VM并在其中调用一些脚本即可。

The best I could find would be this guide from MS https://docs.microsoft.com/en-us/azure/virtual-machines/windows/winrm 我能找到的最好的方法是来自MS https://docs.microsoft.com/zh-cn/azure/virtual-machines/windows/winrm的本指南

Or this a litte older blog post. 还是这是一篇比较老的博客文章。

http://fabriccontroller.net/using-remote-powershell-with-windows-azure-virtual-machines/ http://fabriccontroller.net/using-remote-powershell-with-windows-azure-virtual-machines/

According to your description, we can use New-Pssession to execute script to stop/start service, like this: 根据您的描述,我们可以使用New-Pssession执行脚本来停止/启动服务,如下所示:

$username = 'jason'
$pass = ConvertTo-SecureString -string 'password' -AsPlainText -Force
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $pass
$s = New-PSSession -ConnectionUri 'http://23.99.82.2:5985' -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck)
Invoke-Command -Session $s -ScriptBlock {Get-Process PowerShell}

Result like this: 结果如下: 在此处输入图片说明 在此处输入图片说明

Another way, we can use Azure custom script extension to run script, we can upload script to Azure storage account, and use Set-AzureRmVMCustomScriptExtension to set custom script: 另一种方法,我们可以使用Azure自定义脚本扩展来运行脚本,可以将脚本上传到Azure存储帐户,并使用Set-AzureRmVMCustomScriptExtension来设置自定义脚本:

PS C:\> Set-AzureRmVMCustomScriptExtension -ResourceGroupName "ResourceGroup11" -Location "Central US" -VMName "VirtualMachine07" -Name "ContosoTest" -TypeHandlerVersion "1.1" -StorageAccountName "Contoso" -StorageAccountKey <StorageKey> -FileName "ContosoScript.exe" -ContainerName "Scripts"

But custom script only can run one time, if you want to re-run this script, we should remove it with this command Remove-AzureRmVMCustomScriptExtension , then re-set it. 但是自定义脚本只能运行一次,如果要重新运行此脚本,我们应该使用此命令Remove-AzureRmVMCustomScriptExtension将其Remove-AzureRmVMCustomScriptExtension ,然后重新设置它。 More information about Azure custom script extension, please refer to this link . 有关Azure自定义脚本扩展的更多信息,请参考此链接

I ran into a lot of trouble using the accepted answer, and found I wanted to use SSL in my remote execution. 使用接受的答案时,我遇到了很多麻烦,发现我想在远程执行中使用SSL。 I could not find anywhere this was succinctly put, so here's what worked for me. 我找不到在这个地方简明放置的东西,所以这对我有用。 Essentially, use the built-in Azure command to enable remote PowerShell on the VM, and then run secure remote sessions to your heart's content! 本质上,请使用内置的Azure命令在VM上启用远程PowerShell,然后运行安全的远程会话来满足您的需求!

Invoke-AzureRmVMRunCommand -ResourceGroupName $vmResourceGroupName -Name $vmName -CommandId 'EnableRemotePS'
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $username, $secureStringPassword
$sessionOptions = New-PSSessionOption -SkipCACheck -SkipCNCheck                 
Invoke-Command -ComputerName $ipAddress -Credential $cred -UseSSL -SessionOption $sessionOptions -FilePath $scriptPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure Runbook - 在远程 VM 上运行 Powershell - Azure runbook - run Powershell on remote VM 无法运行位于远程 Azure VM D 中的远程 powershell 脚本:使用 Invoke-Command 的驱动器 - Unable to run remote powershell script which is located in remote Azure VM D: drive using Invoke-Command 如何在使用ARM模板的Azure VM部署期间运行PowerShell脚本? - How to run a PowerShell script during Azure VM deployment with ARM template? 无法在具有Windows Powershell远程功能的Windows Server Azure VM上运行任何程序,没有错误 - Can not run any program on windows server Azure VM with windows powershell remote, No error 在预配的 Azure VM 上运行自定义 Powershell 脚本 - Run custom Powershell script on provisioned Azure VM Azure VM中的远程Powershell - remote powershell in azure VM 如何对Azure Data Factory V2中的现有Azure VM运行远程命令(PowerShell / Bash)? - How to run a remote command (powershell/bash) against an existing Azure VM in Azure Data Factory V2? Sysprep Azure VM 与远程 PowerShell - Sysprep Azure VM with Remote PowerShell 从 git 存储库运行 Azure VM 扩展 Powershell 脚本 - Run Azure VM Extension Powershell script from git repository Azure VM 运行 PowerShell 脚本不 Output - Azure VM Run PowerShell Script doesn't Output
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM