【问题标题】:Creating new Profilepath for AD User为 AD 用户创建新的 Profilepath
【发布时间】:2021-08-19 04:28:24
【问题描述】:

我正在制作一个用于创建新 AD 用户的脚本。我还想给它一个 Profilepath 和一个 Homepath。但是当我尝试创建新的 Profilepath 和 Homepath 时,我在运行这部分时总是会出错。那是我的脚本:

$StandardPath = "\\192.168.1.32\Users\"
$ProfilePath = “\\192.168.1.32\Users\$($Username)"
$HomePath = “\\192.168.1\Users\$($Username)\Home"

New-Item -path $StandardPath -Name $Username -ItemType Directory -force
Set-ADUser $Username -ProfilePath $ProfilePath
New-Item -path $Profilepath -Name "Home" -ItemType Directory -force
Set-ADUser $Username -HomeDrive $driveLetter -HomeDirectory $HomePath

这是错误:

New-Item : The path is not of a legal form.
At C:\Users\ewzadmin\Desktop\AddADUsers.ps1:47 char:9
+         New-Item -path $StandardPath -Name $Username -ItemType Direct ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (\\192.168.1.32\Users:String) [New-Item], ArgumentException
    + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.NewItemCommand

有人可以帮帮我吗?

【问题讨论】:

  • 您不会忘记 $StandardPath 中的驱动器号吗? "\\192.168.1.32\$driveLetter\Users" ?附言最好不要使用(在这种情况下混合使用)大引号 并始终使用直引号。还要确保您的变量 $Username 不包含无效的文件夹名称字符
  • 天哪,你对驱动器字母的看法是对的,我完全忘记了这一点。
  • 不要忘记管理驱动器号共享也以$ 为后缀。

标签: powershell active-directory


【解决方案1】:

这里发生了一些事情:

  • 您需要在服务器名称和共享路径之间的 UNC 路径中指定共享名称。默认情况下会为附加存储自动创建管理共享(例如C:),并且驱动器号以$ 为后缀。
    • 您不需要转义 $,因为继续进行的 \ 不会被解析为变量名的一部分
  • 您在设置$HomePath 时忘记了IP 的最后一个八位字节
  • 虽然不是问题,但我们可以使用 $StandardPath 变量来构建其他两个
  • 两个New-Item 调用都可以省略-Name 参数,因为您在-Path 变量中指定了目录路径。使用-Name 参数将在-Path 中指定的目录下创建另一个 目录,这将创建任何缺少的目录。
  • 您也可以简单地使用$ProfilePath创建$Username目录,因为路径与$StandardPath\$Username相同
  • 您不需要使用$($Username) 来扩展单个变量。您可以完全省略子表达式运算符$()

以下更正应该适合您:

警告:如果您必须使用漫游配置文件,请通过DFS 或至少一个适当的存储设备支持它们,而不是来自单个服务器的CIFS 共享。也不要使用 IP,在 UNC 路径中使用 NetBIOSDNSFQDN 名称。如果该服务器出现故障或 IP 更改,您在该系统上的用户将遇到配置文件无法正确同步的问题,并且在同步问题方面漫游配置文件可能变化无常。

$StandardPath = "\\192.168.1.32\$driveletter$\Users"
$ProfilePath = "$StandardPath\$Username"
$HomePath = “$ProfilePath\Home"

New-Item -Path $ProfilePath -ItemType Directory -Force
Set-ADUser $Username -ProfilePath $ProfilePath
New-Item -Path $ProfilePath -ItemType Directory -Force
Set-ADUser $Username -HomeDrive $driveLetter -HomeDirectory $HomePath

【讨论】: