Set-BrowserControl.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function Set-BrowserControl
{
    <#
    .Synopsis
        Sets a value in a browser control
    .Description
        Sets the value of a control in a browser, and fires an On_Change event
    .Link
        Get-BrowserControl
    #>

    [CmdletBinding(DefaultParameterSetName='Id')]
    [OutputType([PSObject])]
    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,

    # Sets a series of values to specific IDs
    [Parameter(Mandatory=$true, ParameterSetName='Table')]
    [Hashtable]
    $Hashtable,    
    
    # The ID of the object within the page
    [Parameter(Mandatory=$true, ParameterSetName='ById')]
    [string]$Id,
    # The name of the object within the page
    [Parameter(Mandatory=$true, ParameterSetName='ByName')]
    [string]$Name,
    # The tag name of the object within the page
    [Parameter(Mandatory=$true, ParameterSetName='ByTagName')]
    [string]$TagName,
    
    # The value to set
    [string]$Value
    )
    
    process {
        if ($psCmdlet.ParameterSetName -eq 'Table') {
            foreach ($kv in $Hashtable.GetEnumerator()) {
                $idMatch = Get-BrowserControl -ie $ie -Id $kv.Key
                if (-not $IdMatch) {
                    $nameMatch = Get-BrowserControl -ie $ie -Name $kv.Key
                    if ($nameMatch) {
                        foreach ($nm in $nameMatch) {
                            if ($nm.psobject.properties["Value"]) {
                                $nm.Value = $kv.Value
                            } elseif ($nm.psobject.properties["InnerText"]) {
                                $nm.InnerText = $kv.Value
                            }
                            
                        }
                    }
                } else {
                    $idMatch.Value = $kv.Value
                }
                
                
            }
        } else {
            $null = $psBoundParameters.Remove("Value")
            Get-BrowserControl @psBoundParameters |
                ForEach-Object {
                    if ($_.psobject.properties["Value"]) {
                        $_.Value = $value
                    } elseif ($_.psobject.properties["InnerText"])  {
                        $_.InnerText = $value
                    }
                    
                    $null = $_.FireEvent("onchange", $null)
                }
           
        } 
        $ie
    }
}