Hi folks. We have developed a script in our team that successfully starts and completes the backup of all our Sharepoint on-prem site collections. The backup via Powershell is successful. But we are trying to capture how long a backup job takes, the site URL and Site size and export to CSV file. Now when i kickoff my script, which is pasted below, i can execute the $running and see PSBegintime and PSEndTime by executing $running[0] | select *. The problem is that i’m not able to see what Site is being backed up, the “Command” property within $running[0] shows the script block we ran, but the $variables in there are not filled in with real values, they only show variable names (so no help). I found that i can figure out how long a job ran but executing this right below.
((Get-Job Job8).PSEndTime.TimeOfDay - (Get-Job Job8).PSBeginTime.TimeOfDay).TotalMinutes
What i’m having a difficult with is grabbing all Job ID #s that were created and are in either Running or Completed state. I wanted to add them to the empty array of $backupJobs. How will i be able to pull the Site URL from the running/completed Jobs? Thank you for any assistance
[pre] ## Backup all sitecollections
$jobThreshold = 7
$logfile = “d:\PSBackups\test\RunLog.txt” ;
$(Get-Date) > $logfile ;
$sites = get-spsite -limit all
Remove-Item d:\PSBackups\test\Data*.* ;
foreach ($site in $sites) {
Get all running jobs
Write-Host $site.ID
$running = @(Get-Job | where { $_.JobStateInfo.State -eq “Running” })
Loop as long as our running job count is >= threshold
while ($running.Count -ge $jobThreshold) {
Block until we get at least one job complete
$running | Wait-Job -Any | Out-Null
Refresh the running job list
$running = @(Get-Job | where { $_.JobStateInfo.State -eq “Running” })
}
##$sitesize = $site.Usage.Storage;
##TotalSize = TotalSize + $sitesize
Write-Host “BEGIN:” $(Get-Date) $site.Url ;
“BEGIN: $(Get-Date) $site.url” >> $logfile ;
Start-Job -InputObject $site.Url {
$url = $input | %{$}
Add-PSSnapin Microsoft.SharePoint.PowerShell
$filename = $url ;
$filename = $filename.replace(“https://” , “”);
$filename = $filename.replace(“sites/” , “”);
$filename = $filename.replace(“/” , "“);
$filename = $filename.replace(”." , “_”);
$filename = $filename + “.dat” ;
write-host $filename;
backup-spsite -Identity $url -Path d:\PSBackups\test\Data$filename -force -NoSiteLock
}
$backupJobs = @()
Write-Host “END:” $(Get-Date) $site.Url ;
“END : $(Get-Date) $site.Url” >> $logfile ;
completed jobs
Get-Job | where { $_.JobStateInfo.State -eq “Completed” } | Receive-Job
Remove completed jobsin
Get-Job | where { $_.JobStateInfo.State -eq “Completed” } | Remove-Job
}
$(Get-Date) >> $logfile ; [/pre]