2015-04-20 83 views
1

列表我主持的Auzre约10个虚拟机,我需要通过他们每个人的迭代,然后在他们每个人的执行PowerShell脚本,让我们说“设置日期”迭代通过虚拟机在Azure中

连接到每个虚拟机的最佳方式是什么,执行ps脚本然后断开连接?

回答

1

您可以通过扩展使用PowerShell Remoting或自定义脚本在远程VM上执行PowerShell代码。

对于这两种解决方案,您都可以使用PowerShell命令Get-AzureVM获取虚拟机列表。使用循环来迭代这些虚拟机。我在这里跳过这个部分,因为迭代是PowerShell的基础。

1. PowerShell远程

为此,您需要在远程VM启用PowerShell远程和有一个开放的端口,用于PowerShell远程处理。两者都是新VM的默认设置。

优点:该解决方案对于与远程VM交互会话非常方便。 缺点这个解决方案是,你需要验证每个虚拟机,并在执行时必须保持连接。

与每个虚拟机你可以做这样的事情。这是一个简短的例子,我在远程虚拟机上安装了ADDS。

# Prepare credentials for remote session. 
$secpasswd = ConvertTo-SecureString $AdminPassword -AsPlainText -Force 
$credentialDC1 = New-Object System.Management.Automation.PSCredential ($AdminUsername, $secpasswd) 

$EndpointDC = Get-AzureWinRMUri -ServiceName testlab-dc -Name dc1 
#$EndpointDC = Get-AzureVM -ServiceName testlab-dc -Name dc1 | Get-AzureEndpoint -Name WinRmHTTPs 

$psso = New-PSSessionOption -SkipCACheck 
$sessionDC = New-PSSession -ComputerName testlab-dc.cloudapp.net -Port $EndpointDC.Port -Credential $credentialDC1 -UseSSL -SessionOption $psso 

Invoke-Command -Session $sessionDC -ScriptBlock { 

# Set-Date or other command 
# or for example 
# Install-WindowsFeature AD-Domain-Services 

} 

Remove-PSSession -Session $sessionDC 

2.通过扩展

这里自定义脚本,你可以上传一个PowerShell文件到您的BLOB存储,然后让执行你的虚拟机的文件。要求必须在虚拟机上安装VM代理。 (默认为图库中的新虚拟机。)

优点:您无需对每个虚拟机进行身份验证,也无需在执行时保持连接。 缺点:你必须准备一个单独的PowerShell文件来上传。获得结果是异步的。

实施例:

# Upload PowerShell file 
Set-AzureStorageBlobContent -Container extensions -File "Install-ADForest.ps1" -Blob "Install-ADForest.ps1" 

# Install AD services and forrest 
Get-AzureVM -ServiceName demoext -Name demoext | 
    Set-AzureVMCustomScriptExtension -ContainerName extensions -FileName "Install-ADForest.ps1" | 
    Update-AzureVM 

该容器具有存在。在上传文件之前创建该容器。