Close-Browser.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function Close-Browser { 
    <#
    .Synopsis
        Closes a running autobrowser
    .Description
        Quits an InternetExplorer AutoBrowser.
    .Example
        Open-Browser -Visible -Url http://start-automating.com/ | Close-Browser
    .Link
        Open-Browser
    .Link
        Wait-Browser
    #>
 
    [OutputType([Nullable])]
    param(
    # The Browser Object.
    [Parameter(Mandatory=$true,
        ValueFromPipeline=$true,
        ValueFromPipelineByPropertyName=$true)]
    [ValidateScript({
        if ($_.psobject.typenames -notcontains 'System.__ComObject' -and -not $_.Quit) {
            throw "Not IE"
        }
        $true
    })]
    $IE
    )
    process {
        #region Close the IE Object
        
        # Variable sleight of hand. If $ie is assigned to null directly, it becomes invalid and the browser never really quits
        $ieObject = $ie#.Quit()
        $ieObject.Quit()
        $ieObject = $null        
        
        #endregion
    
    }

}