1

Lest say there is a network folder \\my_server\my_root\my_dir. To access this folder these credentials required username: my_doman\my_user password: my_password.

Now in my program first it try to map network folder to a local drive. If there is a exception, it consider as folder not exist. I think this is not a good way.

Is there a way to check this folder exist without try to map to a local drive? I'm looking for something like

[System.IO.Path]::Exist("\\my_server\my_root\my_dir","my_doman\my_user","my_password")

I'm using Powershell 5

This is how I map the drive now

try{
    $net = new-object -ComObject WScript.Network
    $net.MapNetworkDrive($free_drive, $network_dir, $false, "domain\user", "password")
}catch{
  Write-host: "folder does not exist"
}
3
  • How are you mapping a drive currently? Commented Apr 24, 2017 at 4:45
  • You can use New-PSDrive -Name P -PSProvider FileSystem -Root "\\my_server\my_root\my_dir" -Credential "my_domain\my_user" Commented Apr 24, 2017 at 4:57
  • @Bacon: added to the question. Commented Apr 24, 2017 at 6:21

3 Answers 3

1

Using New-PSDrive:

New-PSDrive -Name Q -PSProvider FileSystem -Root \\my_server\my_root\my_dir -Credential my_domain\my_user -Persist

Old School method using cmdline utility:

net use \\my_server\my_root\my_dir /user:my_domain\my_user my_password
start \\my_server\my_root\my_dir

For mapping you can use this:

$net = New-Object -comobject Wscript.Network
$net.MapNetworkDrive("Q:","\\my_server\my_root\my_dir",0,"my_domain\my_user","my_password")

For testing the path, you can use:

Test-Path \\my_server\my_root\my_dir

Note:You will get a boolean value in return from the test path.

Hope it helps.

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

7 Comments

Thank you for the answer! but these all fail if network folder not exist. I'm looking for a way to check folder really exist before mapping. because mapping could be fail because of other reasons too.
You can check that using test-path. Let me update my answer
@RanadipDutta - The problem using Test-Path is that you can't pass credentials to it.
Thats why you need to use psdrive.. I dont think of any other alternative at this point.
It should be possible to just run a powershell script as another user and run Test-Path in that users context.
|
0

Assuming that on the machine executing the script you're able to log in as the user under which you want to connect to the remote share, you could use Invoke-Command to invoke Test-Path as another user:

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

I'm not in a position to test this at present but I suspect you may need to include -Authentication Credssp as a parameter to Invoke-Command (assuming the necessary environment for that is in place) due to the double-hop problem.

Of course, if you're checking for the existence of that directory under another user in order to make decisions for future filesystem operations to be executed as that same user, you would then need to Invoke-Command another batch of operations or include them after Test-Path. At that point you might be better off connecting to the share under alternate credentials in the usual way. It's just the difference between executing as another user and connecting as another user, each with their own pros and cons.

Comments

0

I was having the same problem checking the folder and/or file existence on a remote server before copying the file from deployment server. Nothing worked for me and ended up in a huge frustration.

I then tried this...

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

This worked just fine for me after using the absolute path in the Test-Path. For some reason the UNC path does not work! Hope, this helps someone!

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.