8

The user has appropriate permissions to read the share, and the share maps properly. This issue seems to happen only in PowerShell v2.0.

If I remove all mapped drives, remap one drive, and run Test-Path:

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

Test-Path returns False, even though 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 returns True, as it should. Any ideas on, (A): what could be causing this, and how do I make it work properly? or (B): Is there another way to test for the existence of a network drive besides Test-Path? I am attempting to write a logon script to map user's network drives and this behavior is driving me insane.

4

6 回答 6

14

我确实认为我找到了答案:

Test-Path $('filesystem::\\Server\share$\install.wim')
于 2017-04-10T13:52:22.203 回答
11

如果Test-Pathcmdlet 确实有问题,那么我建议在.NET 类上使用该Exists()方法。System.IO.Directory

[System.IO.Directory]::Exists('G:\'); # This returns $true for me
于 2014-03-14T02:22:09.393 回答
7

要验证 UNC,请使用:

[bool]([System.Uri]$path).IsUnc
于 2014-11-19T13:28:45.803 回答
1

我在尝试两个这样的网络路径时遇到了完全相同的问题:

$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"
}

...起初,我总是得到>“未找到所有网络路径”,这对我来说似乎真的很奇怪...

然后我尝试将这些路径放在简单的引号 (') 中,并在这两个网络路径中搜索现有文件夹,如下所示:

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

接着

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

接受条件。。

我认为 Test-path cmdlet 有点错误,因为我们只输入网络路径的第一个字母......但是像这样我不再收到错误......

于 2017-11-07T15:28:42.060 回答
0

简短的回答

if(-Not(Test-Path "filesystem::\\$server\c$")) {Write-Error "Server not found: $server"; continue} 如果测试路径意外失败,请确保在客户端和目标服务器上都设置了 SMB2=1(或其他 SMB 设置) 。

更多信息

重要 SMB 注意:当前系统和目标系统都必须至少启用一个通用 SMB 协议才能使测试路径成功。(强烈建议使用 SMB2 或更高版本。)例如,如果目标启用了 SMB1 + 禁用了 SMB2,并且客户端仅启用了 SMB2,则上述逻辑将返回"Server not found..."。这让我偏离了轨道,直到我最终检查了我的目标服务器(Win7)并发现它有 SMB2=0(禁用)并且没有 SMB1 条目(默认启用)。我通过在下面的每篇文章中设置 SMB2=1 来修复。

SMB 操作系统特定和脚本详细信息:https: //support.microsoft.com/en-us/help/2696547/detect-enable-disable-smbv1-smbv2-smbv3-in-windows-and-windows-server

摘录:Win8/Win20012

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

摘录: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

代码示例:仅在目标服务器存在时复制项目文件夹(递归)

$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."
} 
于 2019-05-14T23:33:16.613 回答
0

对我来说,修复它的唯一方法是使用完整的 UNC,而不是共享名称。

所以代替\\server\share我使用\\server\c$\sharedir.

我遇到的另一个问题是在使用时Import-Module sqlps,我必须确保CD回到文件系统,然后 PowerShell 命令才能正常工作。

于 2017-03-01T14:02:19.263 回答