Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $ErrorActionPreference = 'SilentlyContinue'
- #Your XAML goes here :)
- $inputXML = @"
- <Window
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:local="clr-namespace:AD_Extractor"
- Title="AD Extractor" Height="431" Width="343" ResizeMode="NoResize" WindowStartupLocation="Manual">
- <Grid>
- <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">
- <TextBox x:Name="groupname" HorizontalAlignment="Left" Height="23" Margin="10,17,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="280"/>
- </GroupBox>
- <GroupBox x:Name="groupBox1" Header="Members:" HorizontalAlignment="Left" Height="248" Margin="10,97,0,0" VerticalAlignment="Top" Width="315">
- <ListBox x:Name="output" HorizontalAlignment="Left" Height="208" Margin="10,10,0,0" VerticalAlignment="Top" Width="285"/>
- </GroupBox>
- <Button x:Name="extract" Content="Extract members" HorizontalAlignment="Left" Margin="10,353,0,0" VerticalAlignment="Top" Width="120"/>
- <Button x:Name="export" Content="Export to file" HorizontalAlignment="Left" Margin="205,353,0,0" VerticalAlignment="Top" Width="120" IsEnabled="False"/>
- <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"/>
- </Grid>
- </Window>
- "@
- $inputXML = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
- [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
- [xml]$XAML = $inputXML
- #Read XAML
- $reader=(New-Object System.Xml.XmlNodeReader $xaml)
- try{
- $Form=[Windows.Markup.XamlReader]::Load( $reader )
- }
- catch{
- 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)"
- throw
- }
- #===========================================================================
- # Load XAML Objects In PowerShell
- #===========================================================================
- $xaml.SelectNodes("//*[@Name]") | %{;
- try {Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -ErrorAction Stop}
- catch{throw}
- }
- Function Get-FormVariables{
- if ($global:ReadmeDisplay -ne $true){
- #Write-host "If you need to reference this display again, run Get-FormVariables" -ForegroundColor Yellow;$global:ReadmeDisplay=$true
- }
- #write-host "Found the following interactable elements from our form" -ForegroundColor Cyan
- #get-variable WPF*
- }
- #Get-FormVariables
- #===========================================================================
- # Use this space to add code to the various form elements in your GUI
- #===========================================================================
- #############
- ## KNAPPAR ##
- #############
- $WPFextract.Add_Click({extract})
- $WPFexport.Add_Click({export})
- ##########################
- ## Mostly GUI-functions ##
- ##########################
- function extract {
- $WPFoutput.Items.Clear()
- ######################check if object is group or not #############################
- $Group = $wpfgroupname.text
- function checkgroup ($Group1)
- {
- $Search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
- $Search.filter = "(&(objectCategory=group)(objectClass=group)(cn=$Group1))"
- $input=$Search.Findall()
- if($input -ne $null)
- {
- ##Write-Host "$Group1 is a valid"
- return $true
- }
- else
- {
- ##Write-Host "$Group1 is a invalid"
- return $false
- }
- }
- ##################################Recurse thru groups ##############################
- function getallmembersrecursively ($Group)
- {
- $Search = New-Object DirectoryServices.DirectorySearcher([ADSI]"")
- $Search.filter = "(&(objectCategory=group)(objectClass=group)(cn=$Group))"
- $input=$Search.Findall()
- if($input -ne $null)
- {
- Foreach($group in $input){
- $groupname = $group.GetDirectoryEntry()
- $GPName = $groupname.DistinguishedName
- $GPMember = $groupname.member
- $GPName1 = [string]$GPName
- $gsplit1 = $GPName1.split(",")
- $fpiece1 = $gsplit1[0]
- $cnsplit1 = $fpiece1.split("=")
- $GPName2 = $cnsplit1[1]
- #Write-Host "$GPName2 is a Group"
- ####get all groups from file to compare so as there is no circular nesting
- Foreach($gmember in $GPMember){
- $gsplit = $gmember.split(",")
- $fpiece = $gsplit[0]
- $cnsplit = $fpiece.split("=")
- $Name = $cnsplit[1]
- $result = checkgroup $Name
- if ($result -eq "true")
- {
- if ($getallgroups -contains $Name)
- {
- Write-Host "$Name equals $GPName2"
- #####not needed for troubleshooting######Add-Content .\conflict.txt "$Name equals $getallgroups -----"
- }
- else
- {
- #####not needed for troubleshooting######Add-Content .\donotconflict.txt "$Name recurse"
- getallmembersrecursively $Name
- }
- }
- else
- {
- $WPFoutput.Items.Add("$name")
- ##############Write-Host "$Name not equals $GPName2"
- }
- }
- }
- }
- }
- #######################################################################
- getallmembersrecursively $Group
- sleep 5
- #########################unique members################################
- #######################################################################
- $WPFexport.IsEnabled = $true
- }
- function export {
- $openFileDialog = New-Object windows.forms.FolderBrowserDialog
- $result = $openFileDialog.ShowDialog()
- $result
- $result = $result -replace '(^\s+|\s+$)','' -replace '\s+',' '
- if ($result -like '*Cancel*') {
- $WPFselectedfile.content = ""
- [System.Windows.Forms.MessageBox]::Show('No folder selected')
- } else {
- $outputpath = $openFileDialog.SelectedPath
- $outputnamn = $wpfgroupname.text
- $outputpath = $outputpath + "\$outputnamn.txt"
- $WPFoutput.Items | Out-File $outputpath
- [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
- [Microsoft.VisualBasic.Interaction]::MsgBox("You will find your file here: $outputpath",'OKOnly,Information',"Export complete")
- } }
- ###################################
- #$Results | export-csv -Path $csvoutputpath -NoTypeInformation
- #$csvoutputpath = $openFileDialog.SelectedPath
- $Form.ShowDialog() | out-null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement