为什么我的login脚本没有映射任何驱动器?

我在PowerShell中编写了一个login脚本。 脚本embedded在GPO中,并在用户login到域时运行。

我的问题是,我在脚本中部署的驱动器没有部署。 我确定login脚本运行,因为在脚本结束时,我发送一封邮件给自己,我总是收到它。 所以运行脚本不应该是问题。 每次脚本运行时,我还logging整个$Errorvariables,并且没有指示为什么会发生此错误。

我几乎可以肯定,错误不是在脚本本身 – 它必须是我认为的权限错误。

我认为在运行脚本时指定-NoProfile参数时可以解决这个问题,但是我不知道把它放在GPO的哪个位置。 我不想要batch file调用脚本,因为美的原因:-)。

编辑:指定-noprofile没有解决问题

编辑:由于有评论说没有足够的信息如何驱动器映射,我粘贴整个映射部分如下:

 # UserOU as a parameter, value set in GPO Param([string]$UserOU) # preparing some stuff... Import-Module .\SecureStringFunctions.psm1 [Byte[]]$key = (1..16) $pw = Get-Content .\LocalAdminCred.txt | ConvertTo-SecureString -key $key # Tried this to elevate the Script IF it's needed. Didn't solve my problem. works only on PS 4.0 but didn't solve the issue on my 5.0 machine if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit } # Import CSV with drive information based on username and userOU to get every maps that the current user needs # also replacing a string inside the CSV for current session to map the homedrive of each user $Drives = (Get-Content .\NetworkDrives.csv) -replace "userhome",$env:username | ConvertFrom-CSV -Delimiter ';' | ? { (($_.Group.split(',') -contains $UserOU) -or ($_.Group.split(',') -contains $env:username)) -and (!(Test-Path $_.Letter)) } # When I export the $Drives Variable at this point, all drives were read correctly, so the failure must occur in the mapping # map all drives for current user. Drives to DMZ with password in plain text because ".mapnetworkdrive" only accepts plain text. $Drives | % { if (![system.string]::IsNullOrEmpty($_.Username)) { if (($UserOU -like 'admin*') -and (($_.Server -ne $env:computername) -or ([system.string]::IsNullOrEmpty($_.Server)))) { Continue } [string]$pwplain = ConvertFrom-SecureString $pw -AsPlainText -Force $Map = New-Object -comobject Wscript.Network $Map.MapNetworkDrive($_.letter,$_.path,$false,$_.username,$pwplain) $pwplain = "" } else { $Map = New-Object -comobject Wscript.Network $Map.MapNetworkDrive($_.letter,$_.path,$false) } } 

我现在有一个解决scheme。

什么创build您的login脚本的行为不想映射驱动器?

如果你有Windows 10,你想使用边缘,计算器和PDF应用程序。 微软说你需要启用UAC,否则它将无法工作。 所以你通过GPO( EnableLua )来启用UAC。 https://technet.microsoft.com/en-us/library/dd835564%28v=ws.10%29.aspx

但是:如果您在GPO内部部署login脚本,并且您的用户是他login的计算机上的本地pipe理员 – 在我的情况下,由于我是域pipe理员,所以在我的情况下,UAC会将您的帐户到非特权用户,而且据我们所知,如果他们没有在同一个环境中运行,那么您的pipe理员帐号中就不会有您的驱动器。

这实际上是devise的,这是由UAC工作的方式造成的。 如果您是Administrators组的成员并且login,则UAC将您的帐户分配给非特权用户。 当您右键单击“命令提示符”并以pipe理员身份启动时,此运行上下文与您得到的上下文完全分离。 正如您可能已经注意到的那样,在一个环境中连接的networking驱动器在另一环境中是不可见的。 问题是基于GPO的login脚本正在pipe理员上下文中执行 – 而不是普通的用户上下文。

引自http://pcloadletter.co.uk/2010/05/15/missing-network-drives/

所以你正在禁用UAC和你的GPOlogin脚本将工作。 你开心了一会儿,但你意识到你不能再使用边缘了。

那么我们如何启用UAC并使我们的login脚本工作?

我这样做是由微软不支持的registry黑客。 我这对您的系统安全来说是一个漏洞,请记住,如果您像我一样执行此操作:

 HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System New Entry: Type: DWORD Name: "EnableLinkedConnections" Value: 1 

和BAM! 你可以使用login脚本,UAC,边缘,无论如何。

其他的东西,你可以尝试 – 我没有尝试任何这些:

  • 在存储在GPO中的批处理脚本中调用PS文件
  • 将您的PS脚本设置为GPO中的计划任务

所以希望这对于其他login脚本有问题的pipe理员有用。

干杯!

我认为你的问题可能存在于你正在导入的文件中。 这些文件是否存储在您的GPO sysvol中? \意味着它正在寻找与所讨论文件的脚本相同的目录。
现在,如果“Get-content”文件位于适当的位置,则可能是因为您正在调用尚未初始化的用户名环境variables。 尝试用像Brandyn这样的日志来testing合适的加载和variables。