Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

alright im breaking down, I need logic help…possibly other things

$
0
0

Alright so I am breaking down…I need your help figuring out the logic I need to write for this function to work.

What this function does (or what it is supposed to do):
Look through our AD environment based on parameters specified after the search-AD_object cmdlet is passed. Mostly searching through OU’s and finding users and large amount of computers in different office locations.

What I am trying to accomplish:
As you can see there are four parameters, and the function worked great with the first three parameters until I added the fourth $type parameter today and messed with all of my logic. So I want the user running the function to have the option to search through an OU,possible sub OU and pull out only the objects in the OU matching the type (i.e. computer,user,group). As you can see I currently have a bunch of elseif statements (which I actually dont like and I find them to be alot of clutter but I am still learning powershell and its what I have for now).

What currently happens:

Say I run my new command with the given parameters

“Search-AD_Object -OrganizationalUnit Campus_San_Marcos -Type Computer”

in a way it works but it gives me every type of object in the OU, which is a no because I only want the computers.

Where I am at now:

currently frustrated at my lack of logic skills. So if you have any guidance to resolve my issue that would be great, even if you think you can add efficiency to my code, I would love to hear it.

$OrganizationalUnit
$SubOrganizationalUnit
$ObjectName
$Type


function Search-AD_Object
{
[CmdletBinding()]

Param(
[Parameter (Mandatory=$true)]
[ValidateSet('_Servers','Application_Accounts','Builtin','Campus_Austin','Campus_Co-Lo','Campus_FLEX','Campus_Online','Campus_Saint_Augustine','Campus_San_Marcos','Computers','Domain Controllers','Exchange Groups','Exchange Mailboxes','External Contacts','ForeignSecurityPrincipals','Groups','IT Personnel','Laureate','LostAndFound','Managed Service Accounts','Microsoft Exchange Security Groups', 'Students','Students_i nactive','Users')]

[string]$OrganizationalUnit,

[ValidateSet('_Servers','_Dev_Servers','Faculty','Library','Finance','Smart_Carts','Staff','Computers','Users')]
[string]$SubOrganizationalUnit,

[string[]]$ObjectName,

[ValidateSet('Computer','User','Group','site')]
[string]$Type,

)

BEGIN{

$ou_result = Get-ADObject -LDAPFilter "(name=*)" -SearchBase "DC=xxx,DC=xxx" -SearchScope OneLevel | `
Where-Object {$_.Name -eq "$OrganizationalUnit"} | `
Select DistinguishedName -ExpandProperty DistinguishedName

Write-Host "-----------------------------------------------------------"
Write-Host "You're Searching in the $ou_result OU" -ForegroundColor Green
Write-Host "-----------------------------------------------------------"
r
$sub_result = Get-ADObject -LDAPFilter "(name=*)" -SearchBase $ou_result -SearchScope OneLevel | `
Where-Object {$_.Name -eq "$SubOrganizationalUnit"} | `
Select DistinguishedName -ExpandProperty DistinguishedName

If(-not($SubOrganizationalUnit) -and ($Type)) {
$Search = Get-ADObject -LDAPFilter "(name=*)" -SearchBase $ou_result | `
Where-Object {$_.Name -match "$ObjectName"} | `
Select Name

echo $Search
}
elseif($SubOrganizationalUnit) {
Write-Host "-----------------------------------------------------------"
Write-Host "You're now in $sub_result OU" -ForegroundColor Green
Write-Host "-----------------------------------------------------------"

$Search = Get-ADObject -LDAPFilter "(name=*)" -SearchBase $sub_result -SearchScope OneLevel | `
Where-Object {$_.Name -match "$ObjectName"} | `
Select Name -ExpandProperty Name

echo $Search
}

elseif($Type) {

$Search = Get-ADObject -LDAPFilter "(objectClass=$Type)" -SearchBase $ou_result -SearchScope Base | `
Where-Object {$_.Name -match "$ObjectName"} | `
Select Name -ExpandProperty Name

echo $Search
}

elseif($SubOrganizationalUnit -and $Type) {
Write-Host "-----------------------------------------------------------"
Write-Host "You're now in $sub_result OU" -ForegroundColor Green
Write-Host "-----------------------------------------------------------"

$Search = Get-ADObject -LDAPFilter "(objectClass=$Type)" -SearchBase $sub_result -SearchScope Base | `
Where-Object {$_.Name -match "$ObjectName"} | `
Select Name -ExpandProperty Name

echo $Search

}

}

PROCESS {

foreach ($i in $Search) {

Write-Host "this works"

}

}

}


Viewing all articles
Browse latest Browse all 13067

Trending Articles