STtools.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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 |
Import-Module NTFSSecurity function Get-STGroupsFromCSV { <# .SYNOPSIS Gets/outputs group names and associated/unassociated users. Good for adding/removing year level or class groups for staff/students. .DESCRIPTION Gets user and group information from a CSV file that has at least two columns; one with usernames, the other with group names. The command will output to the pipeline: * A generated group name made up of the group field and your prefix/postfix/both * A list of Active Directory User objects (like you would get from Get-ADUser) who should be in the group based on their username * A list of Active Directory User objects (like you would get from Get-ADUser) who should NOT be in the group (all OTHER users from AD) .PARAMETER csvfile The source CSV file that contains a field for usernames and a field for groups .PARAMETER username_header The header of the field that contains usernames (Defaults to "Student Code") .PARAMETER group_header The header of the field that contains group (Defaults to "Roll Class Code") .PARAMETER group_name The group name to assign ALL users that result from this list. Use if there is no group_header that makes sense, but a filter does. .PARAMETER prefix A Prefix to prepend to the group name to generate the desired group name (Eg. "Year" to generate "Year7"). Can also be used with postfix. .PARAMETER postfix A Postfix to append to the group name to generate the desired group name (Eg. "Students" to generate "7Students"). Can also be used with prefix. .PARAMETER onlynumbers Only the numbers are kept in the group field. Useful when you're trying to create year groups from fields that have letters (Eg. "7B" -> "7") .PARAMETER stripzeros Strips leading zeros on for example a group year (Eg. "07" -> "7") .EXAMPLE Get-STGroupsFromCSV -csvfile \\path\to\csv\file.csv -prefix "Year" |ForEach-Object { Remove-ADGroupMember -Identity $_.Identity -Members $_.NonMembers -Confirm:$false Add-ADGroupMember -Identity $_.Identity -Members $_.Members -Confirm:$false } Get year groups and associated users from file.csv, for each "YearX" group, remove the users who don't appear in the CSV file, and add users who do .EXAMPLE Get-STGroupsFromCSV -csvfile \\path\to\csv\file.csv -postfix "Teachers" -username_header "Teacher Code" |ForEach-Object { Remove-ADGroupMember -Identity $_.Identity -Members $_.NonMembers -Confirm:$false Add-ADGroupMember -Identity $_.Identity -Members $_.Members -Confirm:$false } Get year groups and associated users from file.csv, for each "XTeachers" group, remove the users who aren't in the CSV file, add users who are in the CSV file .EXAMPLE Get-STGroupsFromCSV -csvfile \\CASES\share\ST_XXXX.csv #> Param( [Parameter(Mandatory=$true)] [string] $csvfile, [Parameter(Mandatory=$false,HelpMessage="The CSV table header for student code (default is 'Student Code' for TTv7)")] [string] $username_header = "Student Code", [Parameter(Mandatory=$false,HelpMessage="The CSV table header for year level (default is 'Roll Class Code' for TTv7)")] [string] $group_header, [Parameter(Mandatory=$false,HelpMessage="Prepends this to the beggining of each year level (eg. Year -> Year7)")] [string] $group_name, [Parameter(Mandatory=$false,HelpMessage="Prepends this to the beggining of each year level (eg. Year -> Year7)")] [string] $prefix = "", [Parameter(Mandatory=$false,HelpMessage="Appends this to the beggining of each year level (eg. students -> 7students)")] [string] $postfix = "", [Parameter(Mandatory=$false,HelpMessage="Strips leading zeros on group year")] [switch] $stripzeros, [Parameter(Mandatory=$false,HelpMessage="Selects only numbers from the group field (eg. '7B' -> '7')")] [switch] $onlynumbers, [Parameter(Mandatory=$false,HelpMessage='Filters records in CSV according to this filter (eg. -filter {$_.STATUS -eq "ACTV"')] [scriptblock] $filter ) Begin{ if ($group_name -and $($group_header -or $prefix -or $postfix -or $stripzeros -or $onlynumbers)) { Throw "Group Name must be used on its own!" } elseif ($group_name) { $groups = @{} if ($filter) { Import-Csv $csvfile |Where-Object $filter |ForEach-Object {$groups.$($group_name) += @($($_.$username_header))} } else { Import-Csv $csvfile |ForEach-Object {$groups.$($group_name) += @($($_.$username_header))} } } else { $groups = @{} if ($filter) { if ($onlynumbers) { if ($stripzeros) { Import-Csv $csvfile |Where-Object $filter |ForEach-Object {$groups.$($($_.$group_header -replace '\D+').trimstart('0')) += @($($_.$username_header))} } else { Import-Csv $csvfile |Where-Object $filter |ForEach-Object {$groups.$($_.$group_header -replace '\D+') += @($($_.$username_header))} } } else { if ($stripzeros) { Import-Csv $csvfile |Where-Object $filter |ForEach-Object {$groups.$($($_.$group_header).trimstart('0')) += @($($_.$username_header))} } else { Import-Csv $csvfile |Where-Object $filter |ForEach-Object {$groups.$($_.$group_header) += @($($_.$username_header))} } } } else { if ($onlynumbers) { if ($stripzeros) { Import-Csv $csvfile |ForEach-Object {$groups.$($($_.$group_header -replace '\D+').trimstart('0')) += @($($_.$username_header))} } else { Import-Csv $csvfile |ForEach-Object {$groups.$($_.$group_header -replace '\D+') += @($($_.$username_header))} } } else { if ($stripzeros) { Import-Csv $csvfile |ForEach-Object {$groups.$($($_.$group_header).trimstart('0')) += @($($_.$username_header))} } else { Import-Csv $csvfile |ForEach-Object {$groups.$($_.$group_header) += @($($_.$username_header))} } } } } } Process{ foreach ($group in $groups.Keys) { $unique_users = @() $adusers = @() $unique_users += $groups.$group |Select-Object -Unique foreach ($user in $unique_users) { try { $adusers += $(Get-ADUser -Identity $user) } catch { Write-Warning "Couldn't find AD User account for $user" } } $non_members = Get-ADUser -Filter * |Where-Object {$_.samaccountname -notin $unique_users} Write-Debug "Group: $group" Write-Debug "Members: $adusers" Write-Debug "Non-members: $non_members" $props = @{ 'Identity' = "$prefix$group$postfix"; 'Members' = $adusers; 'NonMembers' = $non_members } $obj = New-Object -TypeName PSObject -Property $props $obj.PSObject.TypeNames.Insert(0,"ST.GroupUsers") Write-Output $obj } } } function ConvertFrom-STEduHubSF { <# .SYNOPSIS Converts EduHUB Staff CSV file to objects suitable for consumption by New-ADUser and Set-ADUser .DESCRIPTION Gets staff information from a piped in EduHUB CSV file, and outputs objects that can be used by New-ADUser, Set-ADUser, Add-STUser, and Set-STUser The command will output objects to the pipeline with the following properties: * DisplayName * EmailAddress * Enabled * GivenName * HomeDirectory * HomeDrive * Name * PasswordNotRequired * SamAccountName * Surname * UserPrincipalName .PARAMETER UserList The source imported EduHUB CSV .PARAMETER HomeDirBase Path to a shared directory in which to create the user's home drive .PARAMETER Domain Domain name used to create EmailAddress and UPN properties .PARAMETER HomeDrive Drive letter for the user's home directory including the colon (eg. "H:", not "H") .PARAMETER Enabled True by default, but can be set to false to create accounts in a disabled state, or disable existing accounts .EXAMPLE Create new staff user accounts from EduHUB records that have an "ACTV" status Import-Csv -Path "\\path\to\eduhub\SF_0000.csv" | Where-Object {$_.STAFF_STATUS -eq "ACTV"} | ConvertFrom-STEduHubSF -HomeDirBase "\\my\network\share" -Domain "mydomain.com" -HomeDrive "H:" | New-ADUser .EXAMPLE Deactivate staff accounts that don't have an "ACTV" status Import-Csv -Path "\\path\to\eduhub\SF_0000.csv" | Where-Object {$_.STAFF_STATUS -ne "ACTV"} | ConvertFrom-STEduHubSF -HomeDirBase "\\my\network\share" -Domain "mydomain.com" -HomeDrive "H:" -Enabled:$false | Set-ADUser .EXAMPLE Update accounts that have an "ACTV" status to use a different home drive letter ("U:" instead of "H:") Import-Csv -Path "\\path\to\eduhub\SF_0000.csv" | Where-Object {$_.STAFF_STATUS -eq "ACTV"} | ConvertFrom-STEduHubSF -HomeDirBase "\\my\network\share" -Domain "mydomain.com" -HomeDrive "U:" | Set-ADUser #> [CmdletBinding()] param ( # list of users [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [System.Object[]] $UserList, # Directory (share) where user home dirs are created [Parameter(Mandatory=$true)] [string] $HomeDirBase, # Domain name (to generate email address) [Parameter(Mandatory=$true)] [string] $Domain, # Home drive letter (including colon) [Parameter(Mandatory=$true)] [string] $HomeDrive ) process { foreach ($user in $UserList) { $converteduser = $user | Select-Object -Property ` @{Name="DisplayName"; Expression={"$($_.FIRST_NAME) $($_.SURNAME)"}}, @{Name="EmailAddress"; Expression={"$($_.SFKEY)@$Domain"}}, @{Name="Enabled"; Expression={$($_.STAFF_STATUS -eq "ACTV")}}, @{Name="GivenName"; Expression={"$($_.FIRST_NAME)"}}, @{Name="HomeDirectory"; Expression={"$(Join-Path $HomeDirBase $_.SFKEY)"}}, @{Name="HomeDrive"; Expression={"$HomeDrive"}}, @{Name="Name"; Expression={"$($_.SFKEY)"}}, @{Name="PasswordNotRequired"; Expression={$false}}, @{Name="SamAccountName"; Expression={$_.SFKEY}}, @{Name="Surname"; Expression={$_.SURNAME}}, @{Name="UserPrincipalName"; Expression={"$($_.SFKEY)@$Domain"}} Write-Output $converteduser } } } function ConvertFrom-STEduHubST { <# .SYNOPSIS Converts EduHUB Student CSV file to objects suitable for consumption by New-ADUser and Set-ADUser .DESCRIPTION Gets student information from a piped in EduHUB CSV file, and outputs objects that can be used by New-ADUser, Set-ADUser, Add-STUser, and Set-STUser The command will output objects to the pipeline with the following properties: * DisplayName * EmailAddress * Enabled * GivenName * HomeDirectory * HomeDrive * Name * SamAccountName * Surname * UserPrincipalName .PARAMETER UserList The source imported EduHUB CSV .PARAMETER HomeDirBase Path to a shared directory in which to create the user's home drive .PARAMETER Domain Domain name used to create EmailAddress and UPN properties .PARAMETER HomeDrive Drive letter for the user's home directory including the colon (eg. "H:", not "H") .PARAMETER Enabled True by default, but can be set to false to create accounts in a disabled state, or disable existing accounts .EXAMPLE Create new student user accounts from EduHUB records that have an "ACTV" status Import-Csv -Path "\\path\to\eduhub\ST_0000.csv" | Where-Object {$_.STATUS -eq "ACTV"} | ConvertFrom-STEduHubST -HomeDirBase "\\my\network\share" -Domain "mydomain.com" -HomeDrive "H:" | New-ADUser .EXAMPLE Deactivate student accounts that don't have an "ACTV" status Import-Csv -Path "\\path\to\eduhub\ST_0000.csv" | Where-Object {$_.STATUS -ne "ACTV"} | ConvertFrom-STEduHubST -HomeDirBase "\\my\network\share" -Domain "mydomain.com" -HomeDrive "H:" -Enabled:$false | Set-ADUser .EXAMPLE Update accounts that have an "ACTV" status to use a different home drive letter ("U:" instead of "H:") Import-Csv -Path "\\path\to\eduhub\ST_0000.csv" | Where-Object {$_.STATUS -eq "ACTV"} | ConvertFrom-STEduHubST -HomeDirBase "\\my\network\share" -Domain "mydomain.com" -HomeDrive "U:" | Set-ADUser #> [CmdletBinding()] param ( # list of users [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [System.Object[]] $UserList, # Directory (share) where user home dirs are created [Parameter(Mandatory=$true)] [string] $HomeDirBase, # Domain name (to generate email address) [Parameter(Mandatory=$true)] [string] $Domain, # Home drive letter (including colon) [Parameter(Mandatory=$true)] [string] $HomeDrive ) process { foreach ($user in $UserList) { $converteduser = $user | Select-Object -Property ` @{Name="DisplayName"; Expression={"$($_.FIRST_NAME) $($_.SURNAME)"}}, @{Name="EmailAddress"; Expression={"$($_.STKEY)@$Domain"}}, @{Name="Enabled"; Expression={$($_.STATUS -eq "ACTV" -or $_.STATUS -eq "LVNG")}}, @{Name="GivenName"; Expression={"$($_.FIRST_NAME)"}}, @{Name="HomeDirectory"; Expression={"$(Join-Path $HomeDirBase $_.STKEY)"}}, @{Name="HomeDrive"; Expression={"$HomeDrive"}}, @{Name="HomeGroup"; Expression={"$($_.HOME_GROUP)"}}, @{Name="Name"; Expression={"$($_.STKEY)"}}, @{Name="SamAccountName"; Expression={$_.STKEY}}, @{Name="Surname"; Expression={$_.SURNAME}}, @{Name="UserPrincipalName"; Expression={"$($_.STKEY)@$Domain"}}, @{Name="YearLevel"; Expression={"$($_.SCHOOL_YEAR)"}} Write-Output $converteduser } } } function Update-Properties { <# .SYNOPSIS Updates an ADUser object's proerties from the properties of a compatible object .DESCRIPTION Takes Source and Destination objects, and updates matching properties on the destination object with values from the source object .PARAMETER SourceObject The source CSV file that contains a field for usernames and a field for groups .PARAMETER DestinationObject The header of the field that contains usernames (Defaults to "Student Code") .EXAMPLE Update a single user $updatedUser = Import-Csv "\\eduhub\ST_0000.csv" |where-object {$_.STKEY -eq "ABC0001"} | ConvertFrom-STEduHubST $targetUser = Get-ADUser "ABC0001" Update-Properties -sourceObject $updatedUser -destinationObject $targetUser #> [CmdletBinding()] param ( # Source object [Parameter(Mandatory=$true)] [System.Object] $sourceObject, # Destination object [Parameter(Mandatory=$true)] [System.Object] $destinationObject ) process { $updated = $false $properties = $sourceObject |Get-Member |Where-Object {$_.membertype -eq "NoteProperty"} $properties |ForEach-Object { if ($_.Name -ne "Name" -and $_.Name -ne "HomeGroup" -and $_.Name -ne "YearLevel") { if ($sourceObject.$($_.Name) -ne $destinationObject.$($_.Name)) { write-host "$($_.Name): $($sourceObject.$($_.Name)) and $($destinationObject.$($_.Name)) are different" $destinationObject.Item("$($_.Name)").Value = $sourceObject.$($_.Name) $updated = $true } } } if ($updated -eq $true) { Write-Output $destinationObject } } } function Get-STRandomCharacter($range, $count) { return $([string](-join ($range |Get-Random -count $count | ForEach-Object {[char]$_}))) } function Get-STNewCompliantPassword () { $randomChars = $($(Get-STRandomCharacter -range (48..57) -count 3) + $(Get-STRandomCharacter -range (33..47) + (58..64) -count 2) + $(Get-STRandomCharacter -range (65..90) -count 3) + $(Get-STRandomCharacter -range(97..122) -count 4)) -split "" |Sort-Object {Get-Random} return -join $randomChars } function Set-STUser ($users, $homeDirBase="", $allUsers) { <# .SYNOPSIS Wraps Set-ADUser adding home directory creation and permission setting .DESCRIPTION Set-ADUser on users that occur in -allUsers using updated properties from -users Then creates home directories (if applicable) and sets full control for the user on their own home directory .PARAMETER users List of ADUser compatible objects with new properties .PARAMETER DestinationObject List of ADUser objects to be updated from AD .PARAMETER homeDirBase Path to directory in which to create user home directories .EXAMPLE Update active users with new/changed properties in EduHUB CSV file # Be more specific if you have a large directory structure $allUsers = Get-ADUser -filter * -Properties * $activeStaffList = Import-Csv $staffcsv | Where-Object { ($_.STAFF_STATUS -eq "ACTV") } | ConvertFrom-STEduHubSF -HomeDirBase \\fs1\users\ -Domain mydomain.com -HomeDrive U: $staffHomeDirBase = "\\fs1\users\" Set-STUser -users $activeStaffList -HomeDirBase $staffHomeDirBase -allUsers $allUsers #> $users |ForEach-Object { foreach ($account in $allUsers) { if ($_.samaccountname -eq $account.samaccountname) { Write-Host "Checking: $($account.samaccountname)" Update-Properties -sourceObject $_ -destinationObject $account } } } |ForEach-Object {set-aduser -Instance $_} $users |New-STHomeDirectory } function New-STUser { <# .SYNOPSIS Wraps New-ADUser adding home directory creation and permission setting .DESCRIPTION New-ADUser on users that occur in -sourceUsers Then creates home directories (if applicable) and sets full control for the user on their own home directory .PARAMETER sourceUsers List of ADUser compatible objects .PARAMETER currentUsers List of all ADUser objects (so this cmdlet knows which users already exist) .PARAMETER OUPath Hash of OU => list of users (users are created in the OU specified in this hash) .PARAMETER homeDirBase String referring to the directory in which this script should create user home directories .PARAMETER smtp_server SMTP server for sending user creation reports .PARAMETER support_address From address for user creation reports .PARAMETER to_addresses One or more email addresses to send new user creation reports to .EXAMPLE Create new accounts in the default OU and email support@mydomain.com with new user account details $activeStaffList = Import-Csv $staffcsv | Where-Object { ($_.STAFF_STATUS -eq "ACTV") } | ConvertFrom-STEduHubSF -HomeDirBase \\fs1\users\ -Domain mydomain.com -HomeDrive U: $allUsers = Get-ADUser -filter * New-STUser -sourceUsers $activeStaffList -currentUsers $allUsers -HomeDirBase "\\fs1\users\" -smtp_server "exch01" -support_address "support@mydomain.com" -to_addresses "support@mydomain.com", "boss@mydomain.com" #> [CmdletBinding(SupportsShouldProcess=$True)] param( # List of source user objects [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [System.Object[]] $sourceUsers, # List of current AD users [Parameter(Mandatory=$false)] [System.Object[]] $currentUsers, # Function for generating new password for user [Parameter(Mandatory=$false)] [scriptblock] $passwordFunction ) begin { if (-not $currentUsers) { $currentUsers = get-aduser -Filter * -Properties * } } process{ $sourceUsers |ForEach-Object { if ($passwordFunction) { $plainpassword = Invoke-Command $passwordFunction } else { $plainpassword = Get-STNewCompliantPassword } $_ |add-member -MemberType NoteProperty -name "AccountPassword" -value $(convertto-securestring $plainpassword -AsPlainText -Force) -Force $_ |add-member -MemberType NoteProperty -name "PlainPassword" -value $plainpassword -Force Write-Output $_ } | foreach-object { if ($PSCmdlet.ShouldProcess("New User Created: $($_.samaccountname)")) { try { $_ |New-ADUser -ErrorAction Stop if ($_.homeDirectory -and $_.HomeDrive) { $_ |New-STHomeDirectory } } catch { # Suppresses pipeline output if the above fails $null = $_ } } } } } function New-STHomeDirectory { param ( # User Object(s) [Parameter(Mandatory=$true,ValueFromPipeline)] [object[]] $User ) process { $_ |ForEach-Object { if ( $_.Enabled ` -and $_.HomeDirectory ` -and $_.HomeDrive ` -and $(Test-Path -PathType Container $(split-path -parent $_.HomeDirectory)) ` ) { if (-not $(Test-Path -PathType Container $_.HomeDirectory)) { New-Item -ItemType Directory -Path $_.homeDirectory -ErrorAction Continue |Out-Null Start-Sleep -Seconds 3 } $sam = $_.samaccountname if (-not $(Get-NTFSAccess -Path $_.homeDirectory |Where-Object {$_.Account -contains $sam})) { Add-NTFSAccess -Path $_.homeDirectory -Account $_.samaccountname -AccessRights FullControl |Out-Null } } } } } function NormaliseBirthDate ($date) { if ($date -eq "") { $date = "0/00/0000" } if ($date.Length -lt 22) { $date = "0" + $date } $date -match "(?<day>[0-9]+)/(?<month>[0-9]+)/(?<year>[0-9]+)" |Out-Null $date = $Matches["day"] + $Matches["month"] + $Matches["year"].Substring(2, 2) return $date } function Set-STOrganizationalUnit ($userOUs) { foreach ($OU in $userOUs.Keys) { $targetOU = "" try { $targetOU = Get-ADOrganizationalUnit $OU -ErrorAction Stop } catch { Write-host "$OU OU doesn't exist" } if ($targetOU -ne "") { $currentOUUsers = get-aduser -SearchBase $targetOU -SearchScope 1 -Filter * foreach ($user in $userOUs.$OU) { if ($user -notin $currentOUUsers.samaccountname) { $usertomove = Get-ADUser $user write-host "Moving $user to $targetOU" Move-ADObject -Identity $usertomove.ObjectGUID -TargetPath $targetOU } } } } } function Update-STManagedGroups { <# .SYNOPSIS Creates Universal security groups based on a group name and a target OU .DESCRIPTION Takes a group name and a target OU, and tries to create a universal security group in that OU if it doesn't already exist. This command is idempotent, so if the group name from the list already exists and is a Universal group, no changes will be made for that group. If the group does not exist yet, it will be created and made Universal. .PARAMETER newGroup A string with the name of the group .PARAMETER GroupOU A string path to the OU that the new group will be created in (if created) .EXAMPLE Given the following data in a CSV file "blah.csv": # | GroupName | Some other attribute | # +===========+======================+ # | MAT101 | blah blah | # +-----------+----------------------+ $currentGroups = get-adgroup -Filter * Import-Csv .\blah.csv |ForEach-Object {Update-STManagedGroups -newGroup $_.GroupName -GroupOU 'OU=ClassGroups,DC=test-domain,DC=local' -currentGroups $currentGroups} .EXAMPLE Get-STGroupsFromCSV -csvfile \\CASES\share\ST_XXXX.csv #> [CmdletBinding()] param ( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [String] $newGroup, [Parameter(Mandatory=$true)] [String] $GroupOU, # Parameter help description [Parameter(Mandatory=$false)] [object[]] $currentGroups ) begin { if (-not $currentGroups) { $currentGroups = Get-ADGroup -Filter * } } process { if ($newGroup -and $newGroup -notin $currentGroups.name) { try { $adgroup = New-ADGroup -Name $newGroup -GroupScope Universal -Path $GroupOU -ErrorAction Stop -PassThru write-host "Created group: $newGroup in OU $GroupOU" } catch { Write-Warning "Couldn't create group $newGroup. Error was:`n$($_.Exception.Message)" } } else { $adgroup = $false $adgroup = $($currentGroups | Where-Object Name -eq $newGroup) if (-not $adgroup) { try { Get-ADGroup $newGroup -ErrorAction Stop } catch { Write-Warning "Couldn't get group $newGroup. Error was:`n$($_.Exception.Message)" } } } if($adgroup -and $adgroup.GroupScope -ne "Universal") { try { $adgroup | Set-ADGroup -GroupScope Universal Write-Host "Changed GroupScope for $newGroup to Universal" } catch { Write-Warning "Couldn't change GroupScope for $newGroup to Universal" } } if ($adgroup -and (-not $adgroup.distinguishedname -match "CN=$($adgroup.CN),$GroupOU")) { try { Move-ADObject -Identity $adgroup.ObjectGUID -TargetPath $GroupOU Write-Host "Moved $($adgroup.CN) to $GroupOU" } catch { Write-Warning "Couldn't move $($adgroup.CN) to $GroupOU" } } } } function Get-STManualActiveUsernames { try { $(Import-Csv "ManualActiveUserNames.csv").KEY } catch { Write-Warning "No manual active users file found" New-item "ManualActiveUserNames.csv" } } function Get-STFutureUsernames { try { $(Import-Csv "FutureUserNames.csv").KEY } catch { Write-Warning "No future users file found" New-item "FutureUserNames.csv" } } function Select-STFutureStudents { [CmdletBinding()] Param ( [Parameter(Mandatory=$true,ValueFromPipeline=$true)] [object[]] $casesUsers, [Parameter(Mandatory=$false)] [object[]] $ADUsers, [Parameter(Mandatory=$true)] [string] $FutureStudentFile ) begin { $ExistingSTFutureStudents = Get-STFutureStudents -FutureStudentFile $FutureStudentFile if (-not $ADUsers) { Write-Warning "No AD Users passed in, grabbing manually..." Write-Warning "If you are running this command from a script," Write-Warning "it's better to do:" Write-Warning '$ADUsers = Get-ADUser -Filter *' Write-Warning 'and call this function with -ADUsers $ADUsers' $ADUsers = Get-ADUser -Filter * } } process { $_ |Where-Object { $_.HomeGroup -eq "ZZZ" ` -and $_.Enabled -eq $false ` -and $_.SamAccountName -notin $ADUsers.SamAccountName ` -and $_.SamAccountName -notin $($ExistingSTFutureStudents).SamAccountName ` } } } function Update-STFutureStudents { [CmdletBinding()] Param ( [Parameter(Mandatory=$true,ValueFromPipeline)] [object[]] $casesUsers, [Parameter(Mandatory=$true)] [string] $FutureStudentFile ) process { $_ | Export-Csv -NoTypeInformation -Append -Path $FutureStudentFile } } function Get-STFutureStudents { param ( [Parameter(Mandatory=$true)] [string] $FutureStudentFile ) try { Import-Csv $FutureStudentFile } catch { Write-Warning "No future students file found" New-Item $FutureStudentFile } } function Select-STAccountsToDisable { [CmdletBinding()] param ( # Source Accounts [Parameter(Mandatory,ValueFromPipeline)] [object[]] $sourceUsers, # Current AD accounts [Parameter(Mandatory=$false)] [object[]] $ADUsers, # List of accounts to ignore [Parameter(Mandatory=$false)] [string[]] $ignoreList, # List of accounts to always output [Parameter(Mandatory=$false)] [string[]] $forceInactiveList ) begin { $inactiveADUsers = $ADUsers |Where-Object {$_.Enabled -eq $false} } process { $_ | Where-Object { $_.Enabled -eq $false ` -and $_.SamAccountName -notin $inactiveADUsers.SamAccountName ` -and $_.SamAccountName -in $ADUsers.SamAccountName ` -and $_.SamAccountName -notin $ignoreList ` -or $_.SamAccountName -in $forceInactiveList ` } |foreach-object { if ($_.Enabled) { $_.Enabled = $false } Write-Output $_ } } } function Select-STAccountsToCreate { [CmdletBinding()] Param ( # source users [Parameter(Mandatory=$true,ValueFromPipeline)] [object[]] $sourceUsers, # current users [Parameter(Mandatory=$false)] [object[]] $ADUsers, # Users who should never occur in output [Parameter(Mandatory=$false)] [string[]] $IgnoreList, # Users who should occur in output if they don't have active accounts [Parameter(Mandatory=$false)] [string[]] $forceActiveList ) begin { if (-not $ADUsers) { Write-Warning 'If you call this function multiple times, you should do something like:' Write-Warning '$allusers = get-aduser -filter * -properties *' Write-Warning 'Select-STAccountsToCreate -sourceUsers $sourceUsers -ADUsers $allusers' $ADUsers = Get-ADUser -Filter * } $activeADUsernames = $ADUsers.SamAccountName $futureUsernames = Get-STFutureUsernames $manualActiveUsernames = Get-STManualActiveUsernames } process { $_ |Where-Object { ($_.Enabled -eq $true -or $_.SamAccountName -in $ManualActiveUserNames) ` -and $_.SamAccountName -notin $activeADUsernames ` -and $_.SamAccountName -notin $IgnoreList ` -and $_.SamAccountName -notin $futureUsernames ` } } } Function New-STWelcomeLetter { <# .SYNOPSIS Generates an ICT Welcome Letter for McKinnon SC based on a template .PARAMETER User [object[]] List of user objects to create letters for .OUTPUTS Formatted HTML welcome letter string .NOTES Version: 9001 Author: Eric van de Paverd... actually... he stole it from Sam Neal Creation Date: 30/10/2018 .EXAMPLE New-ADUser <parameters> | Generate-WelcomeLetter | Out-File -path "Welcome.html" #> [CmdletBinding()] Param( [Parameter(Mandatory=$true,ValueFromPipeline)] [object[]]$User, [Parameter(Mandatory=$true)] [string] $TemplatePath ) begin { $HTML = Get-Content -Path $TemplatePath -Raw } process { $_ |ForEach-Object { $HTML -f $_.GivenName, $_.Surname, $_.SamAccountName, $_.PlainPassword, "$($_.SamAccountName)@brunswick.vic.edu.au" } } } |