次の方法で共有


Sample automation script | exemple de script d’automatisation

27-MAY-2013 Update: A much more complete set of scripts has just been released last Friday. Please refer to https://blogs.msdn.com/b/windowsazure/archive/2013/05/24/automating-sharepoint-deployments-in-windows-azure-using-powershell.aspx Mise à jour du 27 MAI 2013: Un ensemble de scripts beaucoup plus complets est sorti vendredi dernier. Voir https://blogs.msdn.com/b/windowsazure/archive/2013/05/24/automating-sharepoint-deployments-in-windows-azure-using-powershell.aspx
This sample script shows how to create virtual machines in Windows Azure to prepare an installation of SharePoint Server. Le script suivant montre comment automatiser la création de machines virtuelles dans Windows Azure destinées à accueillir une installation de SharePoint Server

 

 #region set default Windows Azure subscription
Import-Module azure

#select default subscription and storage account
$subscription = 'My Azure subscription'
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccount 'mystockageazure'
Set-AzureSubscription -DefaultSubscription $subscription
#endregion

#$adminPassword="******obfuscated*****"
#region secret
$adminPassword="MWfvsoor75"
#endregion

#region other initializations
$serviceName = 'confsharepointc05'
$virtualNetworkName = 'network123'
$adminUsername = 'c05admin'

# get the credential as PSCredential (same type as what would be retrieved by the following command:
#    $credential = Get-Credential -UserName ".\${adminUsername}"
# )
$SecureStringadminPassword = ConvertTo-SecureString –String $adminPassword –AsPlainText -Force
$credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $adminUsername, $SecureStringadminPassword


# One can select an image by issuing commands like
# get-azurevmimage | select Label, ImageName, IsPremium
# get-azurevmimage | where { $_.Label -Match 'Windows Server 2012 Datacenter, Apri' }
$WindowsServer2012ImageName = 'a699494373c04fc0bc8f2bb1389d6106__Windows-Server-2012-Datacenter-201304.01-en.us-127GB.vhd'
$WindowsAndSqlServerImageName = 'fb83b3509582419d99629ce476bcb5c8__Microsoft-SQL-Server-2012SP1-Standard-CY13SU04-SQL11-SP1-CU3-11.0.3350.0-B'

#create an empty collection of VMConfigs
$vms = @()
#endregion

#region create domain controller
$DcVmName = 'C05DC1'

Write-Host "creating $DcVmName"
$adminPort = 52101

#create a new VM Config
$newVM = `
    New-AzureVMConfig -ImageName $WindowsServer2012ImageName -InstanceSize Small -Name $DcVmName `
        -AvailabilitySetName "C05DC" -DiskLabel "${DcVmName}os" `
        -HostCaching ReadWrite -Label "$DcVmName" |
    Add-AzureProvisioningConfig -Windows -AdminUsername $adminUsername -Password $adminPassword -NoRDPEndpoint |
    Add-AzureDataDisk -CreateNew -DiskSizeInGB 30 -DiskLabel "${DcVmName}data1" -LUN 0 |
    Add-AzureEndpoint -LocalPort 3389 -Name "RDP" -Protocol tcp -PublicPort $adminPort |
    Set-AzureSubnet 'DCSubnet'
    
#add the VM config to the collection
$vms += ,$newVM

#show the collection
$vms | format-table

#create the VM and wait for boot
New-AzureVM -ServiceName $serviceName -VMs $vms -VNetName $virtualNetworkName -WaitForBoot
#endregion

#region function definition for PowerShell remoting
function InstallWinRMCert($serviceName, $vmname)
{
    $winRMCert = (Get-AzureVM -ServiceName $serviceName -Name $vmname | select -ExpandProperty vm).DefaultWinRMCertificateThumbprint
 
    $AzureX509cert = Get-AzureCertificate -ServiceName $serviceName -Thumbprint $winRMCert -ThumbprintAlgorithm sha1
 
    $certTempFile = [IO.Path]::GetTempFileName()
    Write-Host $certTempFile
    $AzureX509cert.Data | Out-File $certTempFile
 
    # Target The Cert That Needs To Be Imported
    $CertToImport = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $certTempFile
 
    $store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
    $store.Certificates.Count
    $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
    $store.Add($CertToImport)
    $store.Close()
 
    Remove-Item $certTempFile
}
#endregion

#region install features on DC
$uri = Get-AzureWinRMUri -ServiceName $serviceName -Name $DcVmName
InstallWinRMCert $serviceName $DcVmName

    # Use native PowerShell Cmdlet to execute a script block on the remote virtual machine
Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential `
    -ArgumentList $SecureStringadminPassword -ScriptBlock `
{
    param($SecureStringadminPassword)

    $logLabel = $((get-date).ToString("yyyyMMddHHmmss"))
    $logPath = "$env:TEMP\init-webservervm_webserver_install_log_$logLabel.txt"
    Import-Module -Name ServerManager
    Install-WindowsFeature -Name AD-Domain-Services -IncludeManagementTools -LogPath $logPath


    $disks = Get-Disk | where { $_.NumberOfPartitions -eq 0 } 
    foreach ($d in $disks)
    {
        # there should be one disk only
        $diskNumber = $d.Number
        echo "will format disk $diskNumber"
        Initialize-Disk $diskNumber
        New-Partition -DiskNumber $diskNumber -UseMaximumSize -AssignDriveLetter
        Format-Volume -DriveLetter F -Confirm:$False
        get-volume -DriveLetter F
    }

    Import-module ADDSDeployment
    Install-ADDSForest -DomainName "c05.compiouting.fr" -InstallDns:$true -DatabasePath "F:\NTDS" `
        -LogPath "F:\NTDS" -SysvolPath "F:\SYSVOL" -NoRebootOnCompletion:$false -Force:$true `
        -SafeModeAdministratorPassword $SecureStringadminPassword
} 
#endregion

#region wait for reboot and start a new collection
Start-Sleep -Seconds 45

$DcVm = Get-AzureVM -ServiceName $serviceName -Name $DcVmName
While ($DcVm.InstanceStatus -ne "ReadyRole")
{
    write-host "Waiting for DC to be ready... Current Status = " $DcVm.InstanceStatus
    Start-Sleep -Seconds 15
    $DcVm = Get-AzureVM -ServiceName $serviceName -Name $DcVmName
}

$vms = @()
#endregion

#region SQL Server
$prefix = 'C05SQL'
for($i=1; $i -le 2; $i++)
{
    Write-Host "creating $prefix${i}"
    $adminPort = 53100 + $i

    #create a new VM Config
    $newVM = `
        New-AzureVMConfig -ImageName $WindowsAndSqlServerImageName -InstanceSize Small -Name "$prefix$i" `
            -AvailabilitySetName "$prefix" -DiskLabel "$prefix${i}os" `
            -HostCaching ReadWrite -Label "$prefix${i}" |
        Add-AzureProvisioningConfig -WindowsDomain -AdminUsername $adminUsername -Password $adminPassword `
        -Domain "c05" -DomainUserName $adminUsername -DomainPassword $adminPassword -JoinDomain "c05.compiouting.fr" `
        -NoRDPEndpoint |
        Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -LUN 0 -DiskLabel "$prefix${i}data1" |
        Add-AzureEndpoint -LocalPort 3389 -Name "RDP" -Protocol tcp -PublicPort $adminPort |
        Set-AzureSubnet 'SQLSubnet'
    
    #add the VM config to the collection
    $vms += ,$newVM
}
#endregion

#region SharePoint Server
$prefix = 'C05SP'
for($i=1; $i -le 2; $i++)
{
    Write-Host "creating $prefix$i"
    $adminPort = 52200 + $i

    #create a new VM Config
    $newVM = `
        New-AzureVMConfig -ImageName $WindowsServer2012ImageName -InstanceSize Small -Name "$prefix$i" `
            -AvailabilitySetName "$prefix" -DiskLabel "$prefix${i}os" `
            -HostCaching ReadWrite -Label "$prefix${i}" |
        Add-AzureProvisioningConfig -WindowsDomain -AdminUsername $adminUsername -Password $adminPassword `
        -Domain "c05" -DomainUserName $adminUsername -DomainPassword $adminPassword -JoinDomain "c05.compiouting.fr" `
        -NoRDPEndpoint |
        Add-AzureDataDisk -CreateNew -DiskSizeInGB 50 -LUN 0 -DiskLabel "$prefix${i}data1" |
        Add-AzureEndpoint -LocalPort 3389 -Name "RDP" -Protocol tcp -PublicPort $adminPort |
        Add-AzureEndpoint -LocalPort 80 -Name "Web" -Protocol tcp -PublicPort 80 `
            -LBSetName "SPWebLB" -ProbePort 8080 -ProbeProtocol http -ProbePath '/' |
        Set-AzureSubnet 'SP2013Subnet'
    
    #add the VM config to the collection
    $vms += ,$newVM
}
#endregion

#region create SharePoint and SQL Server VMs and wait for them to boot
#show the collection
$vms | format-table

#create the VM and wait for boot
New-AzureVM -ServiceName $serviceName -VMs $vms -WaitForBoot
#endregion


#region install features on SharePoint Servers
$prefix = 'C05SP'
for($i=1; $i -le 2; $i++)
{
    $vmName = "$prefix$i"

    $uri = Get-AzureWinRMUri -ServiceName $serviceName -Name $vmName
    InstallWinRMCert $serviceName $vmName

    # Use native PowerShell Cmdlet to execute a script block on the remote virtual machine
    Invoke-Command -ConnectionUri $uri.ToString() -Credential $credential -ScriptBlock `
    {
        $logLabel = $((get-date).ToString("yyyyMMddHHmmss"))
        $logPath = "$env:TEMP\init-webservervm_webserver_install_log_$logLabel.txt"
        Import-Module -Name ServerManager
        Install-WindowsFeature -Name Web-Server -IncludeAllSubFeature -IncludeManagementTools -LogPath $logPath
    }
}
#endregion

 

 

In order to stop and remove the VMs and their associated Azure disks and VHD, you can use the following script (with care!) de façon à arrêter et supprimer tout (VMs, disques Azure associés et VHD), vous pouvez utiliser (avec précautions!) le script suivant

 

 #region init
Import-Module azure

$subscription = 'My Azure subscription'
Set-AzureSubscription -SubscriptionName $subscription -CurrentStorageAccount 'mystockageazure'
Set-AzureSubscription -DefaultSubscription $subscription

$cloudSvcName = 'confsharepointc05'
#endregion

#region shutdown and delete
echo 'will shut down and remove the following'
#$vms = Get-AzureVM -ServiceName $cloudSvcName | where { ($_.name -NotMatch 'SLB') } 
$vms = Get-AzureVM -ServiceName $cloudSvcName 
echo $vms | select name

$AzureDisks = @()

foreach ($vm in $vms)
{
    foreach($d in $vm | Get-AzureOSDisk)
    {
        $AzureDisks += ,$d
    }
    foreach($d in $vm | Get-AzureDataDisk)
    {
        $AzureDisks += ,$d
    }
}

echo 'will remove the following Azure Disks'
foreach($d in $AzureDisks)
{
    echo $d.DiskName
}

$vms | Stop-AzureVM
$vms | Remove-AzureVM

Start-Sleep -Seconds 30

foreach($d in $AzureDisks)
{
    Remove-AzureDisk -DiskName $d.DiskName -DeleteVHD
}
#endregion

 

Smile

Benjamin