通过Powershell远程管理Windows Azure上的虚拟机

简介:

Windows Azure上连接虚拟机想必不是件难事尤其是连接Windows操作系统简单点几下鼠标通过远程桌面RDPWindows Azure虚拟机会帮助你自动创建远程连接RDP的Profile你就能体验到公有云带来的便利。由于虚拟机外部连接都是通过端口映射连接的。当然基于区域网络目前Azure.CN中新创建的虚拟网络已经都是区域网络了当然你目前仍然可以创建基于地缘组的虚拟网络虽然并不推荐后可以创建虚拟机实例级别的公共IP地址所以你也可以跳过通过"云服务"端口映射的RDP而直接连接虚拟机公共IP的3389端口。好吧说了这么多都是通过远程桌面对远程虚拟机进行管理的如果需要批量对虚拟机进行管理有什么方法呢在全球运营的Microsoft Azure上已经提供了自动化as a Service的自动化云服务 通过Powershell工作流运行手册批量定时管理虚拟机服务当然听上去很酷不过目前国内的自动化服务还要等等呢。

可是如果我们希望通过Powershell脚本批量管理在国内Azure公有云上的虚拟机该如何操作呢这里把我用于演示的脚本和大家分享一下。

首先下载并安装最新的 Azure Powershell 注意本脚本只是在Powershell 4.0环境测试通过Azure中创建的Windows服务器虚拟机自动provision部署的时候后台会自动帮助启用Powershell基于https的WINRM访问而用到的证书正是"云服务FQDN"证书这个可以通过以下方式验证在Azure管理门户云服务证书中查看

来到Azure虚拟机中查看配置的Powershell已经启用了基于https的访问并且配置了访问证书的指纹就是"云服务"配置的证书指纹因此想通过Powershell远程访问云中的Windows 服务器虚拟机则需要再访问客户端安装相应的证书文件到本地受信任证书列表 CurrentUser\My下面脚本中就是将该证书安装到远程管理客户机的该位置。


(*注意* 这里的脚本没有做"云服务"下的虚拟机操作系统判断因为演示环境中所有虚拟机均为Windows Server 2012 R2的虚拟机。)

演示脚本远程执行脚本Invoke-command 的scriptblock中主要用于在Azure虚拟机中添加配置用户体验服务可以根据需要进行修改,另外如果需要交互式的环境也可以通过Enter-PSSession URI连接虚拟机的URI管理。

实际演示中需要指定参数订阅名称云服务名称 Remote-configAzVM.ps -Subscriptionname "订阅名"-Servicename "云服务名"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Param
(
     [ Parameter ( Mandatory = $false , Position =0)]
     [String] $SubscriptionName ,
     [ Parameter ( Mandatory = $false )]
     [String] $Servicename ,
      [ Parameter ( Mandatory = $false )]
     [String] $Path  = ( Get-Location )
)
  
  
# Elevate to admin
Write-Host  "Checking for elevation... "  -NoNewline
$CurrentUser  New-Object  Security.Principal.WindowsPrincipal $( [Security.Principal.WindowsIdentity] ::GetCurrent())
if  (( $CurrentUser .IsInRole( [Security.Principal.WindowsBuiltinRole] ::Administrator))  -eq $false ) {
     $ArgumentList "-noprofile-noexit -file `"{0}`" -Path `"$Path`""
     If($DeploymentOnly) {$ArgumentList = $ArgumentList +"  -DeploymentOnly "}
     Write-Host" elevating "
     Start-Processpowershell.exe -VerbRunAs -ArgumentList($ArgumentList -f($myinvocation.MyCommand.Definition))
     Exit
}
  
  
Select-AzureSubscription -SubscriptionName $SubscriptionName -Current
$Validate = $true
  
# Check Current PS Version
If ($PSVersionTable.PSVersion.Major -lt 4) {Write-Error " Only Supports PowerShell Version 4 or Higher! ";$Validate =$false} 
  
if ($Validate)
{
function Install-WinRmCertificate($ServiceName, $VMName)
{
     $vm= Get-AzureVM-ServiceName $ServiceName-Name $VMName
     $winRmCertificateThumbprint= $vm.VM.DefaultWinRMCertificateThumbprint
     
     $winRmCertificate= Get-AzureCertificate-ServiceName $ServiceName`
         -Thumbprint$winRmCertificateThumbprint -ThumbprintAlgorithm sha1
     
     $installedCert= Get-Item Cert:\CurrentUser\My\$winRmCertificateThumbprint-ErrorAction SilentlyContinue
     
     if($installedCert -eq$null)
     {
         $certBytes= [System.Convert]::FromBase64String($winRmCertificate.Data)
         $x509Cert= New-ObjectSystem.Security.Cryptography.X509Certificates.X509Certificate
         $x509Cert.Import($certBytes)
         
         $store= New-ObjectSystem.Security.Cryptography.X509Certificates.X509Store" Root "," LocalMachine "
         $store.Open(" ReadWrite")
         $store .Add( $x509Cert )
         $store .Close()
     }
}
  
$VMnames  = ( Get-AzureVM  -ServiceName  $ServiceName ).HostName
foreach  ( $VMname  in  $VMnames )
     {
     Install-WinRmCertificate-ServiceName  $ServiceName -VMName  $VMname
     $VMwinRmUri Get-AzureWinRMUri -ServiceName  $ServiceName -Name  $VMname
     $credential Get-Credential
  
     Start-Job -ScriptBlock{
             Invoke-Command  -URI  $VMwinRmUri  -Credential  $credential  -ScriptBlock {
                     Install-WindowsFeature -nameDesktop-Experience `
                     -IncludeAllSubFeature - Restart-Force }} 
     }
}









本文转自 翟老猫 51CTO博客,原文链接:http://blog.51cto.com/3387405/1586497,如需转载请自行联系原作者
目录
相关文章
|
20天前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
225 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
28天前
|
Linux 虚拟化 iOS开发
VMware Fusion 25H2 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
VMware Fusion 25H2 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
298 0
VMware Fusion 25H2 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
|
1月前
|
Unix Shell Windows
Windows PowerShell技巧:使用findstr实现类似grep的功能
显示带有线路编号**: `/N`选项将显示每条结果前面带有其在线路上出现位置编号。
273 7
|
1月前
|
存储 Windows
Windows PowerShell操作:如何删除环境变量
此外,还有一些第三方工具可以用来管理环境变量,这些工具通常提供了更为用户友好的界面来添加、编辑或删除环境变量,但是使用PowerShell可以更直接地控制这些设置,并且可以很容易地集成到脚本中以自动化环境配置的管理。
250 7
|
3月前
|
Linux 虚拟化 iOS开发
Windows Server 2025 OVF (2025 年 8 月更新) - VMware 虚拟机模板
Windows Server 2025 OVF (2025 年 8 月更新) - VMware 虚拟机模板
357 3
Windows Server 2025 OVF (2025 年 8 月更新) - VMware 虚拟机模板
|
2月前
|
数据安全/隐私保护
【Azure Function App】PowerShell Function 执行 Get-AzAccessToken 的返回值类型问题:System.String 与 System.Security.SecureString
将PowerShell Function部署到Azure Function App后,Get-AzAccessToken返回值类型在不同环境中有差异。正常为SecureString类型,但部分情况下为System.String类型,导致后续处理出错。解决方法是在profile.ps1中设置环境变量$env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN=false,以禁用明文输出。
|
5月前
|
Linux 虚拟化 iOS开发
Windows Server 2016 OVF (2025 年 6 月更新) - VMware 虚拟机模板
Windows Server 2016 OVF (2025 年 6 月更新) - VMware 虚拟机模板
113 9
Windows Server 2016 OVF (2025 年 6 月更新) - VMware 虚拟机模板
|
5月前
|
Linux 虚拟化 iOS开发
Windows Server 2022 OVF (2025 年 6 月更新) - VMware 虚拟机模板
Windows Server 2022 OVF (2025 年 6 月更新) - VMware 虚拟机模板
350 6
Windows Server 2022 OVF (2025 年 6 月更新) - VMware 虚拟机模板
|
4月前
|
Linux 虚拟化 iOS开发
VMware Fusion 13.6.4 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
VMware Fusion 13.6.4 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
506 3
|
4月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
911 0
VMware Remote Console 13.0.0 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端