Implement String to SsoAdmin server argument transformation attribute
This commit is contained in:
@@ -175,16 +175,19 @@ function Disconnect-SsoAdminServer {
|
||||
ValueFromPipelineByPropertyName=$false,
|
||||
HelpMessage='SsoAdminServer object')]
|
||||
[ValidateNotNull()]
|
||||
[VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer]
|
||||
[VMware.vSphere.SsoAdmin.Utils.StirngToSsoAdminServerArgumentTransformationAttribute()]
|
||||
[VMware.vSphere.SsoAdminClient.DataTypes.SsoAdminServer[]]
|
||||
$Server)
|
||||
|
||||
Process {
|
||||
if ($global:DefaultSsoAdminServers.Contains($Server)) {
|
||||
$global:DefaultSsoAdminServers.Remove($Server) | Out-Null
|
||||
}
|
||||
foreach ($requestedServer in $Server) {
|
||||
if ($global:DefaultSsoAdminServers.Contains($requestedServer)) {
|
||||
$global:DefaultSsoAdminServers.Remove($requestedServer) | Out-Null
|
||||
}
|
||||
|
||||
if ($Server.IsConnected) {
|
||||
$Server.Disconnect()
|
||||
if ($requestedServer.IsConnected) {
|
||||
$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:DefaultSsoAdminServer variable");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,9 @@
|
||||
<PackageReference Include="Microsoft.WSMan.Runtime" Version="6.1.0" />
|
||||
<PackageReference Include="VMware.System.Private.ServiceModel" Version="4.4.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\VMware.vSphere.SsoAdminClient\VMware.vSphere.SsoAdminClient.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user