Merge pull request #389 from dmilov/topic/obn-for-server-parameter
Implement OBN for -Server parameter of Disconnect-SsoAdminServer funstion. Fixes https://github.com/vmware/PowerCLI-Example-Scripts/issues/387
This commit is contained in:
@@ -174,7 +174,8 @@ function Disconnect-SsoAdminServer {
|
|||||||
ValueFromPipelineByPropertyName = $false,
|
ValueFromPipelineByPropertyName = $false,
|
||||||
HelpMessage = 'SsoAdminServer object')]
|
HelpMessage = 'SsoAdminServer object')]
|
||||||
[ValidateNotNull()]
|
[ValidateNotNull()]
|
||||||
[VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer]
|
[VMware.vSphere.SsoAdmin.Utils.StirngToSsoAdminServerArgumentTransformationAttribute()]
|
||||||
|
[VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer[]]
|
||||||
$Server
|
$Server
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -192,12 +193,14 @@ function Disconnect-SsoAdminServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($global:DefaultSsoAdminServers.Contains($Server)) {
|
foreach ($requestedServer in $Server) {
|
||||||
$global:DefaultSsoAdminServers.Remove($Server) | Out-Null
|
if ($global:DefaultSsoAdminServers.Contains($requestedServer)) {
|
||||||
|
$global:DefaultSsoAdminServers.Remove($requestedServer) | Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($Server.IsConnected) {
|
if ($requestedServer.IsConnected) {
|
||||||
$Server.Disconnect()
|
$requestedServer.Disconnect()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,54 @@
|
|||||||
|
// **************************************************************************
|
||||||
|
// Copyright 2020 VMware, Inc.
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Management.Automation;
|
||||||
|
using System.Management.Automation.Runspaces;
|
||||||
|
using System.Security;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using VMware.vSphere.SsoAdminClient.DataTypes;
|
||||||
|
|
||||||
|
namespace VMware.vSphere.SsoAdmin.Utils
|
||||||
|
{
|
||||||
|
public class StirngToSsoAdminServerArgumentTransformationAttribute : ArgumentTransformationAttribute
|
||||||
|
{
|
||||||
|
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData) {
|
||||||
|
object result = inputData;
|
||||||
|
|
||||||
|
if (inputData is string obnValue &&
|
||||||
|
!string.IsNullOrEmpty(obnValue)) {
|
||||||
|
// Adopt PowerShell regex chars
|
||||||
|
var csharpObnValue = obnValue.Replace("*", ".*").Replace("?", ".?");
|
||||||
|
result = null;
|
||||||
|
|
||||||
|
var obnMatchingServers = new List<SsoAdminServer>();
|
||||||
|
|
||||||
|
var ssoAdminServerVariable = engineIntrinsics.SessionState.PSVariable.GetValue("DefaultSsoAdminServers");
|
||||||
|
|
||||||
|
if (ssoAdminServerVariable is PSObject ssoAdminServersPsObj &&
|
||||||
|
ssoAdminServersPsObj.BaseObject is List<SsoAdminServer> connectedServers) {
|
||||||
|
foreach (var server in connectedServers) {
|
||||||
|
if (!string.IsNullOrEmpty(Regex.Match(server.ToString(), csharpObnValue)?.Value)) {
|
||||||
|
obnMatchingServers.Add(server);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (obnMatchingServers.Count > 0) {
|
||||||
|
result = obnMatchingServers.ToArray();
|
||||||
|
} else {
|
||||||
|
// Non-terminating error for not matching value
|
||||||
|
engineIntrinsics.Host.UI.WriteErrorLine($"'{obnValue}' doesn't match any objects in $global:DefaultSsoAdminServers variable");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -22,4 +22,8 @@
|
|||||||
<PackageReference Include="VMware.System.Private.ServiceModel" Version="4.4.4" />
|
<PackageReference Include="VMware.System.Private.ServiceModel" Version="4.4.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\VMware.vSphere.SsoAdminClient\VMware.vSphere.SsoAdminClient.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -159,5 +159,60 @@ Describe "Connect-SsoAdminServer and Disconnect-SsoAdminServer Tests" {
|
|||||||
$global:DefaultSsoAdminServers | Should Not Contain $expected
|
$global:DefaultSsoAdminServers | Should Not Contain $expected
|
||||||
$expected.IsConnected | Should Be $false
|
$expected.IsConnected | Should Be $false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
It 'Disconnects DefaultSsoAdminServers when * is specified on -Server parameter' {
|
||||||
|
# Arrange
|
||||||
|
$expected = Connect-SsoAdminServer `
|
||||||
|
-Server $VcAddress `
|
||||||
|
-User $User `
|
||||||
|
-Password $Password `
|
||||||
|
-SkipCertificateCheck
|
||||||
|
|
||||||
|
# Act
|
||||||
|
Disconnect-SsoAdminServer -Server "*"
|
||||||
|
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
$global:DefaultSsoAdminServers.Count | Should Be 0
|
||||||
|
$expected.IsConnected | Should Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'Disconnects server specified as string that is equal to VC Address' {
|
||||||
|
# Arrange
|
||||||
|
$expected = Connect-SsoAdminServer `
|
||||||
|
-Server $VcAddress `
|
||||||
|
-User $User `
|
||||||
|
-Password $Password `
|
||||||
|
-SkipCertificateCheck
|
||||||
|
|
||||||
|
# Act
|
||||||
|
Disconnect-SsoAdminServer -Server $VcAddress
|
||||||
|
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
$global:DefaultSsoAdminServers.Count | Should Be 0
|
||||||
|
$expected.IsConnected | Should Be $false
|
||||||
|
}
|
||||||
|
|
||||||
|
It 'Disconnect-SsoAdminServer fails when string that does not match any servers is specified' {
|
||||||
|
# Arrange
|
||||||
|
$expected = Connect-SsoAdminServer `
|
||||||
|
-Server $VcAddress `
|
||||||
|
-User $User `
|
||||||
|
-Password $Password `
|
||||||
|
-SkipCertificateCheck
|
||||||
|
|
||||||
|
# Act
|
||||||
|
{ Disconnect-SsoAdminServer -Server "testserver" } | Should Throw
|
||||||
|
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
$global:DefaultSsoAdminServers.Count | Should Be 1
|
||||||
|
$global:DefaultSsoAdminServers[0] | Should Be $expected
|
||||||
|
$expected.IsConnected | Should Be $true
|
||||||
|
|
||||||
|
# Cleanup
|
||||||
|
Disconnect-SsoAdminServer -Server $expected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user