2

Right so essentially I am storing a domain name into a variable - which then I need to split up later in my code into it's individual parts i.e.

$domainName = "testdomain.co.uk"
$domainNameSplit = $domainName.split(".")

echo $domainNameSplit[0]
echo $domainNameSplit[1]
echo $domainNameSplit[2]

In this instance it's fine as I know it's a .co.uk so will have 3 parts - however my code allows for input and therefore may take a testdomain.local format instead. How can I look through the $domainName variable and find the number of '.' in order to know what to do with it?

I also need to then use it in order to create a $ou variable to work with, so:

$ou = "DC=WhatWouldIPutHere?,DC=AndHere"

Thanks!

2 Answers 2

3

Just another suggestion:

$domainName = "testdomain.co.uk"

# Get Count
$PartCount = $domainName.Split(".").Count

# Make an OU
$ou = "DC=" + $domainName -replace "\.", ",DC="
Sign up to request clarification or add additional context in comments.

Comments

2

Just count the array length isn't enougth?

$domainName = "testdomain.co.uk"
$domainNameSplit = $domainName.split(".")

$domainNameSplitCount = $domainNameSplit.count

or to create a DN:

$domainName = "testdomain.co.uk"
$domainNameSplit = $domainName.split(".")
$a = [string]::empty
$domainNameSplit | % { $a+="DC=$_," }
$a.trim(',')

4 Comments

What does the $_ symbolise in this case? That has always confused me
$_ is always the actual item processed in a pipe.
So the variable being used at the beginning of the pipeline?
$$domainNameSplit contains 3 string, the foresach-object process one string at time assigning the value to $_.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.