Using

Standard C# Using block for PS... named cUsing so as to avoid conflicts with "using" (which PS recognizes, and identifies as unsupported)
Language:
PowerShell
Keywords:
Code Snippet

# REFERENCE: http://weblogs.asp.net/adweigert/archive/2008/08/27/powershell-adding-the-using-statement.aspx
Function cUsing {
   param (
         [System.IDisposable] $inputObject = $(throw "The parameter -inputObject is required."),
         [ScriptBlock] $scriptBlock = $(throw "The parameter -scriptBlock is required.")
   )
   Try { &$scriptBlock }
   Finally { if ($inputObject -ne $null) { if ($inputObject.psbase -eq $null) {$inputObject.Dispose() } else { $inputObject.psbase.Dispose() } } }
}

Example
cUsing ($tSite = New-Object Microsoft.SharePoint.SPSite($scUrl)) {
  # code here
}

Created 2012-02-03
comments powered by Disqus
Login