DSCResources/ROACommon/Src/Import-ROABuildingBlockFile.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
function Import-ROABuildingBlockFile {
<#
    .SYNOPSIS
        Imports RES ONE Automation building blocks.
#>

    [CmdletBinding(DefaultParameterSetName = 'Path')]
    [OutputType([System.Management.Automation.PSCustomObject])]
    param (
        # Specifies a path to one or more locations. Wildcards are permitted.
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'Path')]
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'PathCredential')]
        [ValidateNotNullOrEmpty()]
        [SupportsWildcards()]
        [System.String] $Path,

        # SQL authentication username and password
        [Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'PathCredential')]
        [ValidateNotNull()]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()] $Credential,

        ## Credential is an internal RES ONE Automation user account.
        [Parameter(ValueFromPipelineByPropertyName, ParameterSetName = 'PathCredential')]
        [System.Boolean] $IsRESONEAutomationCredential
    )
    begin {

        $wmcPath = Get-ROAConsolePath;

    }
    process {

        $paths = @();
        if (-not (Test-Path -Path $Path)) {

            $exMessage = $localizedData.CannotFindPathError -f $Path;
            $ex = New-Object System.Management.Automation.ItemNotFoundException $exMessage;
            $category = [System.Management.Automation.ErrorCategory]::ObjectNotFound;
            $errRecord = New-Object System.Management.Automation.ErrorRecord $ex, 'PathNotFound', $category, $Path;
            $psCmdlet.WriteError($errRecord);
        }
        else {

            # Resolve any wildcards that might be in the path
            $provider = $null;
            $paths += $psCmdlet.SessionState.Path.GetResolvedProviderPathFromPSPath($Path, [ref] $provider);
        }

        foreach ($filePath in $paths) {

            $arguments = @(
                '/action=importbb',
                ('/file="{0}"' -f $filePath),
                '/silent'
            );

            if (($PSBoundParameters.ContainsKey('Credential')) -and ($IsRESONEAutomationCredential)) {

                $arguments += ('/user={0}' -f $Credential.UserName);
                $arguments += ('/password={0}' -f $Credential.GetNetworkCredential().Password);
                $exitCode = Start-WaitProcess -FilePath $wmcPath -ArgumentList $arguments;
            }
            elseif ($PSBoundParameters.ContainsKey('Credential')) {

                $exitCode = Start-WaitProcess -FilePath $wmcPath -ArgumentList $arguments -Credential $Credential;
            }
            else {

                $exitCode = Start-WaitProcess -FilePath $wmcPath -ArgumentList $arguments;
            }

            Write-Output -InputObject ([PSCustomObject] @{
                BuildingBlock = $filePath;
                ExitCode = $exitCode;
            });

        } #end foreach resolved path

    } #end process
} #end function Import-ROABuildingBlockFile