Skip to main content

This is how you could use the using statement inside Powershell

function Using-Object
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        .$ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}


Using-Object ($streamWriter = New-Object System.IO.StreamWriter("$SitecoreDataFolder\newfile.txt")) {
    $streamWriter.WriteLine('Line written inside Using block.')
    $streamWriter
}

This is how you could disable events in Sitecore

Using-Object ($ed = New-Object Sitecore.Data.Events.EventDisabler) {
    # "Disbaled""Disabled"
}

Notice that a script execution triggers some events in the background (you can observe index rebuild jobs even if you run empty script).

Once you wrap something with using statement, everything what is inside should work in 'switched context', with all events disabled.

I tested it and number of items added to indexing queue is different.

This is how you could use using statement inside Powershell

function Using-Object
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        .$ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}


Using-Object ($streamWriter = New-Object System.IO.StreamWriter("$SitecoreDataFolder\newfile.txt")) {
    $streamWriter.WriteLine('Line written inside Using block.')
    $streamWriter
}

This is how you could disable events in Sitecore

Using-Object ($ed = New-Object Sitecore.Data.Events.EventDisabler) {
    # "Disbaled"
}

Notice that script execution triggers some events in the background (you can observe index rebuild jobs even if you run empty script).

Once you wrap something with using statement, everything what is inside should work in 'switched context', with all events disabled.

I tested it and number of items added to indexing queue is different.

This is how you could use the using statement inside Powershell

function Using-Object
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        .$ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}


Using-Object ($streamWriter = New-Object System.IO.StreamWriter("$SitecoreDataFolder\newfile.txt")) {
    $streamWriter.WriteLine('Line written inside Using block.')
    $streamWriter
}

This is how you could disable events in Sitecore

Using-Object ($ed = New-Object Sitecore.Data.Events.EventDisabler) {
    # "Disabled"
}

Notice that a script execution triggers some events in the background (you can observe index rebuild jobs even if you run empty script).

Once you wrap something with using statement, everything what is inside should work in 'switched context', with all events disabled.

I tested it and number of items added to indexing queue is different.

added 73 characters in body
Source Link

This is how you could use using statement inside Powershell

function Using-Object
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        .$ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}


Using-Object ($streamWriter = New-Object System.IO.StreamWriter("$SitecoreDataFolder\newfile.txt")) {
    $streamWriter.WriteLine('Line written inside Using block.')
    $streamWriter
}

This is how you could disable events in Sitecore

Using-Object ($ed = New-Object Sitecore.Data.Events.EventDisabler) {
    # "Disbaled"
}

Notice that script execution triggers some events in the background (you can observe index rebuild jobs even if you run empty script).

Once you wrap something with using statement, everything what is inside should work in 'switched context', with all events disabled.

I tested it and number of items added to indexing queue is different.

This is how you could use using statement inside Powershell

function Using-Object
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        .$ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}


Using-Object ($streamWriter = New-Object System.IO.StreamWriter("$SitecoreDataFolder\newfile.txt")) {
    $streamWriter.WriteLine('Line written inside Using block.')
    $streamWriter
}

This is how you could disable events in Sitecore

Using-Object ($ed = New-Object Sitecore.Data.Events.EventDisabler) {
    # "Disbaled"
}

Notice that script execution triggers some events in the background (you can observe index rebuild jobs even if you run empty script).

Once you wrap something with using statement, everything what is inside should work in 'switched context', with all events disabled.

This is how you could use using statement inside Powershell

function Using-Object
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        .$ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}


Using-Object ($streamWriter = New-Object System.IO.StreamWriter("$SitecoreDataFolder\newfile.txt")) {
    $streamWriter.WriteLine('Line written inside Using block.')
    $streamWriter
}

This is how you could disable events in Sitecore

Using-Object ($ed = New-Object Sitecore.Data.Events.EventDisabler) {
    # "Disbaled"
}

Notice that script execution triggers some events in the background (you can observe index rebuild jobs even if you run empty script).

Once you wrap something with using statement, everything what is inside should work in 'switched context', with all events disabled.

I tested it and number of items added to indexing queue is different.

Source Link

This is how you could use using statement inside Powershell

function Using-Object
{
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [AllowEmptyString()]
        [AllowEmptyCollection()]
        [AllowNull()]
        [object]
        $InputObject,

        [Parameter(Mandatory = $true)]
        [scriptblock]
        $ScriptBlock
    )

    try
    {
        .$ScriptBlock
    }
    finally
    {
        if ($null -ne $InputObject -and $InputObject -is [System.IDisposable])
        {
            $InputObject.Dispose()
        }
    }
}


Using-Object ($streamWriter = New-Object System.IO.StreamWriter("$SitecoreDataFolder\newfile.txt")) {
    $streamWriter.WriteLine('Line written inside Using block.')
    $streamWriter
}

This is how you could disable events in Sitecore

Using-Object ($ed = New-Object Sitecore.Data.Events.EventDisabler) {
    # "Disbaled"
}

Notice that script execution triggers some events in the background (you can observe index rebuild jobs even if you run empty script).

Once you wrap something with using statement, everything what is inside should work in 'switched context', with all events disabled.