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