Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

My script takes over an hour to run !?

$
0
0

Hi all,

I'm new to powershell and have finally finished my first script to get all the groups of 2 OU's and some extra groups that I need to report on and get all the users - and output into the format that I need (group, user, displayname).

When I did bit by bit each section took less than 10 mins to run. But now that I have joined it all together it takes over an hour and I don't know why.  Somewhere I have created a bottleneck.   Could one of you experts have a look please and offer some advice?

 

Thanks. Geeked

EDIT:  Found the mark up tool !  XD

 

function getGroups{
<#
This function performs a search of the 2 main OU's and adds all of the groups to an array called $GroupsList. Returns the array so it can be called.
#>
$GroupsList= @()
# add these extra groups to the grouplist array. They are spread over different OU's than the main 2 but are still needed to report on.
$specialMail= @("g1", "g2", "g3", "g4", "g5", "g6", "g7", "g8")

foreach ($Sin$specialMail){
$GroupList+=$S
}
# Get all the groups in the first main OU
$GetDTFGroups=Get-ADGroup-Properties*-Filter*-SearchBase"ou=Groups,ou=###,ou=###,dc=###,dc=main,dc=###"
#Get all the groups in the second main OU
$GetNTTGroups=Get-ADGroup-Properties*-Filter*-SearchBase"ou=Groups,ou=###,ou=###,dc=###,dc=main,dc=###"

# For each of the DTF OU add the GroupName to the array
Foreach($GIn$GetDTFGroups){
$Groupslist+=$G.SamAccountName
}

# For each of the NTT groups found add the GroupName to the array
Foreach($GIn$GetNTTGroups){
$GroupsList+=$G.SamAccountName
}
return$Groupslist
}
# Now go through each of the groups and get the users.
function get([string[]]$Groupslist){

foreach ($GroupNamein$Grouplist) {

Get-ADGroupMember-Identity$GroupName | Sort-Object$_.SamAccountName | ForEach-Object {
$ParentGroupName= (Get-ADGroup-Identity$GroupName).SamAccountName
$ParentGroupCategory= (Get-ADGroup-Identity$GroupName).GroupCategory
$MemberName=$_.SamAccountName

if ($_.ObjectClass-eq'group') {
$GroupCategory= (Get-ADGroup-Identity$GroupName).GroupCategory
$ObjectClass=$_.ObjectClass
$DisplayName=' - '
} elseif ($_.ObjectClass-eq'user') {
$a= (Get-ADUser-Identity$MemberName-Properties*)
$Name=$a.SamAccountName
$DisplayName=$a.DisplayName
}

# Edit line when output is correct. Maybe add this to an array instead.
Write-Output"$ParentGroupName,$MemberName,$DisplayName"
}
}
}

$date= [DateTime]::Now.ToString("yyyyMMMdd_HH:mm")
$sw= [Diagnostics.Stopwatch]::StartNew()

Clear-Host
$date | Out-Host
[array]$grouplist= getgroups
get | Out-File"C:\Users\vendril\Desktop\2015_29Jan_ActiveDirectoryGroupMembership.txt"

$sw.Stop()
$sw.Elapsed

Viewing all articles
Browse latest Browse all 6937

Trending Articles