1

I use the below logic in my Powershell script to map a network drive to read the installation logs and based on the Installation status, it would validate if the installation was successful and I unmap the drive later on.

However I'm seeing errors recently. I'm not sure what is that I'm missing in my script.

New-PSDrive : The local device name has a remembered connection to another network resource At C:\Windows\TEMP\jenkins3036451124142125854.ps1:31 char:1 
+ New-PSDrive -Name M -PSProvider FileSystem -Root "${LogFilePath}" -Cr ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : InvalidOperation: (M:PSDriveInfo) [New-PSDrive], Win32Exception 
    + FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Com mands.NewPSDriveCommand Set-Location : Cannot find drive. A drive with the name 'M' does not exist. At line:1 char:1
     
+ Set-Location $MyInvocation.MyCommand.Name 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : ObjectNotFound: (M:String) [Set-Location], Drive NotFoundException 
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetL ocationCommand Get-ChildItem : Cannot find drive. A drive with the name 'M' does not exist. At C:\Windows\TEMP\jenkins3036451124142125854.ps1:35 char:20 
    
+ ${LatestLogFile} = Get-ChildItem M:\ | Sort CreationTime -Descending ... 
+ ~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (M:String) [Get-ChildItem], Driv eNotFoundException 
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetC hildItemCommand ---------- latest log file is ----------- Get-Content : Cannot find drive. A drive with the name 'M' does not exist. At C:\Windows\TEMP\jenkins3036451124142125854.ps1:39 char:22 

+ $Installer_LogFile = Get-Content -LiteralPath M:\${LatestLogFile} 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : ObjectNotFound: (M:String) [Get-Content], DriveN otFoundException + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.GetC ontentCommand
# Logic to read the log file and verify
${LogFilePath} = “\\inv-r4-bvt-21\c$\FhirInstalls\$Source\installer\logs”
$User = "cor\inbuildu"
$PWord = ConvertTo-SecureString -String "******" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
New-PSDrive –Name M –PSProvider FileSystem –Root "${LogFilePath}" -Credential $Credential –Persist -Scope Global

M:
${LatestLogFile} = Get-ChildItem M:\ | Sort CreationTime -Descending | Select -First 1
Write-Output "---------- latest log file is -----------"
Write-Output ${LatestLogFile}

$Installer_LogFile = Get-Content -LiteralPath M:\${LatestLogFile}
$Install_Complete_Message = $Installer_LogFile | %{$_ -match "STEP: Install of FHIR is complete!"}
$Install_Complete_Message_Components = $Installer_LogFile | %{$_ -match "STEP: Install of selected component\(s\) is complete!"}

if (($Install_Complete_Message -contains $true) -or ($Install_Complete_Message_Components -contains $true))
{
    Write-Host "Fhir API installed succesfully!"
} else {
    Write-Host "Failed to install Fhir API!!"
    $LastExitCode = 1
    exit $LastExitCode 
}
C:
Remove-PSDrive M

if ($RemoteSession){remove-pssession $RemoteSession}
3
  • I don't see a question here. Commented Aug 24, 2022 at 14:39
  • Cool for you, but what is your question? Commented Aug 24, 2022 at 14:39
  • 1
    I have edited the error message, I was so eager that i hit the Enter button, without finishing my error message. Commented Aug 24, 2022 at 14:40

2 Answers 2

1

As long as you only work with PowerShell commands to access the network drive, you can create a temporary PSDrive with local scope (the default). This avoids the problem of "remembered" connections, as the PSDrive is only known to PowerShell and only as long as the scope, where it was defined, exists. After that it will be removed automatically (you can explicitly remove it as well).

In addition, you don't need to come up with a free drive letter. You can give any name to a temporary PSDrive, my recommendation is to use a GUID as part of the name, to avoid any conflicts with other code that may create a PSDrive.

# Create a unique PSDrive name
$psDrvName = "LogDrive-$(New-Guid)"

# Removed "–Persist -Scope Global" to make it a temporary PSDrive with local scope
$null = New-PSDrive –Name $psDrvName –PSProvider FileSystem –Root $LogFilePath -Credential $Credential

${LatestLogFile} = Get-ChildItem ${psDrvName}:\ | Sort CreationTime -Descending | Select -First 1

# ... and so on - just replace "M:\" by "${psDrvName}:\" everywhere

# Optionally remove the PSDrive now. Otherwise it will be removed automatically
# when the scope ends, i. e. when the current function or script returns.
Remove-PSDrive -Name $psDrvName

Note that curly braces {} around the psDrvName variable name are mandatory, when it is used as part of a path, to ensure that : is not interpreted as part of the variable name.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you the solution. Using a unique PSDrive name solved the issue of remembered connection. However now I see Object reference error for Remove-PSDrive : Object reference not set to an instance of an object I tried giving Remove-PSDrive -Name ${psDrvName}
@g90uagk Are you creating PSDrive in a function and try to remove it outside of function? Then the PSDrive has already been removed automatically. You may check like this: if( Test-Path ${psDrvName}:\ ) { Remove-PSDrive -Name $psDrvName }
Yes my bad, got that inside the function and it works just fine. Thanks a ton for your inputs. Appreciate you taking time to help me !
Just out of curiosity, I see this error sometimes . Any idea why would i be seeing this No valid sessions were specified. Ensure you provide valid sessions that are in the Opened state and are available to run commands.
@g90uagk Sorry, no idea.
1

Per the 1st error message drive M: is already attached to: New-PSDrive : The local device name has a remembered connection to another network resource At C:\Windows\TEMP\jenkins3036451124142125854.ps1:31 char:1

You need to use Test-Path to see if the drive letter you are trying to assign to the share is already in use and then process accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.