Public/SEPPmailLegacyConfig.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 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
<#
.SYNOPSIS Sets the Config to a defined SEPPmail instance and credential .DESCRIPTION Use this commandlet to define a SEPPmail Legacy configuration (SLConfig) for the SL Commandlets. Each SLConfig is stored with its FQND and the extension .config in the SLConfig directory. If you run Set-SLConfig, you need to specify a FQDN for a SEPPmail. The CmdLet reads the config, copies it over the default config (current.config) and loads it into the $SLConfig variable for use with other CmdLets. .EXAMPLE PS> Set-SLConfig -SEPPmailFQDN securemail.contoso.de This will read the config file for the FQDN and set it as current config (stores in in the global variable $SLConfig) .EXAMPLE PS> Set-SLConfig -SEPPmailFQDN securemail.contoso.de -SetasDefault This will read the config file for the FQDN and set it as current config and overwrites the default config file SLCurrent.config #> function Set-SLConfig { [CmdletBinding()] param ( [Parameter( Mandatory = $false, ValueFromPipelineByPropertyName = $true )] [String]$SEPPmailFQDN, [Parameter( Mandatory = $false, HelpMessage = 'Overwrites the default config file SLCurrent.Config so that future requests use the new config' )] [switch]$setAsDefault = $false ) begin { $conf = $null } process { if ($SEPPmailFQDN) { Write-Verbose "Check if a file $SEPPmailFQDN.config exists" $SLConfigFilePath = (Join-Path -Path $SLConfigPath -ChildPath $SEPPmailFQDN) + '.config' } else { Write-Verbose "No FQDN specified, load default config file" $SLConfigFilePath = Join-Path -Path $SLConfigPath -ChildPath 'SLCurrent.config' } if (!(Test-Path $SLConfigFilePath)) { Write-Warning 'Configuration file does not exist - please check FQDN or create a new configuration with New-SLConfig' break } Write-Verbose "Check if the $SEPPmailFQDN.config file contains all needed properties" $conf = Get-Content $SLConfigFilePath | ConvertFrom-Json If ((!($conf.SEPPmailFQDN)) -or (!($conf.Secret)) -or (!($conf.AdminPort))) { Write-Warning -Message "Configuration incomplete! Run New-SLConfig to create a proper configuration" break } Write-Verbose "Testing if $($conf.Secret) exists in secrets store." $SecFilePath = Join-Path -Path $SLConfigPath -ChildPath ("$($Conf.Secret)" + '.xml') if (!(Test-Path -Path $SecFilePath)) { Write-Warning "Stored credentials XML file $($conf.Secret) is missing! Run New-SLConfig to create a proper configuration" break } else { $secureSecret = Import-Clixml -Path $SecFilePath } if ($setAsDefault -eq $true) { Write-Verbose "Writing default-Config File SLCurrent.config" $defaultconfigFilePath = Join-Path $SLConfigPath -ChildPath 'SLCurrent.config' Set-Content $defaultconfigFilePath -Value ($conf| ConvertTo-Json) } Write-Verbose "Writing securesecret to config variable." $global:SLConfig = [ordered]@{ SEPPmailFQDN = $conf.SEPPmailFQDN Secret = $secureSecret AdminPort = $conf.Adminport SkipCertificateCheck = $true } } end { if (!(Test-Path $SLConfigFilePath)) { Write-Warning 'There is no current configuration file defined (SLConfig.config). Run New-SLConfig without the -NotCurrent parameter to create one.' } return $conf } } <# .SYNOPSIS Creates or overwrites a new Configuration file .DESCRIPTION Use this commandlet to create a SEPPmail Legacy configuration (SLConfig) for the SL Commandlets. Each SLConfig is stored with its FQND and the extension .config in the SLCOnfig directory. If you run New-SLConfig, you need to specify a FQDN a username and a Password for the Legacy-enabled User for a SEPPmail. The CmdLet creates the config file. .EXAMPLE PS> New-SLConfig -SEPPmailFQDN securemail.contoso.de -UserName Legacyadmin@contoso.de This will create the config file for the FQDN. .EXAMPLE PS> New-SLConfig -SEPPmailFQDN localhost -UserName Legacyadmin@contoso.de -SkipCertificateCheck $true This will create the config file for the FQDN and will not run Certificatechecks on this machine. .EXAMPLE PS> New-SLConfig -SEPPmailFQDN securemail.contoso.de -UserName Legacyadmin@contoso.de -AdminPort 10443 This will create the config file for the FQDN with a different AdminPort .EXAMPLE PS> New-SLConfig -SEPPmailFQDN securemail.contoso.de -UserName Legacyadmin@contoso.de -NotCurrent This will create a config file but NOT copy it to the SLConfig.config. So it will not be used immediately. #> function New-SLConfig { [CmdletBinding()] param ( [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias("FQDN")] [ValidatePattern("^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$")] [String]$SEPPmailFQDN, [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias("sec")] [String]$UserName, [Parameter( Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'Default is 8443, but you may use another port like 10443')] [Alias("Port")] [String]$AdminPort = '8443', [Parameter( Mandatory = $false, ValueFromPipelineByPropertyName = $true, HelpMessage = 'For testmachines which have no valid certificates, turn this on by simply adding -SkipCertificateCheck $true in the commandline')] [Alias("Skip")] [bool]$SkipCertificateCheck = $false, [Parameter( Mandatory = $false, HelpMessage = 'Set if you do NOT want this new config to be set as default' )] [Switch]$NotCurrent = $false ) begin { Write-Verbose 'Create an empty config hashtable' $conf = [ordered]@{} } process { $SecFilePath = Join-Path -Path $SLConfigPath -ChildPath ("$UserName" + ".xml") Write-Verbose "Create secret here: $SecFilePath if it does not exists" if (!(Test-Path -Path $SecFilePath)) { Write-Verbose "Secret for username: $userName not found, creating new" [Securestring]$secretPassword = Read-Host -Prompt "Enter Password for the above username" -AsSecureString New-Object -TypeName PSCredential -ArgumentList ($userName, $secretpassword) | Export-Clixml -Path $SecFilePath } Write-Verbose "Set Parametervalues into temporary config" $conf.SEPPmailFQDN = $SEPPmailFQDN If ($UserName) { $conf.Secret = $UserName } If ($AdminPort) { $conf.AdminPort = $AdminPort } $conf.SkipCertificateCheck = $SkipCertificateCheck Write-Verbose "Writing new config to file." $SLConfigFilePath = (Join-Path $SLConfigPath -ChildPath $SEPPmailFQDN) + ".config" $conf | ConvertTo-Json | New-Item -Path $SLConfigFilePath -Force | Out-Null If ($Notcurrent) { Write-Verbose 'Just created config file, not copying it to SLCurrent.config' } else { $CurrentSLConfigFilePath = (Join-Path $SLConfigPath -ChildPath 'SLCurrent') + '.config' Copy-Item -Path $SLConfigFilePath -Destination $CurrentSLConfigFilePath | Out-Null } } end { return $conf } } <# .SYNOPSIS Test a SEPPmail config .DESCRIPTION After defining a configuration to a SEPPmail appliance, this CmdLet tests to read some data .EXAMPLE PS C:\> Test-SLConfig -SEPPmailFQDN 'securemail.contoso.de' Tests if legacyapi access to securemail.contoso.de works and raises relevant errors. #> function Test-SLConfig { [CmdLetBinding()] param() begin { Set-SLConfig } process { try { if ($IsWindows) { if (!((Resolve-DnsName -Name $SLConfig.SEPPmailFQDN -ErrorAction 0).IPAddress)) { Write-Error "Could not resolve SEPPmailFQDN, please check DNS and FQDN Name!" } else { Write-Host "DNS query to $($SLConfig.SEPPmailFQDN) worked." -ForegroundColor Green } } #((Test-Netconnection -ComputerName $SLConfig.SEPPmailFQDN -Port $SLConfig.AdminPort).TcpTestSucceeded) if ($IsWindows -or ($PSversiontable.PSEdition -eq 'Desktop')) { if (!((Test-NetConnection -ComputerName $SLConfig.SEPPmailFQDN -Port $SLConfig.AdminPort -WarningAction SilentlyContinue).TcpTestSucceeded)) { Write-Error "Could not connect to port $SLConfig.AdminPort! Check Firewalls and Port configuration." } else { Write-Host "TCP Connect to $($SLConfig.SEPPmailFQDN) on Port $($SLConfig.AdminPort) worked." -ForegroundColor Green } } else { if (!(Test-Connection -ComputerName $SLConfig.SEPPmailFQDN -TcpPort $SLConfig.AdminPort -WarningAction SilentlyContinue -Quiet)) { Write-Error "Could not connect to port $SLConfig.AdminPort! Check Firewalls and Port configuration." } else { Write-Host "TCP Connect to $($SLConfig.SEPPmailFQDN) on Port $($SLConfig.AdminPort) worked." -ForegroundColor Green } } # Try login at SEPPmail and receive group INfo $urlroot = New-SLUrlRoot -FQDN $SLConfig.SEPPmailFQDN -adminPort $SLConfig.adminPort $uri = $urlroot + 'statistics' + '?' + 'returnType' + '=' + 'CSV' try { if ((Invoke-RestMethod -Uri $uri -Method GET -Credential $SLConfig.secret | ConvertFrom-Csv -Delimiter ';' | Select-Object -First 1).Length -eq '1') { Write-Host "Data access with $($SLConfig.secret.UserName) worked." -ForegroundColor Green } } catch { Write-Error "Most likely an access error" Write-Error "Check e-Mail/password and membership of the user to the group `"legacyappadmin`". Create the group if necessary." $_ } } catch { Write-Error "Configuratiton is not valid! See error below" $_ } } end { # No code here } } <# .SYNOPSIS Removes a Configuration file .DESCRIPTION Use this commandlet to remove an existing SEPPmail Legacy configuration (SLConfig) for the SL Commandlets. .EXAMPLE PS> Remove-SLConfig -SEPPmailFQDN securemail.contoso.de This will remove the config file for the FQDN and the secret in the BuildInLocalLault #> function Remove-SLConfig { [CmdletBinding()] param ( [Parameter( Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [Alias("FQDN")] [String]$SEPPmailFQDN ) begin { } process { try { Write-Verbose "Read File " $FQDNConfigFilePath = (Join-Path $SLConfigPath -ChildPath $SEPPmailFQDN) + ".config" if ($FQDNConfigFilePath) { Write-Verbose 'Config file found, trying to remove secrets' $SecretName = (Get-Content $FQDNConfigFilePath | ConvertFrom-Json).Secret $SecFilePath = Join-Path -Path $SLConfigPath -ChildPath ("$SecretName" + ".xml") If ((Import-Clixml -Path $SecFilePath -ea 0)) { Write-Verbose "Removing Credentials file $SecFilePath" Remove-Item -Path $SecFilePath -Force } if (Test-Path $FQDNConfigFilePath) { Write-Verbose "Removing File $FQDNConfigFilePath" Remove-Item -Path $FQDNConfigFilePath -Force } else { Write-Warning "Config File for $SEPPmailFQDN not found" } } } catch { $_.Exception } } end { } } <# .SYNOPSIS List existing configurations .DESCRIPTION Reads the .SEPPmailLegacy directory and reads all sonfig files. .EXAMPLE PS C:\> Find-SLConfig Lists all the configurations found, including parameters .EXAMPLE PS C:\> Find-SLConfig -Config securemail.sontoso.de List a specific config, including parameters #> function Find-SLConfig { [CmdletBinding()] param ( [Parameter( Mandatory = $false )] [String]$ConfigName ) begin { try { if ($ConfigName) { Write-Verbose 'Storing names $Configurations array' $Configurations = @(Get-ChildItem -Path (Join-Path $SLConfigPath -ChildPath '\*.config') -Exclude 'SLCurrent*' | Where-Object Name -Like $ConfigName) } else { Write-Verbose 'Storing $Configurations array of all configuration files' $Configurations = @(Get-ChildItem -Path (Join-Path $SLConfigPath -ChildPath '\*.config')-Exclude 'SLCurrent*') } } catch { Write-Error "Find-SLConfig failes with error $_.CategoryInfo" } } process { try { Write-Verbose 'Looping through $configurations array' foreach ($conf in $Configurations) { Write-Verbose 'Emit Configuration' Get-Content $conf | ConvertFrom-Json } } catch { Write-Error "Find-SLConfig failes with error $_.CategoryInfo" } } end { } } # SIG # Begin signature block # MIIL1wYJKoZIhvcNAQcCoIILyDCCC8QCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB # gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR # AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQU5pzYxv9teFj4dO/ZQ4diAT6r # +8uggglAMIIEmTCCA4GgAwIBAgIQcaC3NpXdsa/COyuaGO5UyzANBgkqhkiG9w0B # AQsFADCBqTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDHRoYXd0ZSwgSW5jLjEoMCYG # A1UECxMfQ2VydGlmaWNhdGlvbiBTZXJ2aWNlcyBEaXZpc2lvbjE4MDYGA1UECxMv # KGMpIDIwMDYgdGhhd3RlLCBJbmMuIC0gRm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkx # HzAdBgNVBAMTFnRoYXd0ZSBQcmltYXJ5IFJvb3QgQ0EwHhcNMTMxMjEwMDAwMDAw # WhcNMjMxMjA5MjM1OTU5WjBMMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMdGhhd3Rl # LCBJbmMuMSYwJAYDVQQDEx10aGF3dGUgU0hBMjU2IENvZGUgU2lnbmluZyBDQTCC # ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJtVAkwXBenQZsP8KK3TwP7v # 4Ol+1B72qhuRRv31Fu2YB1P6uocbfZ4fASerudJnyrcQJVP0476bkLjtI1xC72Ql # WOWIIhq+9ceu9b6KsRERkxoiqXRpwXS2aIengzD5ZPGx4zg+9NbB/BL+c1cXNVeK # 3VCNA/hmzcp2gxPI1w5xHeRjyboX+NG55IjSLCjIISANQbcL4i/CgOaIe1Nsw0Rj # gX9oR4wrKs9b9IxJYbpphf1rAHgFJmkTMIA4TvFaVcnFUNaqOIlHQ1z+TXOlScWT # af53lpqv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