# encoding: ascii
# api: powershell
# title: Invoke-FolderCompression
# description: Funtcion to set NTFS compression for specified folder.
# version: 0.1
# type: function
# author: skourlatov
# license: CC0
# function: Invoke-FolderCompression
# x-poshcode-id: 5744
# x-archived: 2015-03-23T11:44:16
# x-published: 2015-02-20T04:33:00
#
#
Function Invoke-FolderCompression
{
    Param
    (
        [Parameter(Position=0, Mandatory, ValueFromPipeline)]
        [string]$Path,
        [Parameter(Position=1)]
        [switch]$Uncompress,
        [Parameter(Position=2)]
        [switch]$Recurse,
        [Parameter(Position=3)]
        [switch]$Background
    )

    $target = ($Path | Resolve-Path -ea 'Stop').Path

    if ($Uncompress) { $action = 'UncompressEx'} else { $action = 'CompressEx' }

    $objPath = [Management.ManagementPath]"\\.\root\cimv2:Win32_Directory.Name='$target'"
    $dir = New-Object -TypeName 'Management.ManagementObject' -ArgumentList $objPath
    $objClass = New-Object -TypeName 'Management.ManagementClass' -ArgumentList $objPath.ClassName
    $parameters = $objClass.GetMethodParameters($action)
    $parameters.Recursive = $Recurse

    if ($Background)
    {
        $observer = New-Object -TypeName 'Management.ManagementOperationObserver'
        $dir.InvokeMethod($observer, $action, $parameters, $null)
    }
}
