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

..

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

I 또한 Exchange cmdlet을 실행하려고합니다.

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

내 사무실에 도착하자마자이 기능을 사용해 볼 수있게되어 기쁩니다. – bendparker

+0

매력처럼 작동했습니다. 고맙습니다! – bendparker

3

그것은 ActiveDirectory에 모듈이 해당 시스템에 설치되어 있지 않은 것 같습니다, 당신이 그것을 얻을 수있는 MSFT의 RSAT 도구를 설치할 수 있습니다

여기 내 스크립트입니다. 당신은 Active Directory 모듈이없는 시스템에서 실행하려면이 스크립트를 원하는 경우, 당신은 단순히 세션을 통해 cmdlet을 가져올 스크립트의 상단에이를 추가 할 수 있습니다

+0

고맙습니다. 그리고 나는 내일 그것을 시도 할 것이다. 그러나 나는 대상 기계에서 스크립트를 직접 실행하면 Exchange 서버라고 할 수있다. – bendparker

+1

아, 내가 틀린 것을 읽었습니다. 나는 당신이 당신의 워크 스테이션에 원격으로 있다고 생각했습니다. 스크립트를 실행하기 전에 원격으로 가져올 때 Import-Module ActiveDirectory가 필요할 수 있습니다. – Phil