Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CreateBulkConnections_ConfCons2_6.ps1 « Tools - github.com/mRemoteNG/mRemoteNG.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b39a2598d97c8bf34ee6417b83407b3768a29818 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#####################################
# Authors: David Sparer & Jack Denton
# Summary:
#   This is intended to be a template for creating connections in bulk. This uses the serializers directly from the mRemoteNG binaries.
#   You will still need to create the connection info objects, but the library will handle serialization. It is expected that you
#   are familiar with PowerShell. If this is not the case, reach out to the mRemoteNG community for help.
# Usage:
#   Replace or modify the examples that are shown toward the end of the script to create your own connection info objects.
#####################################

foreach ($Path in 'HKLM:\SOFTWARE\WOW6432Node\mRemoteNG', 'HKLM:\SOFTWARE\mRemoteNG') {
    Try {
        $mRNGPath = (Get-ItemProperty -Path $Path -Name InstallDir -ErrorAction Stop).InstallDir
        break
    }
    Catch {
        continue
    }
}
if (!$mRNGPath) {
    Add-Type -AssemblyName System.Windows.Forms
    $FolderBrowser = [System.Windows.Forms.FolderBrowserDialog]@{
        Description         = 'Please select the folder which contains mRemoteNG.exe'
        ShowNewFolderButton = $false
    }
    
    $Response = $FolderBrowser.ShowDialog()
    
    if ($Response.value__ -eq 1) {
        $mRNGPath = $FolderBrowser.SelectedPath
    }
    elseif ($Response.value__ -eq 2) {
        Write-Warning 'A folder containing mRemoteNG.exe has not been selected'
        return
    }
}
$null = [System.Reflection.Assembly]::LoadFile((Join-Path -Path $mRNGPath -ChildPath "mRemoteNG.exe"))
Add-Type -Path (Join-Path -Path $mRNGPath -ChildPath "BouncyCastle.Crypto.dll")



function ConvertTo-mRNGSerializedXml {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory)]
        [mRemoteNG.Connection.ConnectionInfo[]]
        $Xml
)

    function Get-ChildNodes {
        Param ($Xml)

        $Xml

        if ($Xml -is [mRemoteNG.Container.ContainerInfo] -and $Xml.HasChildren()) {
            foreach ($Node in $Xml.Children) {
                Get-ChildNodes -Xml $Node
            }
        }
    }

    $AllNodes = Get-ChildNodes -Xml $Xml
    if (
        $AllNodes.Password -or
        $AllNodes.RDGatewayPassword -or
        $AllNodes.VNCProxyPassword
    ) {
        $Password = Read-Host -Message 'If you have password protected your ConfCons.xml please enter the password here otherwise just press enter' -AsSecureString
    }
    else {
        $Password = [securestring]::new()
    }
    $CryptoProvider = [mRemoteNG.Security.SymmetricEncryption.AeadCryptographyProvider]::new()
    $SaveFilter = [mRemoteNG.Security.SaveFilter]::new()
    $ConnectionNodeSerializer = [mRemoteNG.Config.Serializers.Xml.XmlConnectionNodeSerializer26]::new($CryptoProvider, $Password, $SaveFilter)
    $XmlSerializer = [mRemoteNG.Config.Serializers.Xml.XmlConnectionsSerializer]::new($CryptoProvider, $ConnectionNodeSerializer)

    $RootNode = [mRemoteNG.Tree.Root.RootNodeInfo]::new('Connection')
    foreach ($Node in $Xml) {
        $RootNode.AddChild($Node)
    }
    $XmlSerializer.Serialize($RootNode)
}

function New-mRNGConnection {
    [CmdletBinding(DefaultParameterSetName = 'Credential')]
    Param (
        [Parameter(Mandatory)]
        [string]
        $Name,

        [Parameter(Mandatory)]
        [string]
        $Hostname,

        [Parameter(Mandatory)]
        [mRemoteNG.Connection.Protocol.ProtocolType]
        $Protocol,

        [Parameter(ParameterSetName = 'Credential')]
        [pscredential]
        $Credential,

        [Parameter(ParameterSetName = 'InheritCredential')]
        [switch]
        $InheritCredential,

        [Parameter()]
        [mRemoteNG.Container.ContainerInfo]
        $ParentContainer,

        [Parameter()]
        [switch]
        $PassThru
    )

    $Connection = [mRemoteNG.Connection.ConnectionInfo]@{
        Name     = $Name
        Hostname = $Hostname
        Protocol = $Protocol
    }

    if ($Credential) {
        $Connection.Username = $Credential.GetNetworkCredential().UserName
        $Connection.Domain = $Credential.GetNetworkCredential().Domain
        $Connection.Password = $Credential.GetNetworkCredential().Password
    }

    if ($InheritCredential) {
        $Connection.Inheritance.Username = $true
        $Connection.Inheritance.Domain = $true
        $Connection.Inheritance.Password = $true
    }

    if ($ParentContainer) {
        $ParentContainer.AddChild($Connection)

        if ($PSBoundParameters.ContainsKey('PassThru')) {
            $Connection
        }
    }
    else {
        $Connection
    }
}

function New-mRNGContainer {
    [CmdletBinding(DefaultParameterSetName = 'Credential')]
    Param (
        [Parameter(Mandatory)]
        [string]
        $Name,

        [Parameter(ParameterSetName = 'Credential')]
        [pscredential]
        $Credential,

        [Parameter(ParameterSetName = 'InheritCredential')]
        [switch]
        $InheritCredential,

        [Parameter()]
        [mRemoteNG.Container.ContainerInfo]
        $ParentContainer
    )

    $Container = [mRemoteNG.Container.ContainerInfo]@{
        Name = $Name
    }

    if ($Credential) {
        $Container.Username = $Credential.GetNetworkCredential().UserName
        $Container.Domain = $Credential.GetNetworkCredential().Domain
        $Container.Password = $Credential.GetNetworkCredential().Password
    }

    if ($InheritCredential) {
        $Container.Inheritance.Username = $true
        $Container.Inheritance.Domain = $true
        $Container.Inheritance.Password = $true
    }

    if ($ParentContainer) {
        $ParentContainer.AddChild($Container)
    }
    
    $Container
}

function Export-mRNGXml {
    [CmdletBinding()]
    param (
        [Parameter()]
        [string]
        $Path,

        [Parameter()]
        [string]
        $SerializedXml
    )

    $FilePathProvider = [mRemoteNG.Config.DataProviders.FileDataProvider]::new($Path)
    $filePathProvider.Save($SerializedXml)
}




#----------------------------------------------------------------
# Example 1: serialize many connections, no containers
# Here you can define the number of connection info objects to create
# You can also provide a list of desired hostnames and iterate over those

$Connections = foreach ($i in 1..5) {
    # Create new connection
    $Splat = @{
        Name              = 'Server-{0:D2}' -f $i
        Hostname          = 'Server-{0:D2}' -f $i
        Protocol          = 'RDP'
        InheritCredential = $true
    }
    New-mRNGConnection @Splat
}

# Serialize the connections
$SerializedXml = ConvertTo-mRNGSerializedXml -Xml $Connections

# Write the XML to a file ready to import into mRemoteNG
Export-mRNGXml -Path "$ENV:APPDATA\mRemoteNG\PowerShellGenerated.xml" -SerializedXml $SerializedXml

# Now open up mRemoteNG and press Ctrl+O and open up the exported XML file




#----------------------------------------------------------------
# Example 2: serialize a container which has connections
# You can also create containers and add connections and containers to them, which will be nested correctly when serialized
# If you specify the ParentContainer parameter for new connections then there will be no output unless the PassThru parameter is also used

$ProdServerCreds = Get-Credential
$ProdServers = New-mRNGContainer -Name 'ProdServers' -Credential $ProdServerCreds

foreach ($i in 1..3) {
    # Create new connection
    $Splat = @{
        Name              = 'Server-{0:D2}' -f $i
        Hostname          = 'Server-{0:D2}' -f $i
        Protocol          = 'RDP'
        InheritCredential = $true
        ParentContainer   = $ProdServers
    }
    New-mRNGConnection @Splat
}

$ProdWebServers = New-mRNGContainer -Name 'WebServers' -ParentContainer $ProdServers -InheritCredential

foreach ($i in 1..3) {
    # Create new connection
    $Splat = @{
        Name              = 'WebServer-{0:D2}' -f $i
        Hostname          = 'WebServer-{0:D2}' -f $i
        Protocol          = 'SSH1'
        InheritCredential = $true
        ParentContainer   = $ProdWebServers
    }
    New-mRNGConnection @Splat
}

$DevServers = New-mRNGContainer -Name 'DevServers'

foreach ($i in 1..3) {
    # Create new connection
    $Splat = @{
        Name              = 'DevServer-{0:D2}' -f $i
        Hostname          = 'DevServer-{0:D2}' -f $i
        Protocol          = 'RDP'
        InheritCredential = $true
        ParentContainer   = $DevServers
        PassThru          = $true
    }

    # Specified the PassThru parameter in order to catch the connection and change a property
    $Connection = New-mRNGConnection @Splat
    $Connection.Resolution = 'FullScreen'
}

# Serialize the container
$SerializedXml = ConvertTo-mRNGSerializedXml -Xml $ProdServers, $DevServers

# Write the XML to a file ready to import into mRemoteNG
Export-mRNGXml -Path "$ENV:APPDATA\mRemoteNG\PowerShellGenerated.xml" -SerializedXml $SerializedXml

# Now open up mRemoteNG and press Ctrl+O and open up the exported XML file