From eed2a123853c0c8bc85ed998578eb71a7406c902 Mon Sep 17 00:00:00 2001 From: KavishreeShanmugam11 <43165295+KavishreeShanmugam11@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:12:06 +0530 Subject: [PATCH] File snapshot information (#228) * Fixed issue in constructing job url for fetching job list Issue: https://github.com/cohesity/cohesity-powershell-module/issues/226 Cause: Job name contains special character need to be encoded Fix: Encoding query name list before constructing job url * Included powershell commandlet for colletcing file snapshot informations. * Modified restore file script to fetch latest recoverable snapshot detail, if job run detail not provided by the user. * Removed cluster id and cluster incarnation id parameter in find cohesity snapshot cmdlet. * Hard-coded value for target host type is not correct in remotefile restore cmdlet, which need to be assigned based on source host type. * Updated restore file example in readme file. --------- Co-authored-by: unknown --- .../find-cohesityfilesnapshot.md | 85 +++++++++ .../cmdlets-reference/restore-cohesityfile.md | 15 +- .../restore-cohesityremotefile.md | 20 ++- .../Recovery/FindCohesityFilesForRestore.cs | 1 - .../Cmdlets/Recovery/RestoreCohesityFile.cs | 16 +- .../Cohesity.PowerShell.Core.psd1 | 3 +- .../Cohesity.PowerShell.psd1 | 3 +- .../Cohesity.format.ps1xml | 165 +++++++++++++++++- .../Get-CohesityProtectionJob.ps1 | 7 +- .../Restore/Find-CohesityFileSnapshot.ps1 | 78 +++++++++ .../Restore/Restore-CohesityFileV2.ps1 | 6 +- .../Restore/Restore-CohesityRemoteFile.ps1 | 57 +++--- 12 files changed, 407 insertions(+), 49 deletions(-) create mode 100644 docs/cmdlets-reference/find-cohesityfilesnapshot.md create mode 100644 src/Cohesity.Powershell/Scripts/Restore/Find-CohesityFileSnapshot.ps1 diff --git a/docs/cmdlets-reference/find-cohesityfilesnapshot.md b/docs/cmdlets-reference/find-cohesityfilesnapshot.md new file mode 100644 index 00000000..6bcaf0c4 --- /dev/null +++ b/docs/cmdlets-reference/find-cohesityfilesnapshot.md @@ -0,0 +1,85 @@ +# Find-CohesityFileSnapshot + +## SYNOPSIS +Get the information about snapshots that contain the specified file or folder. In addition, information about the file or folder is provided. + +## SYNTAX + +``` +Find-CohesityFileSnapshot [-ClusterId ] [-ClusterIncarnationId ] [-FileName ] [-JobId ] + [-SourceId ] [] +``` + +## EXAMPLES + +### EXAMPLE 1 +``` +Find-CohesityFileSnapshot -FileName "abc.txt" -SourceId 123 -JobId 11 +``` + +Returns snapshot information of specified file/folder in metioned source. + +## PARAMETERS + +### -FileName +Specifies the name of the file or folder to find in the snapshots. +This field is required. + +```yaml +Type: String +Parameter Sets: (All) +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -JobId +Specifies the id of the Job that captured the snapshots. +These snapshots are searched for the specified files or folders. +This field is required. + +```yaml +Type: Int64 +Parameter Sets: (All) +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### -SourceId +Specifies the id of the Protection Source object (such as a VM) to search. +When a Job Run executes, snapshots of the specified Protection Source +object are captured. This operation searches the snapshots of the +object for the file or folder. This field is required. + +```yaml +Type: Int64 +Parameter Sets: (All) +Aliases: + +Required: True +Position: Named +Default value: None +Accept pipeline input: False +Accept wildcard characters: False +``` + +### CommonParameters +This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216). + +## INPUTS + +## OUTPUTS + +### Cohesity.Model.FileSnapshotInformation +## NOTES + +## RELATED LINKS diff --git a/docs/cmdlets-reference/restore-cohesityfile.md b/docs/cmdlets-reference/restore-cohesityfile.md index 1fb0148e..10d9a737 100644 --- a/docs/cmdlets-reference/restore-cohesityfile.md +++ b/docs/cmdlets-reference/restore-cohesityfile.md @@ -1,7 +1,7 @@ # Restore-CohesityFile ## SYNOPSIS -Restores the specified files or folders from a previous backup. +Restores the specified files or folders. ## SYNTAX @@ -14,7 +14,7 @@ Restore-CohesityFile -TaskName -FileNames -JobId -So ``` ## DESCRIPTION -Restores the specified files or folders from a previous backup. +Request to create a Restore Task for recovering files or folders. ## EXAMPLES @@ -23,14 +23,21 @@ Restores the specified files or folders from a previous backup. Restore-CohesityFile -TaskName "restore-file-vm" -FileNames /C/data/file.txt -JobId 1234 -SourceId 843 -TargetSourceId 856 -TargetParentSourceId 828 -TargetHostType KWindows -TargetHostCredential (Get-Credential) ``` -Restores the specified file to the target windows VM with the source id 856 from the latest backup. +Restores the file from the specified source to the target windows VM from the latest backup. ### EXAMPLE 2 ``` +Restore-CohesityFile -TaskName "restore-file-vm" -FileNames /C/data/file.txt -JobId 1234 -JobRunId 3005 -StartTime 1690646467987573 -SourceId 843 -TargetSourceId 856 -TargetParentSourceId 828 -TargetHostType KWindows -TargetHostCredential (Get-Credential) +``` + +Restores the file from the specified source to the target windows VM from the specified snapshot. + +### EXAMPLE 3 +``` Restore-CohesityFile -TaskName "restore-file-physical" -FileNames /C/data/file.txt -JobId 1234 -SourceId 820 -TargetSourceId 858 ``` -Restores the specified file to the target physical server with the source id 858 from the latest backup. +Restores the file from the specified source to the target physical server using the latest backup. ## PARAMETERS diff --git a/docs/cmdlets-reference/restore-cohesityremotefile.md b/docs/cmdlets-reference/restore-cohesityremotefile.md index aa523b45..7fdd2ebb 100644 --- a/docs/cmdlets-reference/restore-cohesityremotefile.md +++ b/docs/cmdlets-reference/restore-cohesityremotefile.md @@ -1,7 +1,7 @@ # Restore-CohesityRemoteFile ## SYNOPSIS -Restores the specified files or folders from a previous backup from a remote cluster. +Restores the specified files or folders from a remote cluster. ## SYNTAX @@ -13,7 +13,7 @@ Restore-CohesityRemoteFile [[-TaskName] ] [-FileNames] [-JobI ``` ## DESCRIPTION -Restores the specified files or folders from a previous backup from a remote cluster. +Request to create a Restore Task for recovering files or folders from a remote target. ## EXAMPLES @@ -22,19 +22,21 @@ Restores the specified files or folders from a previous backup from a remote clu Restore-CohesityRemoteFile -TaskName "restore-file-vm" -FileNames /C/data/file.txt -JobId 1234 -SourceId 843 -TargetSourceId 856 -TargetParentSourceId 828 -TargetHostCredential (Get-Credential) ``` -Restores the specified file to the target windows VM with the source id 843 from the latest backup. -Get the job id from $jobs = Get-CohesityProtectionJob -Environments KVMware -Get the source id from $jobs\[0\].sourceIds -Get the target details $targets = Get-CohesityProtectionSourceObject -Environments KVMware -Get the target source id $targets\[2\].id -Get the target parent source id $targets\[2\].parentId +Restores the file from the specified source to the target windows VM using the latest backup from remote target. ### EXAMPLE 2 ``` +Restore-CohesityRemoteFile -TaskName "restore-file-vm" -FileNames /C/data/file.txt -JobId 1234 -JobRunId 3005 -StartTime 1690646467987573 -SourceId 843 -TargetSourceId 856 -TargetParentSourceId 828 -TargetHostType KWindows -TargetHostCredential (Get-Credential) +``` + +Restores the file from the specified source to the target windows VM using the specified snapshot from remote target. + +### EXAMPLE 3 +``` Restore-CohesityRemoteFile -FileNames "/C/myFolder" -NewBaseDirectory "C:\temp\restore" -JobId 61592 -SourceId 3517 -TargetSourceId 3098 ``` -Restores the specified file to the target physical server with the source id 3517 from the latest backup. +Restores the file from the specified source to the target physical server using the latest backup from remote target. ## PARAMETERS diff --git a/src/Cohesity.Powershell/Cmdlets/Recovery/FindCohesityFilesForRestore.cs b/src/Cohesity.Powershell/Cmdlets/Recovery/FindCohesityFilesForRestore.cs index 149dd58d..a0591ec0 100644 --- a/src/Cohesity.Powershell/Cmdlets/Recovery/FindCohesityFilesForRestore.cs +++ b/src/Cohesity.Powershell/Cmdlets/Recovery/FindCohesityFilesForRestore.cs @@ -236,7 +236,6 @@ protected override void ProcessRecord() queryString = "?" + string.Join("&", queries.Select(q => $"{q.Key}={q.Value}")); var url = $"/public/restore/files{queryString}"; - WriteObject(url); var result = Session.ApiClient.Get(url); WriteObject(result.Files, true); if (Paginate != null && Paginate.HasValue) diff --git a/src/Cohesity.Powershell/Cmdlets/Recovery/RestoreCohesityFile.cs b/src/Cohesity.Powershell/Cmdlets/Recovery/RestoreCohesityFile.cs index ae5da654..360961df 100644 --- a/src/Cohesity.Powershell/Cmdlets/Recovery/RestoreCohesityFile.cs +++ b/src/Cohesity.Powershell/Cmdlets/Recovery/RestoreCohesityFile.cs @@ -249,6 +249,18 @@ protected override void ProcessRecord() } }; + // Collect the recoverable snapshot information of specified file/folder + var queryString = new QuerystringBuilder(); + foreach(var filename in FileNames) + { + queryString.Add("filename", filename); + } + queryString.Add("sourceId", SourceId); + queryString.Add("jobId", JobId); + + var snapshotUrl = $"/public/restore/files/snapshotsInformation{ queryString.Build()}"; + var snapshotInfo = Session.ApiClient.Get(snapshotUrl); + // If job run id is not specified, get the job run id of the last run if (JobRunId.HasValue) { @@ -256,7 +268,7 @@ protected override void ProcessRecord() } else { - restoreObject.JobRunId = job.LastRun.BackupRun.JobRunId; + restoreObject.JobRunId = snapshotInfo[0].Snapshot.JobRunId; } // If start time is not specified, get the start time of the last run @@ -266,7 +278,7 @@ protected override void ProcessRecord() } else { - restoreObject.StartedTimeUsecs = job.LastRun.BackupRun.Stats.StartTimeUsecs; + restoreObject.StartedTimeUsecs = snapshotInfo[0].Snapshot.StartedTimeUsecs; } restoreRequest.SourceObjectInfo = restoreObject; diff --git a/src/Cohesity.Powershell/Cohesity.PowerShell.Core.psd1 b/src/Cohesity.Powershell/Cohesity.PowerShell.Core.psd1 index 5b2c0987..7c49f86c 100644 --- a/src/Cohesity.Powershell/Cohesity.PowerShell.Core.psd1 +++ b/src/Cohesity.Powershell/Cohesity.PowerShell.Core.psd1 @@ -8,7 +8,7 @@ RootModule = 'Cohesity.PowerShell.Core.dll' # Version number of this module. -ModuleVersion = '1.9.2' +ModuleVersion = '1.9.3' # Supported PSEditions # CompatiblePSEditions = @() @@ -75,6 +75,7 @@ FunctionsToExport = @( 'Copy-CohesityMSSQLObject', 'Copy-CohesityView', 'Copy-CohesityVMwareVM', + 'Find-CohesityFileSnapshot', 'Get-CohesityActiveDirectory', 'Get-CohesityCmdletConfig', 'Get-CohesityExternalClient', diff --git a/src/Cohesity.Powershell/Cohesity.PowerShell.psd1 b/src/Cohesity.Powershell/Cohesity.PowerShell.psd1 index 83c1ca00..14a0c0c0 100644 --- a/src/Cohesity.Powershell/Cohesity.PowerShell.psd1 +++ b/src/Cohesity.Powershell/Cohesity.PowerShell.psd1 @@ -8,7 +8,7 @@ RootModule = 'Cohesity.PowerShell.dll' # Version number of this module. -ModuleVersion = '1.9.2' +ModuleVersion = '1.9.3' # Supported PSEditions # CompatiblePSEditions = @() @@ -75,6 +75,7 @@ FunctionsToExport = @( 'Copy-CohesityMSSQLObject', 'Copy-CohesityView', 'Copy-CohesityVMwareVM', + 'Find-CohesityFileSnapshot', 'Get-CohesityActiveDirectory', 'Get-CohesityCmdletConfig', 'Get-CohesityExternalClient', diff --git a/src/Cohesity.Powershell/Cohesity.format.ps1xml b/src/Cohesity.Powershell/Cohesity.format.ps1xml index ac09be0a..87ba5564 100644 --- a/src/Cohesity.Powershell/Cohesity.format.ps1xml +++ b/src/Cohesity.Powershell/Cohesity.format.ps1xml @@ -275,8 +275,110 @@ + + + FileSnapshotInformation.TableView + + Cohesity.Model.FileSnapshotInformation + + + + + + + $_.Snapshot.JobRunId + + + $_.Snapshot.StartedTimeUsecs | Convert-CohesityUsecsToDateTime + + + $_.Snapshot.AttemptNumber + + + $_.replicaInfoList[0].snapshotTargetSettings.type.Substring(1) + + + $_.HasLocalCopy + + + $_.HasRemoteCopy + + + $_.HasArchivalCopy + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + FileSnapshotInformation.ListView + + Cohesity.Model.FileSnapshotInformation + + + + + + + $_.Snapshot.JobRunId + + + + $_.Snapshot.StartedTimeUsecs | Convert-CohesityUsecsToDateTime + + + + $_.Snapshot.AttemptNumber + + + + $_.replicaInfoList[0].snapshotTargetSettings.type.Substring(1) + + + + $_.HasLocalCopy + + + + $_.HasRemoteCopy + + + + $_.HasArchivalCopy + + + + + + + + - + ProtectionPolicy.TableView Cohesity.Model.ProtectionPolicy @@ -449,7 +551,7 @@ - + ProtectionJobRun.TableView Cohesity.Model.ProtectionRunInstance @@ -1574,6 +1676,65 @@ + + + System.Object.FileSnapshotInformation.TableView + + System.Object#FileSnapshotInformation + + + + + + + $_.Snapshot.JobRunId + + + $_.Snapshot.StartedTimeUsecs | Convert-CohesityUsecsToDateTime + + + $_.Snapshot.AttemptNumber + + + $_.replicaInfoList[0].snapshotTargetSettings.type.Substring(1) + + + $_.HasLocalCopy + + + $_.HasRemoteCopy + + + $_.HasArchivalCopy + + + + + + + + + + + + + + + + + + + + + + + + + + + + + System.Object.ProtectionPolicy.TableView diff --git a/src/Cohesity.Powershell/Scripts/ProtectionJob/Get-CohesityProtectionJob.ps1 b/src/Cohesity.Powershell/Scripts/ProtectionJob/Get-CohesityProtectionJob.ps1 index 277b4cbd..df76e724 100644 --- a/src/Cohesity.Powershell/Scripts/ProtectionJob/Get-CohesityProtectionJob.ps1 +++ b/src/Cohesity.Powershell/Scripts/ProtectionJob/Get-CohesityProtectionJob.ps1 @@ -70,6 +70,7 @@ function Get-CohesityProtectionJob { $filter += "&ids=" + ($Ids -join ",") } if ($Names) { + $Names = [System.Web.HttpUtility]::UrlEncode($Names).split('+') $filter += "&names=" + ($Names -join ",") } if ($PolicyIds) { @@ -93,9 +94,9 @@ function Get-CohesityProtectionJob { if(-not $OnlyDeleted.IsPresent) { $resp = @($resp | where-object { $_.Name -inotmatch '_DELETED'}) } - if($Names) { - $resp = @($resp | where-object { $Names -contains $_.Name}) - } + # if($Names) { + # $resp = @($resp | where-object { $Names -contains $_.Name}) + # } # tagging reponse for display format ( configured in Cohesity.format.ps1xml ) @($resp | Add-Member -TypeName 'System.Object#ProtectionJob' -PassThru) } diff --git a/src/Cohesity.Powershell/Scripts/Restore/Find-CohesityFileSnapshot.ps1 b/src/Cohesity.Powershell/Scripts/Restore/Find-CohesityFileSnapshot.ps1 new file mode 100644 index 00000000..8e0f46f8 --- /dev/null +++ b/src/Cohesity.Powershell/Scripts/Restore/Find-CohesityFileSnapshot.ps1 @@ -0,0 +1,78 @@ +function Find-CohesityFileSnapshot { + <# + .SYNOPSIS + Get the information about snapshots that contain the specified file or folder. In addition, information about the file or folder is provided. + .DESCRIPTION + Get the information about snapshots that contain the specified file or folder. In addition, information about the file or folder is provided. + .NOTES + Published by Cohesity + .LINK + https://cohesity.github.io/cohesity-powershell-module/#/README + .EXAMPLE + Find-CohesityFileSnapshot -FileName "abc.txt" -SourceId 123 -JobId 11 + #> + + [OutputType('System.Array')] + [CmdletBinding()] + Param( + # Specifies the name of the file or folder to find in the snapshots. This field is required. + [string]$FileName, + [Parameter(Mandatory = $true)] + # Specifies the name of the Restore Task.Specifies the id of the Job that captured the snapshots. These snapshots are searched for the specified files or folders. This field is required. + [long]$JobId, + [Parameter(Mandatory = $true)] + # Specifies the id of the Protection Source object (such as a VM) to search. When a Job Run executes, snapshots of the specified Protection Source object are captured. This operation searches the snapshots of the object for the file or folder. This field is required. + [long]$SourceId + ) + Begin { + } + + Process { + $snapshotURL = '/irisservices/api/v1/public/restore/files/snapshotsInformation' + + $filter = "" + if ($ClusterId) { + if ($filter -ne "") { + $filter += "&" + } + $filter += "clusterId=$ClusterId" + } + if ($ClusterIncarnationId ) { + if ($filter -ne "") { + $filter += "&" + } + $filter += "clusterIncarnationId =$ClusterIncarnationId " + } + if ($FileName) { + if ($filter -ne "") { + $filter += "&" + } + $filter += "filename=$FileName" + } + if ($JobId) { + if ($filter -ne "") { + $filter += "&" + } + $filter += "jobId=$JobId" + } + if ($SourceId) { + if ($filter -ne "") { + $filter += "&" + } + $filter += "sourceId=$SourceId" + } + + if ($filter -ne "") { + $snapshotURL += "?" + $filter + } + + $snapshotResp = Invoke-RestApi -Method Get -Uri $snapshotURL + if ($snapshotResp) { + # tagging reponse for display format ( configured in Cohesity.format.ps1xml ) + @($snapshotResp | Add-Member -TypeName 'System.Object#FileSnapshotInformation' -PassThru) + } + } + + End { + } +} \ No newline at end of file diff --git a/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityFileV2.ps1 b/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityFileV2.ps1 index 7d741d78..3b324f79 100644 --- a/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityFileV2.ps1 +++ b/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityFileV2.ps1 @@ -15,11 +15,11 @@ function Restore-CohesityFileV2 { .LINK https://cohesity.github.io/cohesity-powershell-module/#/README .EXAMPLE - Restore-CohesityFileV2 -sourceVM "SQL-UT-VM2" -targetVM "SQL-UT-VM2" -fileNames "C:\.rnd" -restorePath "C:\" -wait + Restore-CohesityFileV2 -sourceVM "SQL-UT-VM2" -targetVM "SQL-UT-VM2" -fileNames "C:\.rnd" -restorePath "C:\" -wait .EXAMPLE - Restore-CohesityFileV2 -sourceVM "SQL-UT-VM2" -targetVM "SQL-UT-VMD2" -fileNames "C:\Users\" -restorePath "C:\temp\" -wait + Restore-CohesityFileV2 -sourceVM "SQL-UT-VM2" -targetVM "SQL-UT-VMD2" -fileNames "C:\Users\" -restorePath "C:\temp\" -wait .EXAMPLE - Restore-CohesityFileV2 -sourceVM "SQL-UT-VM2" -targetVM "SQL-UT-VM2" -fileNames "C:\.rnd" -restorePath "C:\" -taskName "Test_task" -wait + Restore-CohesityFileV2 -sourceVM "SQL-UT-VM2" -targetVM "SQL-UT-VM2" -fileNames "C:\.rnd" -restorePath "C:\" -taskName "Test_task" -wait #> [CmdletBinding(DefaultParameterSetName = "Default", SupportsShouldProcess = $True, ConfirmImpact = "High")] diff --git a/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityRemoteFile.ps1 b/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityRemoteFile.ps1 index 169f9ffb..98e79178 100644 --- a/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityRemoteFile.ps1 +++ b/src/Cohesity.Powershell/Scripts/Restore/Restore-CohesityRemoteFile.ps1 @@ -1,25 +1,19 @@ function Restore-CohesityRemoteFile { <# .SYNOPSIS - Restores the specified files or folders from a previous backup from a remote cluster. + Restores the specified files or folders from a remote cluster. .DESCRIPTION - Restores the specified files or folders from a previous backup from a remote cluster. + Restores the specified files or folders from a remote cluster. .NOTES - Published by Cohesity + Published by Cohesity. .LINK https://cohesity.github.io/cohesity-powershell-module/#/README .EXAMPLE Restore-CohesityRemoteFile -TaskName "restore-file-vm" -FileNames /C/data/file.txt -JobId 1234 -SourceId 843 -TargetSourceId 856 -TargetParentSourceId 828 -TargetHostCredential (Get-Credential) - Restores the specified file to the target windows VM with the source id 843 from the latest backup. - Get the job id from $jobs = Get-CohesityProtectionJob -Environments KVMware - Get the source id from $jobs[0].sourceIds - Get the target details $targets = Get-CohesityProtectionSourceObject -Environments KVMware - Get the target source id $targets[2].id - Get the target parent source id $targets[2].parentId - + Restores the specified file to the target windows VM with specified source id from the latest backup. .EXAMPLE - Restore-CohesityRemoteFile -FileNames "/C/myFolder" -NewBaseDirectory "C:\temp\restore" -JobId 61592 -SourceId 3517 -TargetSourceId 3098 - Restores the specified file to the target physical server with the source id 3517 from the latest backup. + Restore-CohesityRemoteFile -FileNames "/C/myFolder" -NewBaseDirectory "C:\temp\restore" -JobId 61592 -SourceId 3517 -TargetSourceId 3098 + Restores the specified file to the target physical server with specified source id from the latest backup. #> [CmdletBinding(DefaultParameterSetName = "Default", SupportsShouldProcess = $True, ConfirmImpact = "High")] @@ -82,20 +76,26 @@ function Restore-CohesityRemoteFile { Process { if ($PSCmdlet.ShouldProcess($SourceId)) { + # Check whether specified job is valid or not $job = Get-CohesityProtectionJob -Ids $JobId if (-not $job) { - $errorMsg = "Cannot proceed, the job id '$JobId' is invalid" + $errorMsg = "Cannot proceed, the job with id '$JobId' is not found." Write-Output $errorMsg CSLog -Message $errorMsg -Severity 2 return } + <# + Check if job run details are provided. + If not, collect the latest recoverable job run information. + #> if ($JobRunId -le 0) { - $runs = Get-CohesityProtectionJobRun -JobId $JobId -ExcludeErrorRuns:$true -NumRuns 2 - $run = $runs[0] - $JobRunId = $run.backupRun.jobRunId - $StartTime = $run.backupRun.stats.startTimeUsecs + $snapshotInfo = Find-CohesityFileSnapshot -FileName $FileNames[0] -JobId $JobId -SourceId $SourceId + $snapshot = $snapshotInfo[0].snapshot + $JobRunId = $snapshot.jobRunId + $StartTime = $snapshot.startedTimeUsecs } - + + # Validate Source VM $searchURL = '/irisservices/api/v1/searchvms?entityIds=' + $SourceId $sourceVMSearchResult = Invoke-RestApi -Method Get -Uri $searchURL if ($null -eq $sourceVMSearchResult) { @@ -111,27 +111,37 @@ function Restore-CohesityRemoteFile { CSLog -Message $errorMsg -Severity 2 return } + + # Collect target source details $targetSourceDetail = Get-CohesityProtectionSourceObject -Id $TargetSourceId if (-not $targetSourceDetail) { - $errorMsg = "Details for target source '$TargetSourceId' not found." + $errorMsg = "Details of target with id '$TargetSourceId' not found." Write-Output $errorMsg CSLog -Message $errorMsg -Severity 2 return } + # Construct payload for restore $restoreToOriginalPaths = $true if ($NewBaseDirectory) { $restoreToOriginalPaths = $false } $TARGET_ENTITY_TYPE = 1 $TARGET_ENTITY_PARENT_SOURCE_TYPE = 1 - $TARGET_HOST_TYPE = 1 $targetEntity = $null $targetUserName = "" $targetPassword = "" $sourceObjectEntity = $null $parentSource = $null $targetEntityParentSource = $null + + # Determine target host type + $targetHostType = $targetSourceDetail.vmWareProtectionSource.hostType + if ($targetHostType -eq 'kLinux') { $TARGET_HOST_TYPE = 0 } + elseif ($targetHostType -eq 'kWindows') { $TARGET_HOST_TYPE = 1 } + else { $TARGET_HOST_TYPE = 2 } + + # Source with environment type VMware if ($job.environment -eq "kVMware") { $TARGET_ENTITY_VMWARE_TYPE = 8 $targetUserName = $TargetHostCredential.GetNetworkCredential().UserName @@ -163,8 +173,8 @@ function Restore-CohesityRemoteFile { id = $targetSourceDetail.parentId } } + # Source with environment type Physical Server else { - # for files from physical server $TARGET_ENTITY_TYPE = 6 $TARGET_ENTITY_PHYSICAL_TYPE = 1 $TARGET_HOST_TYPE = $null @@ -225,10 +235,11 @@ function Restore-CohesityRemoteFile { if(($version.replicaInfo.replicaVec | Sort-Object -Property {$_.target.type})[0].target.type -eq 3){ $payload.sourceObjectInfo['archivalTarget'] = $version.replicaInfo.replicaVec[0].target.archivalTarget } - $url = '/irisservices/api/v1/restoreFiles' + + $restoreURL = '/irisservices/api/v1/restoreFiles' $payloadJson = $payload | ConvertTo-Json -Depth 100 - $resp = Invoke-RestApi -Method 'Post' -Uri $url -Body $payloadJson + $resp = Invoke-RestApi -Method 'Post' -Uri $restoreURL -Body $payloadJson if ($Global:CohesityAPIStatus.StatusCode -eq 200) { $taskId = $resp.restoreTask.performRestoreTaskState.base.taskId if ($taskId) {