# AddNewClusteredVM.ps1 # This script is expected to work with DPM 2010 Beta # 1. Parameters supplied to the script param([string] $ProductionCluster = "", [string] $PGName = "") # 2. Failures are registered in Applocation events in the DPM server. trap [Exception] { $log = Get-EventLog -List | Where-Object { $_.Log -eq "Application" } $log.Source = "AddNewClusteredVM" $log.WriteEntry("TRAPPED: $error[0]", [system.Diagnostics.EventLogEntryType]::Error,9911) $Error[0] #show on console exit } # 3. Prompt for missing parameters if(!$ProductionCluster) { $Productioncluster = read-host "Enter the cluster FQDN " } if(!$PGName) { $PGName = read-host "Enter the name of your existing Hyper-V protection group " } $dpmservername = &"hostname" connect-dpmserver $dpmservername # Do not modify this line $tape = "Short-term using tape" # Hyper-V Writer guid $guid = "66841cd4-6ded-4f4b-8f17-fd23f8ddc3de" # 4. Search for the protection group name passed as input and exit if it was not found $PGList = @(Get-ProtectionGroup $dpmservername) $PG = $PGlist | where { $_.FriendlyName -eq $PGName} if (!$PG) { Throw "Specified protection group [$PGname] is not found!" } # 5. Obtain the list of protected clusters on this DPM server $Cluster = Get-ProductionCLuster $dpmservername | ? { $_.ClusterName -eq $ProductionCluster} if (!$Cluster) { Throw "Specified cluster name [$ProductionCluster] is not found!" } $RGList = @() $joblist = @() $global:DSlist = @() # 6. Run inquiry on cluster to get list of available resource groups write-host "Running Inquiry on" $Cluster.clusterName $RGlist = Get-ProductionVirtualName $Cluster # 7. Need to run inquiry on each resource group foreach ($RG in $RGlist) { write-host "Running Inquiry on" $RG.NetBiosName # 7.1 Inquiry is run in parallel and the event mechanism is used to signal completion of inquiry. # 'DataSourceDetectionEvent' event signals the completion of inquiry. and the data sources obtained are added to the global variable $joblist += register-objectevent -inputobject $RG -Eventname DatasourceDetectionEvent -Action {$global:DSlist += $($Event.SourceEventArgs.ProtectableObjects)} # 7.2 Run inquiry Get-Datasource -ProductionServer $RG -Inquire -Async } # 8. Scan through each job created in the previous loop foreach ($job in $joblist) { # 8.1 Waiting for the job to run while ($job.State -eq "NotStarted") { write-host "Waiting for inquiry to complete" $global:DSlist.count "item(s) obtained..." sleep 1 } } # 8.2 Inquiry complete on all resource groups write-host "Inquiry listed" $global:DSlist.count "item(s)..." # 9. If object is a data source and of type Hyper-V and is currently not protected add to list of unprotected VMs $unprotectedDSList = @(($global:DSList) | ? {$($_.Type.IsDatasource) -and ($($_.Type.Id) -match $guid) -and ! $($_.Protected)}) # 10. Exit if there are no unprotected VMs if ($unprotectedDSList.count -lt 1) { write-host "No new datasources found!" exit 1 } # 11. Obtain a modifiable protection group type $MPG = Get-ModifiableProtectionGroup $PG # 12. Perform the following for each new VM to add to protection foreach ($ds in $unprotectedDSList) { write-host "Adding data source" $ds.Name "to" $MPG.FriendlyName $npg = Add-ChildDatasource -ProtectionGroup $MPG -ChildDatasource $ds # 12.1 Disk Allocation is skipped in case of short term protection being to tape. if($MPG.protectionmethod -eq $tape) {continue;} $x = Get-DatasourceDiskAllocation -Datasource $ds Set-DatasourceDiskAllocation -Datasource $ds -ProtectionGroup $MPG } # 13 Note that this step will force immediate replica creation irrespective of the PG policy. Set-ReplicaCreationMethod -ProtectionGroup $MPG -Now write-host "Adding new Hyper-V data sources to" $MPG.FriendlyName # 14 Save the changes to the protection group. Replica creation will be triggered immediately. Set-protectiongroup $MPG disconnect-dpmserver $dpmservername -erroraction silentlycontinue "Exiting from script"