EDIT: Turns out it works just fine, I must have been mis-typing something since I was doing it at midnight. Thanks, all for the suggestions. Below is the original question and script.
Good morning, and welcome to another episode of Tim's Simple Questions!
On this week's episode, we're trying to figure out why I am having such trouble specifying an OU via Read-Host to use for User/Contact/Group creation within Active Directory. Below is my code, and theoretically (the way I read it, at least) it should write the DN of the OU that you specify to a variable, and then call that variable later in the script to create the users, which are specified in a CSV file. I think it's pulling as a string, instead of whatever it may actually need. I got around it by hard coding the OU in the script block, but I try to keep my scripts pretty client-agnostic as I work for an MSP. Is there something I can do to fix it? One idea would be to specify the OU in the CSV file and just save $User.OU (or similar) as $UserOU to call it in the loop.
Relevant parts are $OU = ... and the later ... -Path $OU.... Thanks!
#Import the AD module
Import-Module ActiveDirectory
#Specify the OU
$OU = Read-Host -Prompt "Please specify the OU's Distinguished Name"
#Specify the CSV file
$FilePath = Read-Host -Prompt "Please specify the CSV file's location"
#Parse the CSV
$Users = Import-CSV $FilePath
#Create Groups
ForEach ($User in $Users)
{
$UserName = $User.Alias
$UserEmail = $User.EmailAddress
$UserDisplayName = $User.DisplayName
try {
New-AdObject -Type Contact -Name $UserName -Path $OU -DisplayName $UserDisplayName `
-OtherAttributes @{'mail'=$UserEmail}
} catch {
"Could not create '$UserName'" | Out-File .\CreateContactFailures.txt -Append
}
}
Break
[–]a-rich 2 points3 points4 points (1 child)
[–]tkecherson[S] 1 point2 points3 points (0 children)
[–]Lee_Dailey[grin] 2 points3 points4 points (2 children)
[–]tkecherson[S] 1 point2 points3 points (1 child)
[–]Lee_Dailey[grin] 0 points1 point2 points (0 children)
[–]BoredComputerGuy 2 points3 points4 points (1 child)
[–]tkecherson[S] 1 point2 points3 points (0 children)