通过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,如需转载请自行联系原作者
目录
相关文章
|
7月前
|
安全 Linux 开发工具
【Azure 环境】Azure 虚拟机上部署 DeepSeek R1 模型教程(1.5B参数)【失败】
遇见错误一:operator torchvision::nms does not exist 遇见错误二:RuntimeError: Failed to infer device type
526 22
|
Linux C++ Windows
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
132 0
【Azure 应用服务】Azure App Service(Windows)环境中如何让.NET应用调用SAP NetWeaver RFC函数
|
10月前
|
安全 Windows
【Azure Cloud Service】在Windows系统中抓取网络包 ( 不需要另外安全抓包工具)
通常,在生产环境中,为了保证系统环境的安全和纯粹,是不建议安装其它软件或排查工具(如果可以安装,也是需要走审批流程)。 本文将介绍一种,不用安装Wireshark / tcpdump 等工具,使用Windows系统自带的 netsh trace 命令来获取网络包的步骤
203 32
|
10月前
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
158 11
|
Windows
Powershell 重新排列去重 Windows环境变量
【9月更文挑战第13天】本文介绍如何使用PowerShell对Windows环境变量进行重新排列和去重。首先通过`$env:`访问环境变量,接着使用`-split`命令分割路径,再利用`Select-Object -Unique`去除重复项。之后可根据需要对路径进行排序,最后将处理后的路径组合并更新环境变量。注意修改环境变量前应备份重要数据并了解潜在影响。
344 10
|
11月前
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
435 0
|
PHP Windows
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
【Azure App Service for Windows】 PHP应用出现500 : The page cannot be displayed because an internal server error has occurred. 错误
208 1
|
网络安全 API 数据安全/隐私保护
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Windows)
114 0
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
107 0
|
应用服务中间件 nginx Windows
【Azure 应用服务】在App Service for Windows中实现反向代理
【Azure 应用服务】在App Service for Windows中实现反向代理
129 0