Home > Back-end >  Powershell Mapping network drive
Powershell Mapping network drive

Time:08-25

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}

CodePudding user response:

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.

CodePudding user response:

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.

  • Related