我正在撰写PowerShell脚本,以便在我们的域中创建新用户,以及电子邮件地址。当我直接在Exchange上运行时,该脚本可以正常工作。但是,如果我尝试使用Enter-PSSession
或Invoke-Command
从我的本地PC执行此操作,则会收到错误消息:
术语' Get-ADUser'不被识别为cmdlet的名称...
从本地计算机运行相同的命令确实有效。并且在远程计算机上运行该命令可以正常工作,但是如果我远程运行该脚本则不行。
这是我的剧本:
$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 :(得分:3)
看起来该机器上没有安装ActiveDirectory模块,您可以安装MSFT RSAT工具来获取它。
答案 1 :(得分:3)
如果您希望此脚本在没有Active Directory模块的计算机上运行,您只需将其添加到脚本顶部即可通过会话导入cmdlet。
$cred = Get-Credential "DOMAIN\adminuser"
$ADsession = New-PSSession -ComputerName DOMAINCONTROLLERNAME -Credential $cred
Import-Module -PSSession $ADsession ActiveDirectory
我还注意到您正在尝试运行Exchange cmdlet ..
$exchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://EXCHANGESERVER/PowerShell/" -Authentication Kerberos
Import-PSSession $exchSession
答案 2 :(得分:0)
尝试以下方法,它有效!! {我在提供身份验证类型后尝试过}
$ pass = ConvertTo-SecureString -AsPlainText'PASSWORD'-强制
$ MySecureCreds = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList'USERNAME',$ pass
$ s = New-PSSession SERVERNAME-凭据$ MySecureCreds-身份验证Credssp
Invoke-Command -Session $ s -scriptblock { Get-CsUser用户 }