Created
April 8, 2015 12:49
-
-
Save michaellwest/1b7fe5a1a45e9fbf317f to your computer and use it in GitHub Desktop.
New-UsingBlock and BulkUpdateContext changes Context User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function New-UsingBlock { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[IDisposable] | |
$InputObject, | |
[Parameter(Mandatory = $true)] | |
[ScriptBlock] | |
$ScriptBlock | |
) | |
try { | |
$ScriptBlock.Invoke() | |
} finally { | |
if ($InputObject) { | |
$InputObject.Dispose() | |
} | |
} | |
} | |
Get-ChildItem -Path "master:\content\home" | % { Remove-Item -Path $_.ItemPath } | |
$watch = New-Object System.Diagnostics.Stopwatch | |
$watch.Start() | |
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) { | |
[Sitecore.Context]::User.Name | |
for($i = 1; $i -lt 10; $i++) { | |
$item = New-Item "master:/content/home/sample item $($i)" -Type "/sitecore/templates/Sample/Sample Item" | |
$item.Editing.BeginEdit() | |
$item["Title"] = "Sample Item $($i)" | |
$item["Text"] = "Sample Item $($i)" | |
$item.Editing.EndEdit() | Out-Null | |
} | |
[Sitecore.Context]::User.Name | |
} | |
$watch.Stop() | |
Write-Host $watch.Elapsed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment