# encoding: ascii
# api: powershell
# title: Get/Set Write Filters
# description: Get and Set-WriteFilter to manage embedded write filters remotely
# version: 0.1
# type: function
# author: Adam Bertram
# license: CC0
# function: Get-WriteFilter
# x-poshcode-id: 4720
# x-archived: 2013-12-22T06:55:54
# x-published: 2013-12-19T14:06:00
#
#
Function Get-WriteFilter {
    <#
    .SYNOPSIS
    Retrieves the status of an embedded device's write filter
    .DESCRIPTION
    This function uses the sysinternals psexec exe to get the status of a write filter 
    .EXAMPLE
    Get-WriteFilter -ComputerName HOSTNAME
    .PARAMETER ComputerName
    The computer name to query. Just one.
    #>
    [CmdletBinding()]
    param (
    [Parameter(Mandatory=$True,
        ValueFromPipeline=$True,
        ValueFromPipelineByPropertyName=$True,
        HelpMessage='What computer name would you like to target?')]
    $Computername
    )

    begin {
        $output_props = @{'ComputerName'=$ComputerName}
    }

    process {
        try {
            $process_output = C:\psexec.exe -e -s -n 10 \\$ComputerName C:\Windows\System32\ewfmgr.exe c: 2> $null
            if ($process_output -match 'State.+DISABLED') {
                $output_props.Add('State','Disabled')
            } else {
                $output_props.Add('State','Enabled')
            }
            new-object -TypeName PSCustomObject -Property $output_props
        } catch {
            $_.Exception.Message
        }
    }
}

Function Set-WriteFilter {
    <#
    .SYNOPSIS
    Sets various properties on the write filter
    .DESCRIPTION
    This function is typically used to disable an embedded device's write filter 
    .EXAMPLE
    Set-WriteFilter -ComputerName HOSTNAME -State Disabled
    .PARAMETER ComputerName
    The computer name to query. Just one.
    .PARAMETER State
    The state in which you'd like to leave the write filter
    #>
    [CmdletBinding()]
    param (
    [Parameter(Mandatory=$True,
        ValueFromPipeline=$True,
        ValueFromPipelineByPropertyName=$True,
        HelpMessage='What computer name would you like to target?')]
    $Computername,
     [Parameter(Mandatory=$True,
        ValueFromPipeline=$False,
        ValueFromPipelineByPropertyName=$True,
        HelpMessage='What state would you like the write filter in?')]
    $State,
    [Parameter(Mandatory=$False,
        ValueFromPipeline=$False,
        ValueFromPipelineByPropertyName=$False,
        HelpMessage='What state would you like the write filter in?')]
    [switch]$Restart
    )

    begin {
        $output_props = @{'ComputerName'=$ComputerName}
    }

    process {
        try {
            $current_state = (Get-WriteFilter $Computername).State
            if (($State -eq 'Disabled') -and ($current_state -eq 'Enabled')) {
                $process_output = C:\psexec.exe -e -s -n 10 \\$ComputerName C:\Windows\System32\ewfmgr.exe c: -disable 2> $null
                if ($Restart.IsPresent) {
                    Restart-Computer -ComputerName $Computername -Force
                } 
            } elseif (($State -eq 'Enabled') -and ($current_state -eq 'Disabled')) {
                 $process_output = C:\psexec.exe -e -s -n 10 \\$ComputerName C:\Windows\System32\ewfmgr.exe c: -enable 2> $null
                 if ($Restart.IsPresent) {
                    Restart-Computer -ComputerName $Computername -Force
                 }
            }
            if ($LASTEXITCODE -eq 0) {
                $output_props.Add('Result',$true)
            } else {
                $output_props.Add('Result',$false)
            }
            new-object -TypeName PSCustomObject -Property $output_props
        } catch {
            $_.Exception.Message
        }
    }
}
