Public/New-WinSCPItem.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function New-WinSCPItem {

    [CmdletBinding(
        ConfirmImpact = "Low",
        HelpUri = "https://github.com/dotps1/WinSCP/wiki/New-WinSCPItem",
        SupportsShouldProcess = $true
    )]
    [OutputType(
        [WinSCP.RemoteFileInfo]
    )]

    param (
        [Parameter(
            Mandatory = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [ValidateScript({
            if ($_.Opened) {
                return $true
            } else {
                throw "The WinSCP Session is not in an Open state."
            }
        })]
        [WinSCP.Session]
        $WinSCPSession,

        [Parameter(
            Position = 0,
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [String[]]
        $Path = $WinSCPSession.HomePath,

        [Parameter(
            ValueFromPipelineByPropertyName = $true
        )]
        [String]
        $Name,

        [Parameter()]
        [String]
        $ItemType = "File",

        [Parameter()]
        [String]
        $Value,

        [Parameter()]
        [Switch]
        $Force,

        [Parameter()]
        [WinSCP.TransferOptions]
        $TransferOptions = ( New-Object -TypeName WinSCP.TransferOptions )
    )

    process {
        foreach ($pathValue in ( Format-WinSCPPathString -Path $Path )) {
            $nameParameterUsed = $PSBoundParameters.ContainsKey(
                "Name"
            )
            if ($nameParameterUsed) {
                $pathValue = Format-WinSCPPathString -Path ( Join-Path -Path $pathValue -ChildPath $Name )
            }

            $itemExits = Test-WinSCPPath -WinSCPSession $WinSCPSession -Path $pathValue
            if ($itemExits -and -not $Force.IsPresent) {
                Write-Error -Message "An item with the specified name '$pathValue' already exists."

                continue
            }

            try {
                $shouldProcess = $PSCmdlet.ShouldProcess(
                    $pathValue
                )
                if ($shouldProcess) {
                    $newItemParams = @{
                        Path = $env:TEMP
                        Name = (Split-Path -Path $pathValue -Leaf)
                        ItemType = $ItemType
                        Value = $Value
                        Force = $true
                    }

                    $newItem = New-Item @newItemParams
                    $result = $WinSCPSession.PutFiles(
                        $newItem.FullName, $pathValue, $true, $TransferOptions
                    )

                    if ($result.IsSuccess) {
                        Get-WinSCPItem -WinSCPSession $WinSCPSession -Path $pathValue
                    } else {
                        $PSCmdlet.WriteError(
                            $result.Check()
                        )
                    }
                }
            } catch {
                $PSCmdlet.WriteError(
                    $_
                )
            } finally {
                # Make sure to clean up our temp item if the transfer fails.
                if (Test-Path -Path $newItem.FullName) {
                    Remove-Item -Path $newItem.FullName -Force -Confirm:$false -Recurse -ErrorAction SilentlyContinue
                }
            }
        }
    }
}