Advertisement
Workspace-Guru

ADGroupstoFilters2

Mar 18th, 2018
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#  
  2.     .NOTES
  3.     ===========================================================================
  4.      Created on:    18-3-2018
  5.      Created by:    Chris Twiest
  6.      Organization:  Workspace-Guru.com      
  7.     ===========================================================================
  8.     .DESCRIPTION
  9.     This script will read all Groups below a Base OU, then add these groups to a CSV file.
  10.     This CSV is then imported to your LiquidWare ProfileUnity Server.
  11.     It will then create a Filter object for every group in the CSV.
  12. #>
  13.  
  14.  
  15. $BaseOU = "OU=Applications,dc=COMPANY,dc=COM"
  16. $WorkingDirectory = "C:\Temp"
  17.  
  18. import-module ActiveDirectory
  19.  
  20. $ChildGroups = Get-ADGroup -Filter * -SearchBase $baseOU
  21.                            
  22. $TestWorkingDirectory = $WorkingDirectory | Test-Path
  23.                            
  24. if ($TestWorkingDirectory -eq $False)
  25. {
  26.     New-Item -ItemType Directory -Path $WorkingDirectory
  27. }
  28.  
  29. $TestWorkingDirectoryFilter = "$WorkingDirectory\Filters" | test-path
  30.  
  31. if ($TestWorkingDirectoryFilter -eq $False)
  32. {
  33.     New-Item -ItemType Directory -Path "$WorkingDirectory\Filters"
  34. }
  35.  
  36. $CSVFile = "$WorkingDirectory\Filters\Groupfilters.csv"
  37.  
  38. $TestCSVFile = $CSVFile | Test-Path
  39.                            
  40. if ($TestCSVFile -eq $True)
  41. {
  42.     Remove-Item -Path $CSVFile -Force
  43. }
  44.  
  45. foreach ($ChildGroup in $ChildGroups) {
  46.  
  47. $ChildGroupName = $ChildGroup.Name
  48. $filtername = "Group="+"$ChildGroupName"
  49. $ChildGroupSam = $ChildGroup.SamAccountName
  50.  
  51.     #DATA CSV
  52.     $csvtable = New-Object PSobject
  53.     $csvtable | Add-Member -Type NoteProperty -Name Name -Value $filtername
  54.     $csvtable | Add-Member -Type NoteProperty -Name Comments -Value "Added With PowerShell"
  55.     $csvtable | Add-Member -Type NoteProperty -Name RuleAggregate -Value 0
  56.     $csvtable | Add-Member -Type NoteProperty -Name FilterConditionType -Value 0
  57.     $csvtable | Add-Member -Type NoteProperty -Name FilterMatchType -Value 0
  58.     $csvtable | Add-Member -Type NoteProperty -Name Filtervalue -Value $ChildGroupSam
  59.     $csvtable | Add-Member -Type NoteProperty -Name MachineClasses -Value 62
  60.     $csvtable | Add-Member -Type NoteProperty -Name OperatingSystems -Value 4094
  61.     $csvtable | Add-Member -Type NoteProperty -Name SystemEvents -Value 3
  62.     $csvtable | Add-Member -Type NoteProperty -Name Connections -Value 60
  63.  
  64.     $CSVtable | Export-Csv -Append -NoTypeInformation -Path $CSVFile
  65.  
  66. }
  67.  
  68. function connect-ProfileUnityServer{
  69. ##Ignore-SSL Library Code
  70. add-type @"
  71.    using System.Net;
  72.    using System.Security.Cryptography.X509Certificates;
  73.    public class TrustAllCertsPolicy : ICertificatePolicy {
  74.        public bool CheckValidationResult(
  75.            ServicePoint srvPoint, X509Certificate certificate,
  76.            WebRequest request, int certificateProblem) {
  77.            return true;
  78.        }
  79.    }
  80. "@
  81. [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
  82.  
  83. ##Get Creds
  84. [string]$global:servername= Read-Host -Prompt 'FQDN of ProfileUnity Server Name'
  85. $user = Read-Host "Enter Username"
  86. $pass = Read-Host -assecurestring "Enter Password"
  87. $pass2=[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))
  88.  
  89. #Connect to Server
  90. Invoke-WebRequest https://"$servername":8000/authenticate -Body "username=$user&password=$pass2" -Method Post -SessionVariable session
  91. $global:session=$session
  92. }
  93.  
  94. Connect-ProfileUnityServer
  95.  
  96. $lists=import-csv $CSVFile
  97.  
  98. Foreach ($item in $lists){
  99. #FilterRules Hashing
  100. $FilterRules=@{ConditionType=$item.FilterConditionType; MatchType=$item.FilterMatchType; Value=$item.Filtervalue}
  101. $FilterRules=@($FilterRules)
  102.  
  103. ##Make New FilterSettings
  104. $newFilter=[pscustomobject]@{
  105. Name=$item.Name;
  106. Comments=$item.Comments;
  107. RuleAggregate=$item.RuleAggregate;
  108. FilterRules=$FilterRules;
  109. MachineClasses=$item.MachineClasses;
  110. OperatingSystems=$item.OperatingSystems;
  111. SystemEvents=$item.SystemEvents;
  112. Connections=$item.Connections;
  113. }
  114. Invoke-WebRequest https://"$servername":8000/api/filter -ContentType "application/json" -Method Post -WebSession $session -Body($NewFilter | ConvertTo-Json -Depth 10)
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement