如何使用 PowerShell 连接到 Azure Windows VM 和 运行 远程脚本?
How Connect to a Azure Windows VM and run a remote script with PowerShell?
我熟悉 Linux 环境并使用 SSH 从我的桌面 运行 远程脚本和程序以及自动脚本。
我希望我的 Azure 帐户上的 Windows 个 VM 具有类似的工作流程。但是,我找不到关于如何构建本地 PowerShell 脚本的直接说明。
我只需要连接到 VM 并在其中调用一些脚本。
我能找到的最好的就是这份来自 MS 的指南
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/winrm
或者这是一个稍旧的博客 post。
http://fabriccontroller.net/using-remote-powershell-with-windows-azure-virtual-machines/
根据您的描述,我们可以使用New-Pssession
来执行脚本到stop/start服务,像这样:
$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}
结果如下:
另一种方法,我们可以使用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"
但是自定义脚本只能运行一次,如果你想重新运行这个脚本,我们应该用这个命令Remove-AzureRmVMCustomScriptExtension
删除它,然后重新设置它.
更多关于 Azure 自定义脚本扩展的信息,请参考这个 link.
我 运行 在使用接受的答案时遇到了很多麻烦,并且发现我想在我的远程执行中使用 SSL。我找不到任何简洁的地方,所以这对我有用。本质上,使用内置的 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
我熟悉 Linux 环境并使用 SSH 从我的桌面 运行 远程脚本和程序以及自动脚本。
我希望我的 Azure 帐户上的 Windows 个 VM 具有类似的工作流程。但是,我找不到关于如何构建本地 PowerShell 脚本的直接说明。
我只需要连接到 VM 并在其中调用一些脚本。
我能找到的最好的就是这份来自 MS 的指南 https://docs.microsoft.com/en-us/azure/virtual-machines/windows/winrm
或者这是一个稍旧的博客 post。
http://fabriccontroller.net/using-remote-powershell-with-windows-azure-virtual-machines/
根据您的描述,我们可以使用New-Pssession
来执行脚本到stop/start服务,像这样:
$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}
结果如下:
另一种方法,我们可以使用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"
但是自定义脚本只能运行一次,如果你想重新运行这个脚本,我们应该用这个命令Remove-AzureRmVMCustomScriptExtension
删除它,然后重新设置它.
更多关于 Azure 自定义脚本扩展的信息,请参考这个 link.
我 运行 在使用接受的答案时遇到了很多麻烦,并且发现我想在我的远程执行中使用 SSL。我找不到任何简洁的地方,所以这对我有用。本质上,使用内置的 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