1

I have a PowerShell script I am writing to create new users in our domain, as well as email address. The script works when I run it directly on Exchange. However, if I try to do it from my local PC either with Enter-PSSession or Invoke-Command I get the error:

The term 'Get-ADUser' is not recognized as the name of a cmdlet...

Running that same command from the local machine does work. And running that command on the remote machine works, just not if I run the script remotely.

Here is my script:

$cred = Get-Credential

$first_name = Read-Host -Prompt "What is the new user's first name?"
$last_name = Read-Host -Prompt "What is the new user's last name?"
$copy_from = Read-Host -Prompt "Copy from other user (leave blank if not)?"
$password = Read-Host -Prompt "New user's password?"
$ss_password = ConvertTo-SecureString -String $password -AsPlainText -Force

$new_user_name = $last_name.Substring(0,3) + $first_name.Substring(0,2)
$new_user_name = $new_user_name.ToLower()
Write-Host "Creating user $new_user_name..." -ForegroundColor Green

if ([string]::IsNullOrEmpty($copy_from)) 
{
    Write-Host "Setting up new user (not copying...)" -ForegroundColor Yellow
    New-ADUser -Name "$first_name $last_name" -AccountPassword $ss_password -SamAccountName $new_user_name -PassThru | Enable-ADAccount
} 
else 
{
    $copy_from_user = Get-ADUser -Identity $copy_from
    Write-Host "Copying user from: " $copy_from_user.Name -ForegroundColor Yellow
    $ou = $copy_from_user.DistinguishedName -replace '^cn=.+?(?<!\\),'
    New-ADUser -Name "$first_name $last_name" -AccountPassword $ss_password -Path $ou -SamAccountName $new_user_name -PassThru | Enable-ADAccount
    $new_user = Get-ADUser -Identity $new_user_name

    #Time to copy their group memberships
    Get-ADUser -Identity $copy_from -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members $new_user_name
}

$pn = $new_user_name + "@INDY"
Set-ADUser -Identity $new_user_name -GivenName $first_name -Surname $last_name -UserPrincipalName $pn

#Now create email
$email_select = Read-Host -Prompt "Select email domain (1.  Woodmizer; 2.  Lastec;  3. Brightstone)"

if ($email_select -eq 2) 
{
    $domain = "@lastec.com"
}
elseif ($email_select -eq 3)
{
    $domain = "@brightstoneabrasives.com"
}
else 
{
    $domain = "@woodmizer.com"
}

$email_address1 = $first_name.Substring(0,1) + $last_name + $domain
Write-Host "Creating mailbox $email_address1..." -ForegroundColor Green

Enable-Mailbox -Identity $new_user_name -Database "Mailbox Database 1188513962"
Start-Sleep -s 10
Get-Mailbox -Identity $new_user_name | Set-Mailbox -EmailAddresses @{add="$email_address1"} -EMailAddressPolicyEnabled $false
Get-Mailbox -Identity $new_user_name | Set-Mailbox -PrimarySmtpAddress $email_address1 -EmailAddressPolicyEnabled $false

Write-Host "Finished." -ForegroundColor Green
2
  • Can you Import-Module ActiveDirectory before using an AD cmdlet? Commented May 2, 2017 at 20:12
  • I believe I tried that, and it errored out as if no module was available. Can provide exact verbiage tomorrow. Commented May 3, 2017 at 1:47

3 Answers 3

3

If you want this script to run on machines that don't have the Active Directory module, you can simply add this to the top of your script to import the cmdlets via session..

$cred = Get-Credential "DOMAIN\adminuser"
$ADsession = New-PSSession -ComputerName DOMAINCONTROLLERNAME -Credential $cred
Import-Module -PSSession $ADsession ActiveDirectory

I also notice you're trying to run Exchange cmdlets..

$exchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://EXCHANGESERVER/PowerShell/" -Authentication Kerberos
Import-PSSession $exchSession 
Sign up to request clarification or add additional context in comments.

2 Comments

Excited to try this as soon as I get to my office.
Worked like a charm. Thank you!
3

It looks like the ActiveDirectory module is not installed on that machine, you can install the MSFT RSAT tools to get it.

2 Comments

Thank you and.I will try that tomorrow, but does that still make sense considering it works if I run the script directly on the target machine (which but the way is an Exchange server)?
Ah, I read it wrong I thought you were remoting to your workstation. You might have to Import-Module ActiveDirectory when remoting before running the script.
0

Try the following, It works!! {I tried after giving the Authentication type}

$pass = ConvertTo-SecureString -AsPlainText 'PASSWORD' -Force

$MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'USERNAME',$pass

$s=New-PSSession SERVERNAME -Credential $MySecureCreds -Authentication Credssp

Invoke-Command -Session $s -scriptblock { Get-CsUser User }

Comments

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.