In the environment that I work in, sometimes the user’s Active Directory (AD) home drive is not mapped when they logon because they are not connected to the network. Later, when they do connect to the network over VPN they need to be able to map their AD home drive. I’ve created a script in PowerShell to do that but, on Windows 10 I get the error, " Object reference not set to an instance of an object." And the script fails to map the user’s home drive. If I run the same script on Windows 7 it works without issue. Any help with this issue would be greatly appreciated.
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 .\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 }