Guest User

RedditPoSHFix

a guest
Apr 12th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function New-RemoteConferenceRoom {
  2.     [CmdletBinding()]
  3.     param (
  4.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  5.         [ValidateNotNullOrEmpty()]
  6.         [string] $name,
  7.         [parameter(Mandatory=$false,ValueFromPipelineByPropertyName =$true)]
  8.         [ValidateNotNullOrEmpty()]
  9.         [ValidateSet("Yes", "No","Y","N")]
  10.         [string] $VC = "No",
  11.         [parameter(Mandatory=$false,ValueFromPipelineByPropertyName =$true)]
  12.         [ValidateNotNullOrEmpty()]
  13.         [ValidateSet("Yes", "No","Y","N")]
  14.         [string] $LCD = "No",
  15.         [parameter(Mandatory=$false,ValueFromPipelineByPropertyName =$true)]
  16.         [ValidateNotNullOrEmpty()]
  17.         [ValidateSet("Yes", "No","Y","N")]
  18.         [string] $phone = "Yes",
  19.         [parameter(Mandatory=$false,ValueFromPipelineByPropertyName =$true)]
  20.         [ValidateNotNullOrEmpty()]
  21.         [ValidateSet("Yes", "No","Y","N")]
  22.         [string] $whiteboard = "Yes",
  23.         [parameter(Mandatory=$false,ValueFromPipelineByPropertyName =$true)]
  24.         [ValidateNotNullOrEmpty()]
  25.         [ValidateSet("Yes", "No","Y","N")]
  26.         [string] $projector = "No",
  27.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  28.         [ValidateNotNullOrEmpty()]
  29.         [string] $location,
  30.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  31.         [ValidateNotNullOrEmpty()]
  32.         [Alias("Building ID")]
  33.         [string] $buildingID,
  34.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  35.         [ValidateNotNullOrEmpty()]
  36.         [Alias("Seat Capacity")]
  37.         [int] $seatCapacity,
  38.         [parameter(Mandatory=$false,ValueFromPipelineByPropertyName =$true)]
  39.         [Alias("Phone Number")]
  40.         [string] $phoneNumber,
  41.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  42.         [ValidateNotNullOrEmpty()]
  43.         [Alias("Zip Code")]
  44.         [string] $zipcode,
  45.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  46.         [ValidateNotNullOrEmpty()]
  47.         [string] $city,
  48.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  49.         [ValidateNotNullOrEmpty()]
  50.         [string] $address,
  51.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  52.         [ValidateNotNullOrEmpty()]
  53.         [string] $state,
  54.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  55.         [ValidateNotNullOrEmpty()]
  56.         [string] $country,
  57.         [parameter(Mandatory=$true,ValueFromPipelineByPropertyName =$true)]
  58.         [ValidateNotNullOrEmpty()]
  59.         [ValidateSet("Reservable", "Non-reservable")]
  60.         [string] $status,
  61.         [parameter(Mandatory=$false,ValueFromPipelineByPropertyName =$true)]
  62.         [ValidateNotNullOrEmpty()]
  63.         [bool] $HideFromAddressBook= $false
  64.     )
  65.  
  66.     Try {
  67.         #Room/Videoconference, Room/LCD, Room/Phone, Room/Whiteboard,
  68.         #Room/Projector
  69.         #Construct a string that contains the things in the room.
  70.         $roomResources = ""
  71.         $exRoomResources = @()
  72.         if ($WhiteBoard -like "Y*") {
  73.             $roomResources += "WB"
  74.             $exRoomResources += "Whiteboard"
  75.         }
  76.         if ($Phone -like "Y*") {
  77.             $roomResources += "/PH"
  78.             $exRoomResources += "Phone"
  79.         }
  80.         if ($VC -like "Y*") {
  81.             $roomResources += "/VC"
  82.             $exRoomResources += "VideoConference"
  83.         }
  84.         if ($LCD -like "Y*") {
  85.             $roomResources += "/LCD"
  86.             $exRoomResources += "LCD"
  87.         }
  88.         if ($Projector -like "Y*" ) {
  89.             $roomResources += "/Projector"
  90.             $exRoomResources += "Projector"
  91.         }
  92.    
  93.         #Let's target a reachable DC in DC1-EXC for entire transaction
  94.         $dc = Get-ADDomainController -ForceDiscover -Discover -Site 'DC1-EXC' |
  95.         select -expand hostname
  96.         if (-not($dc)) {
  97.             $dc = Get-ADDomainController -ForceDiscover -Discover -Site 'DC2-EXC' |
  98.             select -expand hostname
  99.         }
  100.         [Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
  101.         $password = [System.Web.Security.Membership]::GeneratePassword(20,2) |
  102.         ConvertTo-SecureString -AsPlainText -Force
  103.  
  104.         $mylocation = $location.split(".")[0]
  105.         $DisplayName = "[" + $BuildingID + "." + $myLocation + "] " `
  106.         + $Name + " (" + $status + " Seats " `
  107.         + $seatCapacity + " $roomResources)"
  108.    
  109.         #derive the samaccount name (and the email address)
  110.         #based on the location and building ID.
  111.         $samaccountname  = (($buildingid + $location).replace(".","")).trim()
  112.         $upn = "$($samaccountname)@mydomain.com"
  113.  
  114.         $groupOU = "mydomain.com/mydomain dl's/conferenceroomcontrols"
  115.         #create 2 groups. One group will be the bookinpolicy members.
  116.  
  117.         $Settings = @{
  118.             'organizationalunit' = $groupOU;
  119.             'DomainController' = $dc;
  120.         }
  121.  
  122.         $bookName = $samaccountname + "Booking"
  123.         if (!(Get-DistributionGroup $bookName -ea 'silentlycontinue')) {
  124.             $Settings.Name = "$samaccountname Booking";
  125.             $Settings.SamAccountName = $samaccountname + "Booking";
  126.      
  127.             New-DistributionGroup @Settings
  128.         }
  129.    
  130.         #the other group will be the set of users with advanced viewing rights.
  131.         $viewName = $samaccountname + "View"
  132.         if (!(Get-DistributionGroup $viewName -ea 'silentlycontinue')) {
  133.             $Settings.Name = "$samaccountname Viewing";
  134.             $Settings.SamAccountName = $samaccountname + "View";
  135.      
  136.             New-DistributionGroup @Settings
  137.         }
  138.  
  139.         $roomOU = "mydomain.com/conference rooms"
  140.         $roomSplat = @{
  141.             'OnPremisesOrganizationalUnit' = $roomOU;
  142.             'Identity' = $samaccountname;
  143.             'DomainController' = $dc;
  144.             'ErrorAction' = 'silentlycontinue'
  145.         }
  146.    
  147.         if (!(Get-RemoteMailbox @roomSplat)) {
  148.             $myRoomSettings = @{
  149.                 'Alias' = $samAccountName.trim();
  150.                 'DisplayName' = $DisplayName;
  151.                 'OnPremisesOrganizationalUnit' = $roomOU;
  152.                 'DomainController' = $dc;
  153.                 'Password' = $password;
  154.                 'Name' = $name.trim();
  155.                 'FirstName' = $name.trim();
  156.                 'samaccountname' = $samaccountname.trim();
  157.                 'UserPrincipalName' = $upn;
  158.                 'RemoteRoutingAddress' = "$($samAccountName.trim())@mydomain.mail.onmicrosoft.com";
  159.                 'ErrorAction' = "Stop";
  160.             }
  161.             New-RemoteMailbox @myRoomSettings
  162.         }
  163.         else {
  164.             Write-Output "$DisplayName already exists"
  165.         }
  166.  
  167.         $ToReplace = @{
  168.             'extensionattribute4' = "$exRoomResources";
  169.             'houseidentifier' = "$BuildingID"
  170.         }
  171.  
  172.         $myRoomSetUser = @{
  173.             'Identity' = $samAccountName.trim();
  174.             'employeeNumber' = "999999999999"
  175.             'Office' = $Location;
  176.             'Country' = $Country;
  177.             'City' = $city;
  178.             'PostalCode' = $ZipCode;
  179.             'StreetAddress' = $Address;
  180.             'State' = $State;
  181.             'OfficePhone' = $phoneNumber;
  182.             'Server' = $dc;
  183.             'ErrorAction' = 'Stop'
  184.             'Replace' = $ToReplace
  185.         }
  186.         Set-ADUser @myRoomSetUser
  187.     }
  188.     Catch { $_ }
  189. }
Add Comment
Please, Sign In to add comment