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

Active Directory Script fails

$
0
0

I'm trying to write a script that adds a user to Active Directory. I have been tweaking it a lot but I haven't been able to get it to work correctly. When I run the commands manually in Powershell I can type enter-pssession (domain controller hostname here) and the prompt in Powershell changes. I can then import the Active Directory module and add a user manually. I tried adding the import module command to the script but it seems like the script runs to fast and the import isn't sucessful. I'm trying to manage active Directory from a machine that isn't a domain controller. I tried to modify the script to import the module from a remote session into the local session but I haven't gotten it to work. Here is what I have so far.

 

$hostname = Read-Host -prompt "Enter in the hostname of the domain controller"
New-PsSession "$hostname"
Enter-Pssession "$hostname"
Invoke-Command -Computer "$hostname" {Import-Module ActiveDirectory}
$NewUsername = Read-Host -prompt "Enter in the new username"
$FirstName = Read-Host -prompt "Enter in the new user's first name"
$LastName = Read-Host -prompt "Enter in the new user's last name"
$AdminCredentials = Get-Credential
New-AdUser -SamAccountName "$NewUsername" -PasswordNeverExpires $false -DisplayName "$FirstName $LastName" -AccountPassword (Read-Host -AsSecureString "AccountPassword") -Credential "$AdminCredentials" -Enabled "$true"


Viewing all articles
Browse latest Browse all 6937

Trending Articles