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

Sim*_*onS 2 windows powershell logon-scripts group-policy

我在 PowerShell 中编写了一个登录脚本。该脚本嵌入在 GPO 中,并在用户登录域时运行。

我的问题是,我在脚本中部署的驱动器没有被部署。我确信登录脚本会运行,因为在脚本结束时我会向自己发送一封电子邮件,但我总是会收到它。所以运行脚本应该不是问题。$Error每次脚本运行时,我都会记录整个变量,但没有说明为什么会发生此错误。

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

我认为当我-NoProfile在运行脚本时指定参数时可以解决这个问题,但我不知道将它放在 GPO 的哪个位置。由于美观原因,我不希望批处理文件调用脚本:-)。

编辑:指定-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)    
    }
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*onS 5

我现在有办法了。

是什么导致您的登录脚本不想映射驱动器的这种行为?

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

但是:如果您在 GPO 内部署登录脚本,并且您的用户是他登录的计算机上的本地管理员 - 在我的情况下是在每台计算机上,因为我是域管理员 - UAC 将删除您的帐户给非特权用户,正如我们所知,如果他们不在同一上下文中运行,您将无法在管理员帐户中部署驱动器。

这实际上是设计使然,并且是由 UAC 的工作方式造成的。当您是管理员组的成员并登录时,您的帐户将被 UAC 破坏给非特权用户。此运行上下文与您右键单击命令提示符并以管理员身份启动时获得的上下文完全分开。您可能已经注意到,在一个上下文中连接的网络驱动器在另一个上下文中不可见。问题是基于 GPO 的登录脚本是在管理员上下文中执行的——而不是普通用户上下文。

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

所以你禁用了 UAC,你的 GPO-Logon-Script 就可以工作了。你高兴了一会儿,但后来你意识到你不能再使用边缘了。

那么我们如何启用 UAC 并让我们的登录脚本工作呢?

我这样做是由 Microsoft不支持Registry Hack。我这是您系统安全的一个漏洞,如果您像我一样这样做,请记住这一点:

HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System
New Entry: Type: DWORD Name: "EnableLinkedConnections" Value: 1
Run Code Online (Sandbox Code Playgroud)

还有砰!您可以使用登录脚本、UAC、edge 等。

你可以尝试的其他事情 - 我还没有尝试过:

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

所以希望这对其他登录脚本有问题的管理员有用。

干杯!