问题在Windows 10中映射用户的主驱动器

时间:2016-10-11 23:08:10

标签: powershell active-directory wsh

在我工作的环境中,有时用户的Active Directory(AD)主驱动器在登录时未映射,因为它们未连接到网络。之后,当他们通过VPN连接到网络时,他们需要能够映射他们的AD家庭驱动器。我在PowerShell中创建了一个脚本来执行此操作但是,在Windows 10上,我收到错误“对象引用未设置为对象的实例”。并且脚本无法映射用户的主驱动器。如果我在Windows 7上运行相同的脚本,它可以正常工作。对此问题的任何帮助将不胜感激。

Param (
    [switch] $verbose,
    [switch] $whatif
)

Function Write-Event{
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [Parameter(Mandatory=$true)]
        $Message,
        [parameter(Mandatory=$FALSE)]
        [String[]] $LogName = 'Application',
        [String[]] $Source = "WSH",
        [String[]] $EntryType = "Information",
        [Int] $EventId = 4  
    )
    $tab= [char]9
    Write-Verbose "$tab In the function to write the event to the log"
    TRY {
        Write-Verbose "$tab Using the Wscript.shell COM object to write the event"
        $WShell = New-Object -ComObject WScript.Shell
        IF ($WhatIf){
            Write-output "What if: Performing operation Writing message to the $LogName with Event ID $EventId and message, $Message"
        } Else {
            $WShell.LogEvent($EventId, $Message) > $null
            Write-Verbose "$tab Info: Successfully wrote, $Message, to $LogName log"    
        }

    }
    CATCH {
        Write-Warning "$tab Error writing, $Message, to $LogName log"
    }
    Write-Verbose "$tab End of function to write the event to the log"
}
Function Get-ADUserProperties {
    #Requires -Version 3.0
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [parameter(Mandatory=$FALSE)]
        [String] $Username = [Environment]::UserName    
    )

    $DateNow = Get-Date
    Write-verbose "$DateNow - Getting Active Directory User Account information"
    Write-Event -Message "$DateNow - Getting Active Directory User Account information"

    # Get the user accounts AD properties       
    Try 
    {
        Write-verbose "Checking to see if User, $Username, can be found within the domain ..."
        $strFilter = "(&(objectClass=user)(name=$Username))"
        # Connect to the domain and create search object
        $objDomain = New-Object System.DirectoryServices.DirectoryEntry
        $objSearcher = New-Object System.DirectoryServices.DirectorySearcher
        $objSearcher.SearchRoot = $objDomain
        $objSearcher.PageSize = 15000
        $objSearcher.Filter = $strFilter
        $objSearcher.SearchScope = "Subtree"
        # Search for System
        $AllObj = $objSearcher.FindAll() 
        $ADUser = $AllObj.Properties

        Return $ADUser
    }
    CATCH 
    {
        $DateNow = Get-Date
        Write-Warning -Message "$DateNow - Error: $Username could not be found in AD"
        Return $null
    }

}

Function Set-HomeDrive {
    #Requires -Version 3.0  
    <#
        .SYNOPSIS
        Script to map a users home folder
        .DESCRIPTION
        The script will check to see if the user running it is found within  Active directory. If the user does exist it will get the AD properties for the user.
        Once the AD properties are known it will check to see if the home folder and path exist and are mapped.
        If they are already mapped it will open explorer.exe to that location.
        If they are not mapped, it will map the folder and open explorer.exe to that location.

        .PARAMETER CRQ
        Takes the CRQ number as input.  Should be a string or integer.
        .PARAMETER Name
        Takes a string as input, this should not contain any spaces or special characters.  Will be part of the log file name.

        .EXAMPLE
        C:\PS> .\MapHomeDrive.ps1
        This will map the users Active Directory Home drive letter to their home share 

        .INPUTS
        None
        .OUTPUTS
        None    

    #>      
    [CmdletBinding(SupportsShouldProcess=$true)]
    Param (
        [parameter(Mandatory=$false)]
            [String[]] $CRQ = '19175-10468',
            [String[]] $Name = 'MapHomeDrive'   
    )
    Begin {
        $ErrorActionPreference = "Stop"
        Write-Output -InputObject "`n"
        $DateNow = Get-Date
        Write-Output "$DateNow - Preparing Environment to Map Home Drive"
        $strCompName = $env:computername ; $strWinDir = $env:windir
        $quote = [char]34 ; $tab= [char]9 ; $plus=[char]43 ; $LOGFILE=$null  ; $text = $null ; $Color = $null
        Write-Verbose -Message "Outputting all initial parameters: `n $tab $tab CRQ: $CRQ `n $tab $tab Name: $Name `n"      

    }
    Process {

        $DateNow = Get-Date
        Write-verbose "$DateNow - $Name script started running"
        Write-Event -Message "$DateNow - $Name script started running"
        # Get the user accounts AD properties
        $ADUserProps = Get-ADUserProperties
        IF ($ADUserProps -ne $null) 
        {
            # Set variables for the user's home folder and drive
            $HomeFolder = $ADUserProps.homedirectory
            $HomeDrive = $ADUserProps.homedrive             

            IF (($HomeFolder -eq $null) -or ($HomeDrive -eq $null)) 
            {
                Write-Warning -Message "Error: $Username does not have a home directory or home drive in AD, exiting script" 
                Write-Event -Message "Error: $Username does not have a home directory or home drive in AD, exiting script" -EventID 1               
            }
            ELSE 
            {
                IF ($HomeDrive -Match ":"){$HomeDrive1 = $HomeDrive -replace ".$"} else {$HomeDrive1 = $HomeDrive}          
                # Get the logical disk for the home drive
                $TestHomeDrive = Get-WMIObject Win32_LogicalDisk -Property *|Where-Object {($_.DeviceID -eq $HomeDrive)}
                # Check to see if the drive is already mapped
                IF ($TestHomeDrive -eq $null) 
                {
                    Write-Verbose "Info: The drive, $HomeDrive1, is NOT already mapped, mapping home drive"
                    Write-Event -Message "Info: The drive, $HomeDrive1, is NOT already mapped, mapping home drive" -EventID 2
                    $net = New-Object -ComObject WScript.Network
                   Write-Verbose "$net"
                    TRY 
                    {                       
                        IF ($WhatIf){
                            Write-output "What if: Performing operation mapping, $HomeFolder to $HomeDrive"
                        } Else {
                            $net.MapNetworkDrive($HomeDrive, $HomeFolder, $true)
                        }                       
                    }
                    CATCH {
                        Write-Error -Message "Unable to map $HomeFolder to $HomeDrive  - $_"
                    }
                }
                ELSEIF ($TestHomeDrive.ProviderName -eq $HomeFolder) 
                {
                    Write-Verbose "Info: The drive, $HomeDrive1, is already mapped to $HomeFolder."
                    Write-Event -Message "Info: The drive, $HomeDrive1, is already mapped to $HomeFolder." -EventID 2
                } 
                ELSE 
                {
                    Write-Verbose "$$$$You should not be here"
                }

            }               

        }
    ELSE 
    {
        Write-Warning -Message "Error: $Username could not be found in AD, the home drive will not be mapped, exiting script" 
        Write-Event -Message "Error: $Username could not be found in AD, the home drive will not be mapped, exiting script" -EventID 1          
    }

    # Ensure that the script can get to the Home drive and launch explorer to that location
    IF ($HomeDrive -ne $null)   
    {
        IF (test-path $HomeDrive)
        {
            $DateNow = Get-Date
            $MessageText = "$DateNow - Successfully mapped drive $HomeDrive1 to $HomeFolder"
            Write-Event -Message $MessageText -EventID 0
            Write-Output $MessageText
            # Launch Explorer.exe to the Home drive folder
            $EXSwitches = "/e," + $quote + $HomeDrive1 + ":" + $quote
            & "explorer.exe" $EXSwitches
        } 
    }
    ELSE 
    {
        $DateNow = Get-Date
        $MessageText = "$DateNow - Error mapping drive Home Drive to Home Folder"
        Write-Event -Message $MessageText -EventID 1    
        Write-Verbose $MessageText              
    }


    }
    End {
        $DateNow = Get-Date
        Write-Event -Message "$DateNow - $Name script finished running"
        Write-Output "$DateNow - End of script to map home drive"
        Write-Output -InputObject "`n"
    }

 }
IF ($Verbose) {Set-HomeDrive -verbose} ELSEIF ($Whatif) {Set-HomeDrive -whatif} ELSE {Set-HomeDrive }

错误输出显示错误来自代码行“IF($ Verbose){Set-HomeDrive -verbose} ELSEIF($ Whatif){Set-HomeDrive -whatif} ELSE {Set-HomeDrive}”。当我使用verbose选项运行脚本时,我得到以下输出。

VERBOSE: Outputting all initial parameters:
     CRQ: 19175-10468
     Name: MapHomeDrive
VERBOSE: 10/12/2016 12:08:30 - MapHomeDrive script started running
VERBOSE:   In the function to write the event to the log
VERBOSE:   Using the Wscript.shell COM object to write the event
VERBOSE:   Info: Successfully wrote, 10/12/2016 12:08:30 - MapHomeDrive script started running, to Application log
VERBOSE:   End of function to write the event to the log
VERBOSE: 10/12/2016 12:08:30 - Getting Active Directory User Account information
VERBOSE:   In the function to write the event to the log
VERBOSE:   Using the Wscript.shell COM object to write the event
VERBOSE:   Info: Successfully wrote, 10/12/2016 12:08:30 - Getting Active Directory User Account information, to
Application log
VERBOSE:   End of function to write the event to the log
VERBOSE: Checking to see if User, keith.richardson, can be found within the domain ...
VERBOSE: Info: The drive, H, is NOT already mapped, mapping home drive
VERBOSE:   In the function to write the event to the log
VERBOSE:   Using the Wscript.shell COM object to write the event
VERBOSE:   Info: Successfully wrote, Info: The drive, H, is NOT already mapped, mapping home drive, to Application log
VERBOSE:   End of function to write the event to the log
VERBOSE: System.__ComObject

0 个答案:

没有答案