使用 PowerShell 在 Active Directory 中移动用户不起作用

发布于 2025-01-20 20:14:52 字数 953 浏览 0 评论 0原文

我正在尝试制作一个脚本,允许域中的新用户根据其标签(例如 [DALT])放入某些组中。当我运行脚本时,它应该可以正常工作,因为它使用正确的 OU 和目标路径,但它似乎没有按照我期望的方式工作。我使用保存在 D 盘上的凭据作为拥有管理员权限的凭据。

$deviceName = Hostname
$deviceName = $deviceName -replace '[^a-zA-Z]', ''
$defaultName = Hostname

# Import Cred for access to change Dir
$credential = Import-Clixml -Path 'D:\backgroundProcess\cred.xml'
$credential

# Directing users to different part of AD depending on deviceName

# Action House (Branch of Company)
if ($deviceName -eq 'DALT') {
    Move-ADObject -Identity "CN=$defaultName,CN=Computers,DC=internal,DC=ttlhidden,DC=co,DC=uk" -TargetPath "OU=Windows,OU=Laptop,OU=Computers,OU=DEKRA,DC=internal,DC=ttlhidden,DC=co,DC=uk"
    Write-Host "$defaultName added to Hidden group."

    gpupdate /force
}

else {
    Write-Host "Sorry, $deviceName is not a verified name, please contact Max for more information."
}

Remove-Item -Path 'cred.xml'

即使我的计算机一开始就有 DALT,它似乎总是呈现 else 选项。

I am trying to make a script that allows new users on the domain to be put into certain groups based on their tag e.g [DALT]. When I run the script it should work properly as it uses the correct OUs and target path but it seems to not work the way I expect it to. I use a credential saved on my D drive as the cred to have admin rights.

$deviceName = Hostname
$deviceName = $deviceName -replace '[^a-zA-Z]', ''
$defaultName = Hostname

# Import Cred for access to change Dir
$credential = Import-Clixml -Path 'D:\backgroundProcess\cred.xml'
$credential

# Directing users to different part of AD depending on deviceName

# Action House (Branch of Company)
if ($deviceName -eq 'DALT') {
    Move-ADObject -Identity "CN=$defaultName,CN=Computers,DC=internal,DC=ttlhidden,DC=co,DC=uk" -TargetPath "OU=Windows,OU=Laptop,OU=Computers,OU=DEKRA,DC=internal,DC=ttlhidden,DC=co,DC=uk"
    Write-Host "$defaultName added to Hidden group."

    gpupdate /force
}

else {
    Write-Host "Sorry, $deviceName is not a verified name, please contact Max for more information."
}

Remove-Item -Path 'cred.xml'

It always seems to render the else option even if my computer has DALT at the start.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文