Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何使用powershell 5检查具有凭据的网络文件夹是否存在_Windows_Powershell_Powershell 5.0 - Fatal编程技术网

Windows 如何使用powershell 5检查具有凭据的网络文件夹是否存在

Windows 如何使用powershell 5检查具有凭据的网络文件夹是否存在,windows,powershell,powershell-5.0,Windows,Powershell,Powershell 5.0,假设有一个网络文件夹\\my\u server\my\u root\my\u dir。 要访问此文件夹,这些凭据需要用户名:my\u-doman\my\u-user密码:my\u-password 现在在我的程序中,它首先尝试将网络文件夹映射到本地驱动器。如果存在异常,则认为它不存在文件夹。我认为这不是一个好办法 是否有方法检查此文件夹是否存在,而不尝试映射到本地驱动器?我在找像这样的东西 [System.IO.Path]:存在(\\my\u server\my\u root\my\u dir”

假设有一个网络文件夹
\\my\u server\my\u root\my\u dir
。 要访问此文件夹,这些凭据需要用户名:
my\u-doman\my\u-user
密码:
my\u-password

现在在我的程序中,它首先尝试将网络文件夹映射到本地驱动器。如果存在异常,则认为它不存在文件夹。我认为这不是一个好办法

是否有方法检查此文件夹是否存在,而不尝试映射到本地驱动器?我在找像这样的东西

[System.IO.Path]:存在(\\my\u server\my\u root\my\u dir”、“my\u doman\my\u user”、“my\u password”)

我使用的是
Powershell 5

这就是我现在映射驱动器的方式

try{
    $net = new-object -ComObject WScript.Network
    $net.MapNetworkDrive($free_drive, $network_dir, $false, "domain\user", "password")
}catch{
  Write-host: "folder does not exist"
}
使用:

使用cmdline实用程序的旧方法:

net use \\my_server\my_root\my_dir /user:my_domain\my_user my_password
start \\my_server\my_root\my_dir
对于映射,您可以使用以下方法:

$net = New-Object -comobject Wscript.Network
$net.MapNetworkDrive("Q:","\\my_server\my_root\my_dir",0,"my_domain\my_user","my_password")
要测试路径,可以使用:

Test-Path \\my_server\my_root\my_dir
注意:您将从测试路径得到一个布尔值作为返回


希望有帮助。

假设在执行脚本的计算机上,您能够以要连接到远程共享的用户身份登录,您可以使用
Invoke命令
以另一个用户身份调用
测试路径

$pathExists = Invoke-Command -ComputerName . -Credential $credentials -ScriptBlock {
    Test-Path -Path "\\my_server\my_root\my_dir"
}
if ($pathExists)
{
    # my_dir\ exists
}
else
{
    # my_dir\ is inaccessible/non-existent
}
目前我无法对此进行测试,但我怀疑由于双跳问题,您可能需要将
-Authentication Credssp
作为
调用命令的参数(假设有必要的环境)


当然,如果您正在检查另一个用户下是否存在该目录,以便为将来作为该用户执行的文件系统操作做出决策,那么您需要调用另一批操作,或者在
测试路径
之后包含它们。在这一点上,您最好以通常的方式在备用凭据下连接到共享。这只是作为另一个用户执行和作为另一个用户连接之间的区别,每个用户都有各自的优缺点。

在从部署服务器复制文件之前,我在检查远程服务器上是否存在文件夹和/或文件时遇到了相同的问题。对我来说,什么都不管用,结果是一场巨大的挫折

然后我尝试了这个

$pathExists = Invoke-Command -ComputerName <remoteServerName> -Credential $credentials 
-ScriptBlock {
Test-Path -Path <AbsolutePath> "UNC path does not work here and you must use Absolute 
path.  eg. instead of \\Server use D:\Dir1 or D:\Dir1\File1.bat"
}
if (-not ($pathExists))
{
write-host "Directory or File does not exist"
}
else
{
 write-host "Directory or File exist"
}
$pathExists=Invoke命令-ComputerName-Credential$credentials
-脚本块{
测试路径-路径“UNC路径在此不起作用,您必须使用绝对路径
例如,使用D:\Dir1或D:\Dir1\File1.bat代替\\Server“
}
如果(-not($pathExists))
{
写入主机“目录或文件不存在”
}
其他的
{
写入主机“目录或文件存在”
}

在测试路径中使用绝对路径后,这对我来说效果很好。由于某些原因,UNC路径不工作!希望,这对某人有帮助

您当前如何映射驱动器?您可以使用
New PSDrive-Name p-PSProvider FileSystem-Root“\\my\u server\my\u Root\my\u dir”-凭证“my\u domain\my\u user”
@Bacon:添加到问题中。感谢您的回答!但如果网络文件夹不存在,这些都会失败。我正在寻找一种在映射之前检查文件夹是否确实存在的方法。因为映射可能也会因为其他原因而失败。您可以使用
测试路径
检查这一点。让我更新我的answer@RanadipDutta-使用测试路径的问题在于无法向其传递凭据。这就是为什么需要使用psdrive。。我不认为还有其他选择。应该可以作为另一个用户运行powershell脚本,并在该用户上下文中运行测试路径。
$pathExists = Invoke-Command -ComputerName <remoteServerName> -Credential $credentials 
-ScriptBlock {
Test-Path -Path <AbsolutePath> "UNC path does not work here and you must use Absolute 
path.  eg. instead of \\Server use D:\Dir1 or D:\Dir1\File1.bat"
}
if (-not ($pathExists))
{
write-host "Directory or File does not exist"
}
else
{
 write-host "Directory or File exist"
}