functions/private/ConvertTo-O365.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
function ConvertTo-O365{
    [cmdletbinding()]
    param(
    [parameter(Mandatory=$true)]
    [ValidateNotNullOrEmpty()]
    [string] $Path,
    [Parameter()]
    [ValidateNotNullOrEmpty()]
    [string] $Separator = "`t",
    [Parameter(Mandatory=$true)]
    [hashtable] $Licenses
    )
    try{
        $Inventory = new-object -TypeName psobject -Property @{
            Users = @()
            Groups = @()
        }
        $Content = Import-CSV -Path $Path -Delimiter $Separator
        $CSVProperties = get-member -InputObject $Content -MemberType NoteProperty
        $UserProperties = @()
        [user]::new()|get-member -MemberType Property|%{$UserProperties += $_.Name}
        foreach($Property in $CSVProperties){
            if($UserProperties -notcontains $Property.Name){
                throw "CSV was not in the correct format: '$($Property.Name)' is not recognized."
                
            }
        }
        foreach($Item in $Content){
            $User = new-object User
            foreach($Property in $UserProperties){
                switch($Property){
                    "DisplayName" {$User.DisplayName = [Regex]::Replace($Item.First,'[^a-zA-Z0-9]', '') + "." + [Regex]::Replace($Item.Last,'[^a-zA-Z0-9]', '')}
                    "Password"{break;}
                    "Login"{break;}
                    "Licenses" {
                        foreach($LicenseString in $Item.Licenses.Split("|")){
                            if(($License = $Licenses[$LicenseString]) -ne $null){
                                
                                if($License.Available -le 0){
                                    throw "There are not enough licenses available to provision the users ($($License.Name))"
                                }
                                $User.Licenses.Add($License)
                                $License.Available--
                            } else {
                                Write-Log -Type Warning -Message "License $($Item.License) for user $($Item.DisplayName) not found in the subscription"
                            }
                        }
                        break;
                    }
                    "Groups" {
                        $Group = new-object Group
                        <# if($Item.Groups.StartsWith('*')){
       $Group.Owner = ([ref]$User).value
       $Group.Name = $Item.Groups.substring(1,$Item.Groups.length - 1)
       $found = $false
       $Inventory.Groups.ForEach{
        if($_.Name -eq $Group.Name){
         $_.Owner = ([ref]$Group.Owner).value
         $found = $true
        }
       }
      } else { #>

                            $Group.Name = $Item.Groups
                            # }

                        if((($Inventory.Groups).ForEach{$_.Name}) -notcontains $Group.Name){
                            $Group.Owner = ([ref]$User).Value
                            $Inventory.Groups += $Group
                        } else {
                            $Group = ($Inventory.Groups.Where{$_.Name -eq $Group.Name})[0]
                        }
                        $User.Groups.Add($Group);
                        break;
                        
                    }
                    
                    default {$User.$_ = $Item.$_;break;}
                }
                
            }
            $Inventory.Users += $User
        }
        $global:a = $Inventory
        return $Inventory
        
    } catch {
        throw "Error while parsing CSV to O365 Inventory: '$_' @ $($_.InvocationInfo.ScriptLineNumber) - $($_.InvocationInfo.Line)"
    }

}