StarWars.psm1
#region AD Functions Function New-StarWarsADUser{ <# .SYNOPSIS Create Active Directory account based on the Star Wars world. .DESCRIPTION Use Active Directory module and swapi.com to create users in your Active Directory. .PARAMETER AccountPassword Passwords for the accounts. Defaulted to 123+aze. .PARAMETER UPNSuffix UPN suffix for the accounts. .PARAMETER MailDomain Mail domain for the accounts. .PARAMETER Path Path of the OU to create the account. Defaulted to the default users creation destination. .PARAMETER PassThru Output the progress. .EXAMPLE New-StarWarsADUser -Path 'OU=Users,OU=Star Wars,OU=Prod,DC=D2K16,DC=itfordummies,DC=net' Create all Star Wars users in the specified OU. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> [CmdletBinding()] Param( [String]$AccountPassword = '123+aze', [String]$UPNSuffix = (Get-ADDomain | Select-Object -ExpandProperty DNSRoot), [String]$MailDomain = 'itfordummies.net', [String]$Path = (Get-ADDomain | Select-Object -ExpandProperty UsersContainer), [Switch]$PassThru ) $ApiUrl = 'http://swapi.dev/api' 1..$(((Invoke-WebRequest -Uri $ApiUrl/people).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentUser = (Invoke-WebRequest -Uri $ApiUrl/people/$_).Content | ConvertFrom-Json | Select-Object -Property Height,Mass,Hair_Color,Skin_Color,Eye_Color,Birth_Year,Gender, @{Label='Name';Expression={$_.Name -replace 'é','é'}}, @{Label='Homeworld';Expression={(Invoke-WebRequest -Uri $_.homeworld).Content | ConvertFrom-Json}}, @{Label='Films';Expression={($_.Films | ForEach-Object -Process {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, @{Label='species';Expression={($_.species | ForEach-Object -Process {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, #@{Label='vehicles';Expression={($_.vehicles | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, #@{Label='starships';Expression={($_.starships | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, @{Label='url';Expression={(Invoke-WebRequest -Uri $_.url).Content | ConvertFrom-Json}} $GivenName = ($CurrentUser.Name -split ' ')[0] $SurName = ($CurrentUser.Name -split ' ')[-1] if($GivenName -eq $SurName){ $SurName = $null $FullSamAccountName = $SamAccountName = $GivenName } else{ $SamAccountName = ("$(($CurrentUser.Name -split ' ')[0]).$(($CurrentUser.Name -split ' ')[-1])" -replace ' ','')[0..19] -join '' $FullSamAccountName = "$(($CurrentUser.Name -split ' ')[0]).$(($CurrentUser.Name -split ' ')[-1])" -replace ' ','' } Write-Verbose -Message "Creating $SamAccountName..." New-Object -TypeName PSObject -Property @{ Description = "$($CurrentUser.Gender), $($CurrentUser.Height)cm for $($CurrentUser.Mass)kg. $($CurrentUser.'Hair_Color') hair, $($CurrentUser.'Skin_Color') skin, $($CurrentUser.'Eye_Color') eyes. Born in $($CurrentUser.'Birth_Year')." Office = $CurrentUser.Homeworld.Name #MemberOf = ($CurrentUser.Films.title -join ','),($CurrentUser.species.Name -join ',') -join ',' #,($CurrentUser.vehicles.Name -join ','),($CurrentUser.starships.Name -join ',') #Info = (($CurrentUser.Films.title,$CurrentUser.species.Name) -join ',').TrimEnd(',') #,($CurrentUser.vehicles.Name -join ','),($CurrentUser.starships.Name -join ',') Name = $CurrentUser.Name DisplayName = $CurrentUser.Name GivenName = $GivenName Surname = $SurName SamAccountName = $FullSamAccountName EmailAddress = "$FullSamAccountName@$MailDomain" MailNickName = $FullSamAccountName UserPrincipalName = "$FullSamAccountName@$UPNSuffix" Company = 'Star Wars' Department = $CurrentUser.Homeworld.Name Title = $CurrentUser.species.Name -join ',' } | New-ADUser -Path $Path -AccountPassword (ConvertTo-SecureString -AsPlainText -Force -String $AccountPassword) -Enable $true -PassThru:$PassThru -OtherAttributes @{info=((($CurrentUser.Films.title -join ','),($CurrentUser.species.Name -join ',')) -join ',').TrimEnd(',')} $CurrentUser = $SurName = $GivenName = $SamAccountName = $FullSamAccountName = $null } } Function New-StarWarsADGroup{ <# .SYNOPSIS Create Active Directory groups based on the Star Wars world. .DESCRIPTION Use Active Directory module and swapi.com to create groups in your Active Directory. .PARAMETER Path Path of the OU to create the groups. Defaulted to the default users creation destination. .PARAMETER PassThru Output the progress. .EXAMPLE New-StarWarsADGroup -Path 'OU=Groups,OU=Star Wars,OU=Prod,DC=D2K16,DC=itfordummies,DC=net' Create all Star Wars groups in the specified OU. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> [CmdletBinding()] Param( [String]$Path = (Get-ADDomain | Select-Object -ExpandProperty UsersContainer), [Switch]$PassThru ) $ApiUrl = 'http://swapi.dev/api' #Films 1..$(((Invoke-WebRequest -Uri $ApiUrl/films).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentGroup = (Invoke-WebRequest -Uri $ApiUrl/films/$_).Content | ConvertFrom-Json Write-Verbose -Message "Adding $($CurrentGroup.title) to the group list..." New-Object -TypeName PSObject -Property @{ Name = $CurrentGroup.title Description = "Produced by $($CurrentGroup.producer), diected by $($CurrentGroup.director) released on $($CurrentGroup.release_date)" Company = 'Star Wars' } | New-ADGroup -Path $Path -GroupCategory Security -GroupScope DomainLocal -OtherAttributes @{Info = "Star Wars `r`n$($CurrentGroup.opening_crawl -join '')"} -PassThru:$PassThru $CurrentGroup = $null } #Species 1..$(((Invoke-WebRequest -Uri $ApiUrl/species).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentGroup = (Invoke-WebRequest -Uri $ApiUrl/species/$_).Content | ConvertFrom-Json Write-Verbose -Message "Adding $($CurrentGroup.Name) to the group list..." New-Object -TypeName PSObject -Property @{ Name = $CurrentGroup.Name Description = "average_height : $($CurrentGroup.average_height), skin_colors : $($CurrentGroup.skin_colors), hair_colors : $($CurrentGroup.hair_colors), eye_colors : $($CurrentGroup.eye_colors), average_lifespan : $($CurrentGroup.average_lifespan)" Company = 'Star Wars' } | New-ADGroup -Path $Path -GroupCategory Security -GroupScope DomainLocal -OtherAttributes @{Info = "Star Wars `r`n$($CurrentGroup.language)"} -PassThru:$PassThru } } Function New-StarWarsADSite{ <# .SYNOPSIS Create Active Directory sites based on the Star Wars world. .DESCRIPTION Use Active Directory module and swapi.com to create sites in your Active Directory. .EXAMPLE New-StarWarsADSite Create all the sites. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> [CmdletBinding()] Param() $ApiUrl = 'http://swapi.dev/api' 1..$(((Invoke-WebRequest -Uri $ApiUrl/planets).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentPlanet = Invoke-WebRequest -Uri $ApiUrl/planets/$_ | Select-Object -ExpandProperty Content | ConvertFrom-Json try{ $Name = $CurrentPlanet.Name -replace ' ','' Write-Verbose -Message "Creating $Name site..." New-ADReplicationSite -Name $Name -Description "diameter : $($CurrentPlanet.diameter), climate : $($CurrentPlanet.climate), gravity : $($CurrentPlanet.gravity), terrain : $($CurrentPlanet.terrain), surface_water : $($CurrentPlanet.surface_water), population : $($CurrentPlanet.population), rotation_period : $($CurrentPlanet.rotation_period), orbital_period : $($CurrentPlanet.orbital_period)." -ErrorAction Stop -OtherAttributes @{Location = 'Star Wars'} } catch{ Write-Warning -Message "$Name : $_" } $CurrentPlanet = $null } } Function Add-StarWarsADUserToAdGroup{ <# .SYNOPSIS Add the Star Wars users to the groups which they belong to. .DESCRIPTION Use the Active Directory module and the info attributes. .EXAMPLE Add-StarWarsADUserToAdGroup Add the users to the groups. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> $StarWarsUsers = Get-ADUser -Filter {Company -like 'Star Wars'} -Properties info ForEach($User in $StarWarsUsers){ $User.Info -split ',' | ForEach-Object -Process { Add-ADGroupMember -Identity $_ -Members $User } } } #endregion #region Cleanup AD Function Remove-StarWarsADUser{ <# .SYNOPSIS Remove Star Wars sites. .DESCRIPTION Uses {Company -eq 'Star Wars'} as filter. .EXAMPLE Remove-StarWarsADUser Remove the users. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> Get-ADUser -Filter {Company -eq 'Star Wars'} | Remove-ADUser -Confirm:$false } Function Remove-StarWarsADSite{ <# .SYNOPSIS Remove Star Wars sites. .DESCRIPTION Uses {Location -eq 'Star Wars'} as filter. .EXAMPLE Remove-StarWarsADSite Remove the sites. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> Get-ADReplicationSite -Filter {Location -like 'Star Wars'} | Remove-ADReplicationSite -Confirm:$false } Function Remove-StarWarsADGroup{ <# .SYNOPSIS Remove Star Wars groups .DESCRIPTION Uses {Info -like '*Star Wars*'} as filter. .EXAMPLE Remove-StarWarsADGroup Remove the groups. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> Get-ADGroup -Filter {info -like '*Star Wars*'} | Remove-ADGroup -Confirm:$false } #endregion #region Standard objects Function Get-StarWarsUser{ <# .SYNOPSIS Get user account based on the Star Wars world. .DESCRIPTION Get a list of users based on Star Wars world. .PARAMETER AccountPassword Passwords for the accounts. Defaulted to 123+aze. .PARAMETER UPNSuffix UPN suffix for the accounts. .PARAMETER MailDomain Mail domain for the accounts. .EXAMPLE Get-StarWarsUser Get all Star Wars users and writes the objects on the pipeline. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> [CmdletBinding()] Param( [String]$UPNSuffix = 'itfordummies.net', [String]$MailDomain = 'itfordummies.net' ) $ApiUrl = 'http://swapi.dev/api' 1..$(((Invoke-WebRequest -Uri $ApiUrl/people).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentUser = (Invoke-WebRequest -Uri $ApiUrl/people/$_).Content | ConvertFrom-Json | Select-Object -Property Height,Mass,Hair_Color,Skin_Color,Eye_Color,Birth_Year,Gender, @{Label='Name';Expression={$_.Name -replace 'é','é'}}, @{Label='Homeworld';Expression={(Invoke-WebRequest -Uri $_.homeworld).Content | ConvertFrom-Json}}, @{Label='Films';Expression={($_.Films | ForEach-Object -Process {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, @{Label='species';Expression={($_.species | ForEach-Object -Process {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, #@{Label='vehicles';Expression={($_.vehicles | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, #@{Label='starships';Expression={($_.starships | % {Invoke-WebRequest -Uri $_}).Content | ConvertFrom-Json}}, @{Label='url';Expression={(Invoke-WebRequest -Uri $_.url).Content | ConvertFrom-Json}} $GivenName = ($CurrentUser.Name -split ' ')[0] $SurName = ($CurrentUser.Name -split ' ')[-1] if($GivenName -eq $SurName){ $SurName = $null $FullSamAccountName = $SamAccountName = $GivenName } else{ $SamAccountName = ("$(($CurrentUser.Name -split ' ')[0]).$(($CurrentUser.Name -split ' ')[-1])" -replace ' ','')[0..19] -join '' $FullSamAccountName = "$(($CurrentUser.Name -split ' ')[0]).$(($CurrentUser.Name -split ' ')[-1])" -replace ' ','' } Write-Verbose -Message "Creating $SamAccountName..." New-Object -TypeName PSObject -Property @{ Description = "$($CurrentUser.Gender), $($CurrentUser.Height)cm for $($CurrentUser.Mass)kg. $($CurrentUser.'Hair_Color') hair, $($CurrentUser.'Skin_Color') skin, $($CurrentUser.'Eye_Color') eyes. Born in $($CurrentUser.'Birth_Year')." Office = $CurrentUser.Homeworld.Name Name = $CurrentUser.Name DisplayName = $CurrentUser.Name GivenName = $GivenName Surname = $SurName SamAccountName = $FullSamAccountName EmailAddress = "$FullSamAccountName@$MailDomain" MailNickName = $FullSamAccountName UserPrincipalName = "$FullSamAccountName@$UPNSuffix" Company = 'Star Wars' Department = $CurrentUser.Homeworld.Name Title = $CurrentUser.species.Name -join ',' Info = ((($CurrentUser.Films.title -join ','),($CurrentUser.species.Name -join ',')) -join ',').TrimEnd(',') } $CurrentUser = $SurName = $GivenName = $SamAccountName = $FullSamAccountName = $null } } Function Get-StarWarsGroup{ <# .SYNOPSIS Get Star Wars groups based on the Star Wars world. .DESCRIPTION Get all group from SwApi.dev about Star Wars universe. .PARAMETER Path Path of the OU to create the groups. Defaulted to the default users creation destination. .EXAMPLE Get-StarWarsGroup Get all Star Wars groups in the specified OU. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> [CmdletBinding()] Param( ) $ApiUrl = 'http://swapi.dev/api' #Films 1..$(((Invoke-WebRequest -Uri $ApiUrl/films).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentGroup = (Invoke-WebRequest -Uri $ApiUrl/films/$_).Content | ConvertFrom-Json Write-Verbose -Message "Adding $($CurrentGroup.title) to the group list..." New-Object -TypeName PSObject -Property @{ Name = $CurrentGroup.title Description = "Produced by $($CurrentGroup.producer), diected by $($CurrentGroup.director) released on $($CurrentGroup.release_date)" Company = 'Star Wars' } $CurrentGroup = $null } #Species 1..$(((Invoke-WebRequest -Uri $ApiUrl/species).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentGroup = (Invoke-WebRequest -Uri $ApiUrl/species/$_).Content | ConvertFrom-Json Write-Verbose -Message "Adding $($CurrentGroup.Name) to the group list..." New-Object -TypeName PSObject -Property @{ Name = $CurrentGroup.Name Description = "average_height : $($CurrentGroup.average_height), skin_colors : $($CurrentGroup.skin_colors), hair_colors : $($CurrentGroup.hair_colors), eye_colors : $($CurrentGroup.eye_colors), average_lifespan : $($CurrentGroup.average_lifespan)" Company = 'Star Wars' } $CurrentGroup = $null } #Planets 1..$(((Invoke-WebRequest -Uri $ApiUrl/planets).Content | ConvertFrom-Json).count) | ForEach-Object -Process { $CurrentPlanet = Invoke-WebRequest -Uri $ApiUrl/planets/$_ | Select-Object -ExpandProperty Content | ConvertFrom-Json Write-Verbose -Message "Adding $($CurrentGroup.Name) to the group list..." New-Object -TypeName PSObject -Property @{ Name = $CurrentGroup.Name Description = "diameter : $($CurrentPlanet.diameter), climate : $($CurrentPlanet.climate), gravity : $($CurrentPlanet.gravity), terrain : $($CurrentPlanet.terrain), surface_water : $($CurrentPlanet.surface_water), population : $($CurrentPlanet.population), rotation_period : $($CurrentPlanet.rotation_period), orbital_period : $($CurrentPlanet.orbital_period)." Company = 'Star Wars' } $CurrentGroup = $null } } #endregion #region Misc Function Invoke-StarWarsTheme{ <# .SYNOPSIS PowerShell will sing the Star Wars theme. .DESCRIPTION Use [Console]::Beep to make some sound. .EXAMPLE Invoke-StarWarsTheme Start the song. .NOTES .LINK https://itfordummies.net .INPUTS .OUTPUTS #> #Part 1 [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(783, 1200) [Console]::Beep(1174, 1200) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(1046, 200) [Console]::Beep(880, 1200) #Part 1 [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(783, 1200) [Console]::Beep(1174, 1200) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(1046, 200) [Console]::Beep(880, 1200) #Part2 [Console]::Beep(523,400) [Console]::Beep(523,200) [Console]::Beep(659,900) [Console]::Beep(659,300) [Console]::Beep(1046,300) [Console]::Beep(987,300) [Console]::Beep(880,300) [Console]::Beep(783,300) [Console]::Beep(783,300) [Console]::Beep(880,150) [Console]::Beep(987,150) [Console]::Beep(880,300) [Console]::Beep(659,300) [Console]::Beep(733,600) [Console]::Beep(523,400) [Console]::Beep(523,200) [Console]::Beep(659,900) [Console]::Beep(659,300) [Console]::Beep(1046,300) [Console]::Beep(987,300) [Console]::Beep(880,300) [Console]::Beep(783,300) [Console]::Beep(1174,900) [Console]::Beep(880,300) [Console]::Beep(880,600) [Console]::Beep(523,400) [Console]::Beep(523,200) [Console]::Beep(659,900) [Console]::Beep(659,300) [Console]::Beep(1046,300) [Console]::Beep(987,300) [Console]::Beep(880,300) [Console]::Beep(783,300) [Console]::Beep(783,300) [Console]::Beep(880,150) [Console]::Beep(987,150) [Console]::Beep(880,300) [Console]::Beep(659,300) [Console]::Beep(733,600) [Console]::Beep(1174,400) [Console]::Beep(1174,200) [Console]::Beep(1567,400) [Console]::Beep(1396,200) [Console]::Beep(1244,400) [Console]::Beep(1174,200) [Console]::Beep(1046,400) [Console]::Beep(923,200) [Console]::Beep(880,400) [Console]::Beep(783,200) [Console]::Beep(1174,600) [Console]::Beep(880,200) [Console]::Beep(880,200) [Console]::Beep(880,200) [Console]::Beep(880,600) #Part 1 [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(783, 1200) [Console]::Beep(1174, 1200) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(1046, 200) [Console]::Beep(880, 1200) #Part 1 [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(523, 200) [Console]::Beep(783, 1200) [Console]::Beep(1174, 1200) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(880, 200) [Console]::Beep(1567, 1200) [Console]::Beep(1174, 600) [Console]::Beep(1046, 200) [Console]::Beep(987, 200) [Console]::Beep(1046, 200) [Console]::Beep(880, 1200) #Final [Console]::Beep(1174, 200) [Console]::Beep(1174, 200) [Console]::Beep(1174, 200) [Console]::Beep(1567, 1800) [Console]::Beep(783, 200) [Console]::Beep(783, 200) [Console]::Beep(783, 200) [Console]::Beep(783, 1800) } #endregion #region Export Module Member Export-ModuleMember -Function New-StarWarsADUser,New-StarWarsADGroup,Add-StarWarsADUserToAdGroup,New-StarWarsADSite,Invoke-StarWarsTheme,Remove-StarWarsADUser,Remove-StarWarsADSite,Remove-StarWarsADGroup,Get-StarWarsUser #endregion |