Windows Azure : Script PowerShell pour intialiser les Striped Disks sur une VM

Après avoir créé une machine virtuelle SQL Server 2014 dans le post précédent, il est temps de créer le disque “Physique” assemblant les 8 vhd installés avec la machine.
Nous continuons à utiliser la même console PowerShell (Sinon réinitialiser les varibles du context) et exéctuer en remote PowerShell le script permettant de créer les différents StoragePool et les VirualDisk. Attention la console doit etre lancée en mode administrateur pour pouvoir installer le certificat.

  1. Récupérer l’objet de pour l’exécution à distance un script Powershell
    $winRmUri = Get-AzureWinRMUri -ServiceName $ServiceName -Name $ComputerName
  2. Installer le certificat de sécurité pour pouvoir exécuter un remote script
    <# 
    .SYNOPSIS 
       Installs a Windows Remote Management (WinRm) certificate to the local store 
    .DESCRIPTION 
       Gets the WinRM certificate from the Virtual Machine in the Service Name specified, and  
       installs it on the Current User's personal store.  
    .EXAMPLE 
        Install-WinRmCertificate -ServiceName testservice -vmName testVm 
    .INPUTS 
       None 
    .OUTPUTS 
       None 
    #> 
    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-Object System.Security.Cryptography.X509Certificates.X509Certificate 
            $x509Cert.Import($certBytes) 
             
            $store = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine" 
            $store.Open("ReadWrite") 
            $store.Add($x509Cert) 
            $store.Close() 
        } 
    }
    Install-WinRmCertificate $ServiceName $ComputerName 
  3. Définir le script de création des StoragePools et VirtualsDisk à lancer en remote
  4. $setDiskStripingScript =  
    { 
        param ([Int] $numberOfPools, [Int] $numberOfDisksPerPool) 
         
        Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 
         
        $uninitializedDisks = Get-PhysicalDisk -CanPool $true  
         
        $virtualDiskJobs = @() 
         
        for ($index = 0; $index -lt $numberOfPools; $index++) 
        {          
            $poolDisks = $uninitializedDisks | Select-Object -Skip ($index * $numberOfDisksPerPool) -First $numberOfDisksPerPool  
            $poolName = "Pool" + $index 
            $newPool = New-StoragePool -FriendlyName $poolName -StorageSubSystemFriendlyName "Storage Spaces*" -PhysicalDisks $poolDisks 
            $virtualDiskJobs += New-VirtualDisk -StoragePoolFriendlyName $poolName  -FriendlyName $poolName -ResiliencySettingName Simple -ProvisioningType Fixed -Interleave 1048576 -NumberOfDataCopies 1 -NumberOfColumns $numberOfDisksPerPool -UseMaximumSize -AsJob 
        } 
        Receive-Job -Job $virtualDiskJobs -Wait 
        Wait-Job -Job $virtualDiskJobs                         
        Remove-Job -Job $virtualDiskJobs 
        # Initialize and format the virtual disks on the pools 
        $formatted = Get-VirtualDisk | Initialize-Disk -PassThru | New-Partition -AssignDriveLetter -UseMaximumSize | Format-Volume -FileSystem NTFS -Confirm:$false 
    
        # Create the data directory 
        $formatted | ForEach-Object { 
            # Get current drive letter. 
            $downloadDriveLetter = $_.DriveLetter 
             
            # Create the data directory 
            $dataDirectory = "$($downloadDriveLetter):\DATA" 
             
            New-Item $dataDirectory -Type directory -Force | Out-Null 
        } 
        # Dive time to the storage service to pick up the changes 
        Start-Sleep -Seconds 120 
    }
    
  5. Exécuter la commande PowerShell pour lancer en remote le script ci-dessus avec comme paramétrage :
    1 Storage Pool
    8 Disk sur le même pool
    Invoke-Command -ConnectionUri $winRmUri.ToString() -Credential $credential -ScriptBlock $setDiskStripingScript -ArgumentList @(1, $NumberOfDisks)

About Wicem

One comment

  1. This is a topic which is close to my heart… Cheers!
    Where aare your contact details though?

Leave a Reply

Your email address will not be published. Required fields are marked *