使用Powershell创建AD用户时,驱动器映射不正确

时间:2016-07-06 17:23:21

标签: powershell active-directory powershell-v3.0 network-share

我编写了一个效果很好的脚本..它创建用户帐户,设置密码,放置正确的OU,创建主驱动器文件夹和子文件夹。用户能够登录并且没有问题但是我注意到的一件事是用户映射到AD中指定的差异驱动器。脚本块为:

$homedrivepath = "Server_Path_for_users_data"
$homedrive = "Y"

New-ADUser -HomeDirectory "$homedrivepath" -HomeDrive "$homedrive"

正如我所提到的,其他一切都很好。这个问题是即使在AD中,它显示为Y是驱动器号,但是当用户登录时,它将它们映射到Z:而不是。任何想法为什么会发生这种情况?它不是由GPO或其他任何类似的管理。

谢谢!

3 个答案:

答案 0 :(得分:1)

我遇到了驱动器映射为Z的问题: 我使用vb.net windows应用程序创建新用户,我将homedrive作为M而不是M:

这解决了我的问题!! 所以同样适用于vb.net和powershell一样。

答案 1 :(得分:0)

你有其他任何脚本吗?检查它是否乱七八糟。

测试此脚本可以轻松创建广告。 https://gallery.technet.microsoft.com/scriptcenter/Create-AD-user-on-the-go-3b754198

Import-Module activeDirectory 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null  
$Firstname = [Microsoft.VisualBasic.Interaction]::InputBox("Enter firstname ") 
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null  
$Lastname = [Microsoft.VisualBasic.Interaction]::InputBox("Enter Lastname ") 
$PWD=[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null  
$PWD= [Microsoft.VisualBasic.Interaction]::InputBox("Enter Password ") 
$dnsroot = '@' + (Get-ADDomain).dnsroot 
$OU="CN=users, DC=Domain,DC=COM" 
$SAM = $user.FirstName.Substring(0,1) + $user.LastName #example John snow will be Jsnow 
    #$Sam=$User.FirstName+$User.LastName example john snow will be Johnsnow 
    #$Sam=$User.FirstName example john snow will be John 
    #$Sam= $User.firstName + "." + $User.lastName example john snow will be John.snow 
$Dname="$firstName " + "$lastName" 
$UPN = $Sam + “$dnsroot” 
New-ADUser -Name "$firstName $lastName" -DisplayName $Dname -AccountPassword (ConvertTo-SecureString “$pwd” -AsPlainText -force) -GivenName $FirstName  -Path $OU -SamAccountName $SAM -Surname $LastName  -UserPrincipalName $UPN -Enabled $TRUE 
Write-Output "User $Firstname has been created" -foregroundcolor Green 
## setting up stuff## 
$user=$sam 
Write-Host "Would you like to set up Home Drive?"        -ForegroundColor Green 
$Response = Read-Host "[Y] Yes, [N] No" 
    If($Response -eq "y") 
{Get-ADUser $user | % { Set-ADUser $_ -HomeDrive "H:" -HomeDirectory ('\\SERVER\home$\' + $_.SamAccountName) }} 


#now create Home folder# 


    if( -not ( Test-Path \\server\home$\$User ) ){ 
        New-Item -Path \\server\home$\$User -ItemType directory 
        } 


#**** setting up Login script* 
Write-Host "Would you like to add Login Script?"        -ForegroundColor Green 
$Response = Read-Host "[Y] Yes, [N] No" 
    If($Response -eq "y") 
{Get-ADUser $user | Set-ADUser -ScriptPath 'yyy.bat'} 


## add to group## 
Write-Host "allow to add to group Group?"        -ForegroundColor Green 
$Response = Read-Host "[Y] Yes, [N] No" 
    If($Response -eq "y") 
 {Add-ADGroupMember -Identity Group -Member $user} 
 write-host "Sit back and relax all task has been completed"        -ForegroundColor Green

答案 2 :(得分:0)

你应该写:

$homedrive = "Y:"

而不是:

$homedrive = "Y"