From f831b1365d76e987f1275570b3d78d09ec735a3d Mon Sep 17 00:00:00 2001 From: OCram85 Date: Thu, 26 Oct 2017 09:16:21 +0200 Subject: [PATCH 1/7] implements ExchangeConnection ( fix #3) --- src/Connection/Connect-To.ps1 | 63 ++++++++++++++++++++++++++---- src/Connection/Disconnect-From.ps1 | 28 +++++++++++-- 2 files changed, 79 insertions(+), 12 deletions(-) diff --git a/src/Connection/Connect-To.ps1 b/src/Connection/Connect-To.ps1 index de92e5c..5afdad4 100644 --- a/src/Connection/Connect-To.ps1 +++ b/src/Connection/Connect-To.ps1 @@ -20,6 +20,8 @@ function Connect-To { - NetAppFAS Establish a connection to a NetApp Clustered ONTAP filer. - VMware Establish a connection to a VMware vCenter or ESXi host. - CisServer Establish a connection to a Vmware CisServer. + - ExchangeHTTP Start a new remote session to the given Exchange server via unsecure http. + - Exchange HTTPS Start a new remote session to the given exchange server with the secure https endpoint. .PARAMETER Credentials Use this parameter to bypass the stored credentials. Without this parameter Connect-To tries to read the @@ -54,6 +56,12 @@ function Connect-To { .EXAMPLE Connect-To -RemoteHost "vCenter.myside.local" -Type CisServer + .EXAMPLE + Connect-To -RemoteHost "exchange01.myside.local" -Type ExchangeHTTP + + .EXAMPLE + Connect-To -RemoteHost "exchange01.myside.local" -Type ExchangeHTTPS + .EXAMPLE $MyCreds = Get-Credential Connect-To -RemoteHost "vcr01.myside.local" -Type VMware -Credentials $MyCreds @@ -61,29 +69,28 @@ function Connect-To { Disconnect-From -RemoteHost "vcr01.myside.local" -Type VMware .NOTES - ``` File Name : Connect-To.ps1 Author : Marco Blessing - marco.blessing@googlemail.com Requires : - ``` .LINK https://github.com/OCram85/PSCredentialStore #> + [CmdletBinding(DefaultParameterSetName = "Private")] param( [Parameter(Mandatory = $true, ParameterSetName = "Shared")] [Parameter(Mandatory = $true, ParameterSetName = "Private")] - [String]$RemoteHost, + [string]$RemoteHost, [Parameter(Mandatory = $false, ParameterSetName = "Shared")] [Parameter(Mandatory = $false, ParameterSetName = "Private")] - [String]$Identifier, + [string]$Identifier, [Parameter(Mandatory = $true, ParameterSetName = "Shared")] [Parameter(Mandatory = $true, ParameterSetName = "Private")] - [ValidateSet("CiscoUcs", "FTP", "NetAppFAS", "VMware", "CisServer")] - [String]$Type, + [ValidateSet('CiscoUcs', 'FTP', 'NetAppFAS', 'VMware', 'CisServer', 'ExchangeHTTP', 'ExchangeHTTPS')] + [string]$Type, [Parameter(Mandatory = $False, ParameterSetName = "Shared")] [Parameter(Mandatory = $False, ParameterSetName = "Private")] @@ -91,10 +98,10 @@ function Connect-To { [Parameter(Mandatory = $False, ParameterSetName = "Shared")] [ValidateNotNullOrEmpty()] - [String]$Path = "{0}\PSCredentialStore\CredentialStore.json" -f $env:ProgramData, + [string]$Path = "{0}\PSCredentialStore\CredentialStore.json" -f $env:ProgramData, [Parameter(Mandatory = $false, ParameterSetNAme = "Shared")] - [Switch]$Shared + [switch]$Shared ) begin { @@ -225,6 +232,46 @@ function Connect-To { Write-Error @MessageParams } } + "ExchangeHTTP" { + try { + $ConnectionParams = @{ + ConnectionURI = "http://{0}/powershell" -f $RemoteHost + ConfigurationName = 'Microsoft.Exchange' + Credential = $creds + ErrorAction = 'Stop' + } + $Global:PSExchangeRemote = New-PSSession @ConnectionParams + $Global:PSExchangeRemote + } + catch { + # Write a error message to the log. + $MessageParams = @{ + Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + ErrorAction = "Stop" + } + Write-Error @MessageParams + } + } + "ExchangeHTTPS" { + try { + $ConnectionParams = @{ + ConnectionURI = "https://{0}/powershell" -f $RemoteHost + ConfigurationName = 'Microsoft.Exchange' + Credential = $creds + ErrorAction = 'Stop' + } + $Global:PSExchangeRemote = New-PSSession @ConnectionParams + $Global:PSExchangeRemote + } + catch { + # Write a error message to the log. + $MessageParams = @{ + Message = "Unable to connect to {0} using Type {1}." -f $RemoteHost, $Type + ErrorAction = "Stop" + } + Write-Error @MessageParams + } + } default { # Write a error message to the log. $MessageParams = @{ diff --git a/src/Connection/Disconnect-From.ps1 b/src/Connection/Disconnect-From.ps1 index b67686a..abc864b 100644 --- a/src/Connection/Disconnect-From.ps1 +++ b/src/Connection/Disconnect-From.ps1 @@ -20,6 +20,8 @@ function Disconnect-From { - NetAppFAS Terminates the connection from a NetApp Clustered ONTAP filer. - VMware Terminates the connection from a VMware vCenter or ESXi host. - CisServer Terminates the connection from a Vmware CisServer. + - ExchangeHTTP Remove the existing remote session to the given Exchange server + - ExchangeHTTPS Remove the existing remote session to the given Exchange server .PARAMETER Force Force the disconnect, even if the disconnect would fail. @@ -47,12 +49,16 @@ function Disconnect-From { .EXAMPLE Disconnect-From -RemoteHost "vcenter.myside.local" -Type CisServer + .EXAMPLE + Disconnect-From -RemoteHost "exchange01.myside.local" -Type ExchangeHTTP + + .EXAMPLE + Disconnect-From -RemoteHost "exchange01.myside.local" -Type ExchangeHTTPS + .NOTES - ``` File Name : Disconnect-From.ps1 Author : Marco Blessing - marco.blessing@googlemail.com Requires : - ``` .LINK https://github.com/OCram85/PSCredentialStore @@ -64,14 +70,14 @@ function Disconnect-From { [string]$RemoteHost, [Parameter(Mandatory = $true)] - [ValidateSet("CiscoUcs", "FTP", "NetAppFAS", "VMware", "CisServer")] + [ValidateSet('CiscoUcs', 'FTP', 'NetAppFAS', 'VMware', 'CisServer', 'ExchangeHTTP', 'ExchangeHTTPS')] [string]$Type, [Parameter(Mandatory = $false)] [switch]$Force ) - switch ($Type) { + switch -Regex ($Type) { "VMware" { try { if ($Force) { @@ -134,6 +140,7 @@ function Disconnect-From { Write-Verbose @MessageParams $Global:CurrentNcController = $null } + catch { # Write a error message to the log. $MessageParams = @{ @@ -158,6 +165,19 @@ function Disconnect-From { Write-Error @MessageParams } } + "ExchangeHTTP*" { + try { + Get-Variable -Name 'PSExchangeRemote' -Scope Global -ErrorAction Stop + Remove-PSSession -Session $Global:PSExchangeRemote -ErrorAction Stop + } + catch { + $MessageParams = @{ + Message = "Unable to disconnect from {0} using Type {1}." -f $RemoteHost, $Type + ErrorAction = "Stop" + } + Write-Error @MessageParams + } + } default { # Write a error message to the log. $MessageParams = @{ -- 2.40.1 From cc37abda3c0aca4cbd603a0f6497ce764e9c5202 Mon Sep 17 00:00:00 2001 From: OCram85 Date: Thu, 7 Dec 2017 09:48:36 +0100 Subject: [PATCH 2/7] adds Test-CSConnection helper cmdlet --- src/Connection/Test-CSConnection.ps1 | 114 +++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/Connection/Test-CSConnection.ps1 diff --git a/src/Connection/Test-CSConnection.ps1 b/src/Connection/Test-CSConnection.ps1 new file mode 100644 index 0000000..8c595de --- /dev/null +++ b/src/Connection/Test-CSConnection.ps1 @@ -0,0 +1,114 @@ +function Test-CSConnection { + <# + .SYNOPSIS + Returns the connection state of a given type to the remote host. + + .DESCRIPTION + Use this script to check a connection which was established with the `Connect-To` cmdlet. + + .PARAMETER RemoteHost + Define the remote host you would like to check. + + .Parameter Type + Define the connection type you would like to check. See the `Connect-To` documentation + for valid type values. + + .INPUTS + [None] + + .OUTPUTS + [Boolean] + + .EXAMPLE + .\Test-CMConnection -RemoteHost "r0-i01-vcr01.p0r.kivbf-cloud.net" -Type VMware + + .NOTES + File Name : Test-CSConnection.ps1 + Author : Marco Blessing - marco.blessing@googlemail.com + Requires : + + .LINK + https://github.com/OCram85/PSCredentialStore + #> + + [CmdletBinding()] + [OutputType([boolean])] + param( + [Parameter(Mandatory = $true)] + [ValidateNotNullOrEmpty()] + [string]$RemoteHost, + + [Parameter(Mandatory = $True)] + [ValidateNotNullOrEmpty()] + [ValidateSet("CiscoUcs", "FTP", "NetAppFAS", "VMware")] + [string]$Type + ) + + switch ($Type) { + 'VMware' { + try { + $Conn = Get-Variable -Name DefaultVIServer -Scope Global -ErrorAction Stop + } + catch [System.Management.Automation.ItemNotFoundException] { + $MsgParams = @{ + Message = "There is no open PowerCLI VMware connection bound to 'DefaultVIServer'." + } + Write-Verbose @MsgParams + return $false + } + if ($Conn.Value.Name -eq $RemoteHost) { + if ($Conn.Value.IsConnected) { + $MsgParams = @{ + Message = "'DefaultVIServer' found. Connection to given remote host already established." + } + Write-Verbose @MsgParams + return $True + } + else { + $MsgParams = @{ + Message = "'DefaultVIServer' found. RemoteHost matches but the connection is closed." + } + Write-Verbose @MsgParams + return $false + } + } + } + + "CiscoUcs" { + $MsgParams = @{ + ErrorAction = "Stop" + Message = "CiscoUCS connection test is not implemented yet!" + } + Write-Error @MsgParams + return $false + } + + "FTP" { + $MsgParams = @{ + ErrorAction = "Stop" + Message = "FTP connection test is not implemented yet!" + } + Write-Error @MsgParams + return $false + } + + "NetAppFAS" { + $MsgParams = @{ + ErrorAction = "Stop" + Message = "NetAppFAS connection test is not implemented yet!" + } + Write-Error @MsgParams + return $false + } + + # The Default section will never be shown as long as the powershell framework isn't broken. + Default { + $MsgParams = @{ + ErrorAction = "Stop" + Message = "Panic: There is an invalid type value! This error should never be thrown." + } + Write-Error @MsgParams + return $false + } + } +} -- 2.40.1 From 55415bf9b58590e8da65ad158381c8e401232a47 Mon Sep 17 00:00:00 2001 From: OCram85 Date: Thu, 7 Dec 2017 09:55:40 +0100 Subject: [PATCH 3/7] change switch cases --- src/Connection/Test-CSConnection.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Connection/Test-CSConnection.ps1 b/src/Connection/Test-CSConnection.ps1 index 8c595de..63f7271 100644 --- a/src/Connection/Test-CSConnection.ps1 +++ b/src/Connection/Test-CSConnection.ps1 @@ -74,7 +74,7 @@ function Test-CSConnection { } } - "CiscoUcs" { + 'CiscoUcs' { $MsgParams = @{ ErrorAction = "Stop" Message = "CiscoUCS connection test is not implemented yet!" @@ -83,7 +83,7 @@ function Test-CSConnection { return $false } - "FTP" { + 'FTP' { $MsgParams = @{ ErrorAction = "Stop" Message = "FTP connection test is not implemented yet!" @@ -92,7 +92,7 @@ function Test-CSConnection { return $false } - "NetAppFAS" { + 'NetAppFAS' { $MsgParams = @{ ErrorAction = "Stop" Message = "NetAppFAS connection test is not implemented yet!" -- 2.40.1 From a47f9d1019127fed9aa8e7e66f2e11661c10110d Mon Sep 17 00:00:00 2001 From: OCram85 Date: Thu, 7 Dec 2017 09:55:50 +0100 Subject: [PATCH 4/7] export new function --- src/PSCredentialStore.psd1 | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PSCredentialStore.psd1 b/src/PSCredentialStore.psd1 index 863d6f3..abf44b3 100644 --- a/src/PSCredentialStore.psd1 +++ b/src/PSCredentialStore.psd1 @@ -74,6 +74,7 @@ # Connection Group 'Connect-To', 'Disconnect-From', + 'Test-CSConnection', # Item Group 'Get-CredentialStoreItem', 'Set-CredentialStoreItem', -- 2.40.1 From 0c981909de955f29bf47dd230961ab3fb16bbf48 Mon Sep 17 00:00:00 2001 From: OCram85 Date: Fri, 22 Dec 2017 07:59:28 +0100 Subject: [PATCH 5/7] reenable master build and deployment steps --- appveyor.yml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 2979436..891a0a8 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -44,14 +44,14 @@ deploy: # prerelease: true # on: # branch: dev - # - provider: GitHub - # auth_token: - # secure: M+bBX5/nKdJB0eViP7xtrLVTwf3vGDUA9N2MMprZp2i+9ZR3CBVcJnSzJWUmalhB - # artifact: PSCredentialStore.zip # upload all NuGet packages to release assets - # draft: false - # prerelease: false - # on: - # branch: master # release from master branch only + - provider: GitHub + auth_token: + secure: M+bBX5/nKdJB0eViP7xtrLVTwf3vGDUA9N2MMprZp2i+9ZR3CBVcJnSzJWUmalhB + artifact: PSCredentialStore.zip # upload all NuGet packages to release assets + draft: false + prerelease: false + on: + branch: master # build release on master branch changes after_deploy: - # - ps: Invoke-AppVeyorPSGallery -OnBranch 'master' + - ps: Invoke-AppVeyorPSGallery -OnBranch 'master' -- 2.40.1 From 8c9533e208079f647fb35145e4a8e0a51a0f9943 Mon Sep 17 00:00:00 2001 From: OCram85 Date: Fri, 22 Dec 2017 08:00:34 +0100 Subject: [PATCH 6/7] change versioning to semver --- appveyor.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 891a0a8..635d233 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 0.1.{build} +version: 0.2.0.{build} branches: only: -- 2.40.1 From 30f800e73fe743efce9e1e4ed4b2a1bfeaa1dfbe Mon Sep 17 00:00:00 2001 From: OCram85 Date: Fri, 22 Dec 2017 08:02:53 +0100 Subject: [PATCH 7/7] fix appveyor.yml syntax --- appveyor.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 635d233..19b96e0 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -45,8 +45,8 @@ deploy: # on: # branch: dev - provider: GitHub - auth_token: - secure: M+bBX5/nKdJB0eViP7xtrLVTwf3vGDUA9N2MMprZp2i+9ZR3CBVcJnSzJWUmalhB + auth_token: + secure: M+bBX5/nKdJB0eViP7xtrLVTwf3vGDUA9N2MMprZp2i+9ZR3CBVcJnSzJWUmalhB artifact: PSCredentialStore.zip # upload all NuGet packages to release assets draft: false prerelease: false -- 2.40.1