Guest User

Untitled

a guest
Aug 26th, 2019
300
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ErrorActionPreference = 'SilentlyContinue'
  2. #Your XAML goes here :)
  3. $inputXML = @"
  4. <Window
  5. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  6. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  7. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  8. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  9. xmlns:local="clr-namespace:AD_Extractor"
  10. Title="AD Extractor" Height="431" Width="343" ResizeMode="NoResize" WindowStartupLocation="Manual">
  11. <Grid>
  12. <GroupBox x:Name="groupBox" Header="Enter the group you want to extract members from:" HorizontalAlignment="Left" Height="82" Margin="10,10,0,0" VerticalAlignment="Top" Width="315">
  13. <TextBox x:Name="groupname" HorizontalAlignment="Left" Height="23" Margin="10,17,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="280"/>
  14. </GroupBox>
  15. <GroupBox x:Name="groupBox1" Header="Members:" HorizontalAlignment="Left" Height="248" Margin="10,97,0,0" VerticalAlignment="Top" Width="315">
  16. <ListBox x:Name="output" HorizontalAlignment="Left" Height="208" Margin="10,10,0,0" VerticalAlignment="Top" Width="285"/>
  17. </GroupBox>
  18. <Button x:Name="extract" Content="Extract members" HorizontalAlignment="Left" Margin="10,353,0,0" VerticalAlignment="Top" Width="120"/>
  19. <Button x:Name="export" Content="Export to file" HorizontalAlignment="Left" Margin="205,353,0,0" VerticalAlignment="Top" Width="120" IsEnabled="False"/>
  20. <Label x:Name="label" Content="Robert Paegelow - 2019" HorizontalAlignment="Left" Margin="0,378,0,-1" VerticalAlignment="Top" FontStyle="Italic" Background="White" Foreground="#FFB8B8B8" FontSize="11"/>
  21.  
  22. </Grid>
  23. </Window>
  24.  
  25.  
  26. "@
  27.  
  28. $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
  29. [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  30. [xml]$XAML = $inputXML
  31. #Read XAML
  32.  
  33. $reader=(New-Object System.Xml.XmlNodeReader $xaml)
  34. try{
  35. $Form=[Windows.Markup.XamlReader]::Load( $reader )
  36. }
  37. catch{
  38. Write-Warning "Unable to parse XML, with error: $($Error[0])`n Ensure that there are NO SelectionChanged or TextChanged properties in your textboxes (PowerShell cannot process them)"
  39. throw
  40. }
  41.  
  42. #===========================================================================
  43. # Load XAML Objects In PowerShell
  44. #===========================================================================
  45.  
  46. $xaml.SelectNodes("//*[@Name]") | %{;
  47. try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
  48. catch{throw}
  49. }
  50.  
  51. Function Get-FormVariables{
  52. if ($global:ReadmeDisplay -ne $true){
  53. #Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true
  54. }
  55. #write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
  56. #get-variable WPF*
  57. }
  58.  
  59. #Get-FormVariables
  60.  
  61. #===========================================================================
  62. # Use this space to add code to the various form elements in your GUI
  63. #===========================================================================
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. #############
  75. ## KNAPPAR ##
  76. #############
  77.  
  78. $WPFextract.Add_Click({extract})
  79. $WPFexport.Add_Click({export})
  80.  
  81. ##########################
  82. ## Mostly GUI-functions ##
  83. ##########################
  84.  
  85. function extract {
  86. $WPFoutput.Items.Clear()
  87. ######################check if object is group or not #############################
  88. $Group = $wpfgroupname.text
  89. function checkgroup ($Group1)
  90. {
  91.  
  92. $Search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
  93. $Search.filter = "(&(objectCategory=group)(objectClass=group)(cn=$Group1))"
  94. $input=$Search.Findall()
  95.  
  96. if($input -ne $null)
  97. {
  98. ##Write-Host "$Group1 is a valid"
  99. return $true
  100. }
  101. else
  102. {
  103. ##Write-Host "$Group1 is a invalid"
  104. return $false
  105. }
  106. }
  107. ##################################Recurse thru groups ##############################
  108.  
  109. function getallmembersrecursively ($Group)
  110. {
  111. $Search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
  112. $Search.filter = "(&(objectCategory=group)(objectClass=group)(cn=$Group))"
  113. $input=$Search.Findall()
  114.  
  115. if($input -ne $null)
  116. {
  117. Foreach($group in $input){
  118. $groupname = $group.GetDirectoryEntry()
  119. $GPName = $groupname.DistinguishedName
  120. $GPMember = $groupname.member
  121. $GPName1 = [string]$GPName
  122. $gsplit1 = $GPName1.split(",")
  123. $fpiece1 = $gsplit1[0]
  124. $cnsplit1 = $fpiece1.split("=")
  125. $GPName2 = $cnsplit1[1]
  126.  
  127. #Write-Host "$GPName2 is a Group"
  128.  
  129.  
  130. ####get all groups from file to compare so as there is no circular nesting
  131.  
  132.  
  133. Foreach($gmember in $GPMember){
  134. $gsplit = $gmember.split(",")
  135. $fpiece = $gsplit[0]
  136. $cnsplit = $fpiece.split("=")
  137. $Name = $cnsplit[1]
  138.  
  139. $result = checkgroup $Name
  140.  
  141. if ($result -eq "true")
  142. {
  143. if ($getallgroups -contains $Name)
  144. {
  145. Write-Host "$Name equals $GPName2"
  146. #####not needed for troubleshooting######Add-Content .\conflict.txt "$Name equals $getallgroups -----"
  147.  
  148. }
  149. else
  150. {
  151. #####not needed for troubleshooting######Add-Content .\donotconflict.txt "$Name recurse"
  152. getallmembersrecursively $Name
  153. }
  154. }
  155.  
  156. else
  157. {
  158.  
  159. $WPFoutput.Items.Add("$name")
  160. ##############Write-Host "$Name not equals $GPName2"
  161.  
  162. }
  163. }
  164. }
  165. }
  166. }
  167. #######################################################################
  168. getallmembersrecursively $Group
  169. sleep 5
  170. #########################unique members################################
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. #######################################################################
  179.  
  180. $WPFexport.IsEnabled = $true
  181. }
  182.  
  183. function export {
  184. $openFileDialog = New-Object windows.forms.FolderBrowserDialog
  185. $result = $openFileDialog.ShowDialog()
  186. $result
  187. $result = $result -replace '(^\s+|\s+$)','' -replace '\s+',' '
  188.  
  189. if ($result -like '*Cancel*') {
  190. $WPFselectedfile.content = ""
  191. [System.Windows.Forms.MessageBox]::Show('No folder selected')
  192.  
  193. } else {
  194. $outputpath = $openFileDialog.SelectedPath
  195. $outputnamn = $wpfgroupname.text
  196. $outputpath = $outputpath + "\$outputnamn.txt"
  197. $WPFoutput.Items | Out-File $outputpath
  198. [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
  199. [Microsoft.VisualBasic.Interaction]::MsgBox("You will find your file here: $outputpath",'OKOnly,Information',"Export complete")
  200.  
  201. } }
  202.  
  203.  
  204.  
  205. ###################################
  206.  
  207.  
  208. #$Results | export-csv -Path $csvoutputpath -NoTypeInformation
  209. #$csvoutputpath = $openFileDialog.SelectedPath
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219. $Form.ShowDialog() | out-null
RAW Paste Data