Functions/Create/New-SOCatClient.psm1

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
#region: New-SOCatClient
<#
    .Synopsis
    Create a StoreOnce Catalyst Client on your StoreOnce system.
 
    .Description
    Create a StoreOnce Catalyst Client on all Service Sets on your StoreOnce system.
  
    .Parameter Server
    IP Address oder DNS Name of your StoreOnce system like defined via Connect-SOAppliance (check Get-SOConnections).
 
    .Parameter SOCatClientName
    Name for the new Client on your StoreOnce system.
 
    .Parameter SOCatClientPass
    Password for the new Client on your StoreOnce system.
 
    .Parameter SOCatClientCredential
    Name and Password per "Get-Credential" for the new Client on your StoreOnce system.
 
    .Parameter SOCatClientDesc
    Description for the new Client on your StoreOnce system.
 
    .Parameter Timeout
    Timeout for the Client creation process (Default is 30 Seconds).
 
    .Example
    New-SOCatClient -Server 192.168.2.1 -SOCatClientName MyNewClient -SOCatClientPass MyNewClientPass!!
 
    .Example
    New-SOCatClient -Server 192.168.2.1 -SOCatClientCredential (Get-Credential)
 
#Requires PS -Version 4.0
#>

function New-SOCatClient {
    [CmdletBinding(DefaultParametersetName="SOCatClientName")][OutputType('System.Management.Automation.PSObject')]
    param (
        [parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
            [String]$Server,
        [parameter(Mandatory=$true, Position=1, ParameterSetName="SOCatClientName")]
        [ValidateNotNullOrEmpty()]
            [String]$SOCatClientName,
        [parameter(Mandatory=$true, Position=2, ParameterSetName="SOCatClientName")]
        [ValidateNotNullOrEmpty()]
            [String]$SOCatClientPass,
        [Parameter(Mandatory=$true, Position=3, ParameterSetName="Credential")]
        [ValidateNotNullOrEmpty()]
            [Management.Automation.PSCredential]$SOCatClientCredential,
        [parameter(Mandatory=$false, Position=4)]
            [String]$SOCatClientDesc = $SOCatClientName,
        [parameter(Mandatory=$false, Position=5)]
            [Int]$Timeout = 30
            
    )
    Process {
        if (!$Global:SOConnections) {throw "No StoreOnce Appliance(s) connected! Use 'Connect-SOAppliance'"}
        if ($Server.count -gt 1) {throw "This Command only Supports one D2D System."}
        $Connection = $Global:SOConnections | Where {$_.Server -eq $Server}
        if (!$Connection) {throw "No D2D System found, check Get-SOConnections."}
        if ($Connection.count -gt 1) {throw "This Command only Supports one D2D System. Multiple Matches for $Server found..."}
        
        if ($PSBoundParameters.ContainsKey("SOCatClientCredential")){

            $SOCatClientName = $SOCatClientCredential.UserName
            $SOCatClientPass = $SOCatClientCredential.GetNetworkCredential().Password
        
        }  

        if (Test-IP -IP $($Connection.Server)) {
            if (Get-SOCatClients | where {$_.Name -eq $SOCatClientName -and $_.System -eq $($Connection.Server)}) {Throw "Client $SOCatClientName already Exists."}

            [Array]$IDs = (Get-SOSIDs | where {$_.System -eq $($Connection.Server)}).SSID

            for ($i = 1; $i -le $IDs.Count; $i++ ){
                $ClientCall = @{uri = "https://$($Connection.Server)/storeonceservices/cluster/servicesets/$i/services/cat/configs/clients/";
                                Method = 'POST';
                                Headers = @{Authorization = 'Basic ' + $($Connection.EncodedPassword);
                                            Accept = 'text/xml';
                                            'Content-Type' = 'application/x-www-form-urlencoded'
                                }
                                Body = @{name = $SOCatClientName;
                                        description = $SOCatClientDesc;
                                        password = $SOCatClientPass;
                                        canCreateStores = 'false';
                                        canSetServerProperties = 'false';
                                        canManageClientPermissions = 'false'
                                } 
                            }

                $ClientResponse = Invoke-RestMethod @ClientCall
            }
            
            $i = 0
            while(!(Get-SOCatClients | where {$_.Name -eq $SOCatClientName -and $_.System -eq $($Connection.Server)})){
                $i++
                Start-Sleep 1
            if($i -gt $Timeout) { Write-Error "Creating Client Failed."; break}
                Write-Progress -Activity "Creating Client" -Status "Wait for Client..."
            }
        }
        Return (Get-SOCatClients | where {$_.Name -eq $SOCatClientName -and $_.System -eq $($Connection.Server)} | ft * -AutoSize)
        
    }
}
#endregion