2017-05-02 4 views
1

ドメイン内の新しいユーザーと電子メールアドレスを作成するために作成しているPowerShellスクリプトがあります。このスクリプトはExchangeで直接実行すると機能します。ローカルマシンから同じコマンドが作業を行うことを実行PowerShellスクリプトはローカルで実行されますが、エラーはリモートで発生します

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

:私は私のローカルPCからそれを実行しようとする場合は、どちらかEnter-PSSessionまたはInvoke-Commandと私はエラーを取得します。リモートマシンでそのコマンドを実行すると、リモートでスクリプトを実行しても問題ありません。

$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 
+0

AD cmdletを使用する前に 'Import-Module ActiveDirectory'を使用できますか? –

+0

私はそれを試したと信じて、モジュールが利用可能でないかのようにエラーを出しました。明日の正確な言葉遣いを提供できます。 – bendparker

答えて

3

このスクリプトは、Active Directoryモジュールを持っていないマシン上で実行したい場合、あなたは、単にセッションを介してコマンドレットをインポートするために、あなたのスクリプトの先頭にこれを追加することができます。..

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

私はまた、Exchangeコマンドレットを実行しようとしています。

$exchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://EXCHANGESERVER/PowerShell/" -Authentication Kerberos 
Import-PSSession $exchSession 
+0

私は私のオフィスに着くとすぐにこれを試して興奮しています。 – bendparker

+0

チャームのように働いた。ありがとうございました! – bendparker

3

それはのActiveDirectoryモジュールは、そのマシンにインストールされていないように見える、あなたはそれを得るためにMSFTのRSATツールをインストールすることができます

は、ここに私のスクリプトです。

+0

ありがとうございます。私は明日もそれを試してみますが、それはターゲットマシンでスクリプトを直接実行すると効果があると考えても意味があります(途中でExchangeサーバーです)。 – bendparker

+1

ああ、私はあなたがあなたのワークステーションにリモーティングしていると思ったが間違って読んだ。スクリプトを実行する前にリモーティングするときは、Import-Module ActiveDirectoryが必要な場合があります。 – Phil

関連する問題