PowerShell Test-Path returns False when testing a network share

The user has the appropriate permissions to read the shared resource and the shared resource map properly. This issue only occurs in PowerShell v2.0.

If I delete all mapped drives, reassign one drive and run Test-Path:

net use /delete * /y net use G: \\somefileserver\share Test-Path G:\ 

Test-Path returns False, although the drive is clearly mapped, and I can access it through Windows Explorer.

If I close the PowerShell session and open a new session, Test-Path will return True, as it should. Any ideas on (A): what could be causing this, and how do I get this to work properly? or (B): is there any other way to check for a network drive besides Test-Path? I am trying to write a login script to map users to network drives, and this infuriates me.

+8
source share
6 answers

If the Test-Path cmdlet is really broken, then I would suggest using the Exists() method in the System.IO.Directory .NET class.

 [System.IO.Directory]::Exists('G:\'); # This returns $true for me 
+9
source

I think I found the answer with this:

 Test-Path $('filesystem::\\Server\share$\install.wim') 
+9
source

To verify UNC, use:

 [bool]([System.Uri]$path).IsUnc 
+6
source

For me, the only way to fix this is to use the full UNC, not the share name.

Therefore, instead of \\server\share I used \\server\c$\sharedir .

Another problem I encountered was that when using Import-Module sqlps I had to make sure the CD back in the file system and then the PowerShell commands worked fine.

0
source

I had the same problem trying two network paths like this:

 $verif_X = Test-Path "X:\" $verif_S = Test-Path "S:\" if($verif_X -and $verif_S){ Write-Host "X: and S: network paths found" } else{ Write-Host "Not all network paths found" } 

... At first I always got> "Not all network paths were found", which seemed really strange to me ...

Then I tried putting these paths in simple quotation marks (') and looked for an existing folder in these two network paths, for example:

 $verif_X = Test-Path 'X:\ISOs' $verif_S = Test-Path 'S:\Scripts' 

And then

 if($verif_X -and $verif_S){[..]} 

condition accepted ..

I think the Test-path cmdlet is a bit buggy, since we only enter the first letter of the network path ... but this way I no longer get errors ...

0
source

SHORT ANSWER

if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} If Test-Path failed unexpectedly, make sure that SMB2 = 1 (or another SMB parameter) is set on both the client and the destination server.

MORE INFORMATION

IMPORTANT NOTE SMB: both the current system and the target system must have at least a common SMB protocol that is allowed to pass the Test-Path successfully. (It is strongly recommended that you use SMB2 or later.) For example, if SMB1 + is disabled for the target and SMB2 is disabled and only SMB2 is enabled on the client, the above logic will return the message "Server not found ...". This baffled me until I finally checked my target server (Win7) and found that it had SMB2 = 0 (disabled) and there was no entry for SMB1 (enabled by default). I fixed it by setting SMB2 = 1 for the article below.

SMB OS details and script details: https://support.microsoft.com/en-us/help/2696547/detect-enable-disable-smbv1-smbv2-smbv3-in-windows-and-windows-server

Exposure: Win8 / Win20012

 Detect: Get-SmbServerConfiguration | Select EnableSMB1Protocol Disable: Set-SmbServerConfiguration -EnableSMB1Protocol $false Enable: Set-SmbServerConfiguration -EnableSMB1Protocol $true 

Exposure: Win7 / Win2008R2Server

 Detect: Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters | ForEach-Object {Get-ItemProperty $_.pspath} Default configuration = Enabled (No registry key is created), so no SMB1 value will be returned Disable: Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" SMB1 -Type DWORD -Value 0 –Force Enable: Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" SMB1 -Type DWORD -Value 1 –Force 

Code example: Copy-Item folder (recursive), only if the target server exists

 $scriptRootPath="." $scriptToolsPath="$scriptRootPath\Tools" $targetServerList="$scriptToolsPath\DeployServerList-INT-POC.txt" #DeployServerList-INT.txt, DeployServerList-QA.txt, DeployServerList-PROD.txt $stageTargetDrive="c" $stageFolderPath="$stageTargetDrive$\staging" $VerbosePreference="Continue" #"SilentlyContinue" (default), "Continue", "Stop", "Inquire" $InformationPreference="Continue" Write-Host "Getting list of servers from $targetServerList to stage deployment files to..." $serverList = (get-content "$targetServerList") Write-Verbose "scriptToolsPath=$scriptToolsPath" Write-Verbose "serverlist=$serverList" Write-Verbose "stageFolderPath=$StageFolderPath" Write-Host -Separator "-" Read-Host -Prompt "READY TO STAGE FILES: Check info above, then press Enter to continue (or Ctrl+C to exit)." Write-Host "-------------------------------------------------" Write-Host "Staging files to $stageFolderPath on each target server..." foreach ($server in $serverlist) { # Input validation if([string]::IsNullOrWhiteSpace($server)) {continue} if($server.StartsWith("#")) {Write-Verbose "Comment skipped: $server"; continue} # Skip line if line begins with hashtag comment char Write-Verbose "Testing filesystem access to $server..." if(-Not(Test-Path "filesystem::\\$server\$stageTargetDrive$")) {Write-Error "Server not found: $server"; continue} # TIP: If Test-Path returns false unexpectedly then check if SMB2 is enabled on target server, check SMB1 disabled for both src and target servers. Write-Verbose "Staging files to $server..." Copy-Item ".\" -Destination "\\$server\$stageFolderPath" -Recurse -Force -ErrorAction Continue Write-Information "Files staged on $server." } 
0
source

All Articles