【问题标题】:PowerShell Script Runs Locally, but Errors on RemotePowerShell 脚本在本地运行,但远程出错
【发布时间】:2017-09-30 11:13:53
【问题描述】:

我正在编写一个 PowerShell 脚本,用于在我们的域中创建新用户以及电子邮件地址。该脚本在我直接在 Exchange 上运行时有效。但是,如果我尝试使用 Enter-PSSessionInvoke-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

【问题讨论】:

  • 在使用 AD cmdlet 之前你能Import-Module ActiveDirectory 吗?
  • 我相信我试过了,它出错了,好像没有可用的模块一样。明天可以提供准确的措辞。

标签: powershell exchange-server


【解决方案1】:

如果您希望此脚本在没有 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】:

该机器上似乎没有安装 ActiveDirectory 模块,您可以安装 MSFT RSAT 工具来获取它。

【讨论】:

  • 谢谢你,我明天会试试,但考虑到如果我直接在目标机器上运行脚本(但方式是 Exchange 服务器)它仍然有效,这仍然有意义吗?跨度>
  • 啊,我看错了,我以为您正在远程连接到您的工作站。在运行脚本之前进行远程处理时,您可能需要 Import-Module ActiveDirectory。
【解决方案3】:

尝试以下方法,它有效! {我在给出身份验证类型后尝试}

$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

调用命令 -Session $s -scriptblock { 获取 CsUser 用户 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2013-10-15
    • 2020-11-12
    相关资源
    最近更新 更多