Microsoft Virtualization Discussions

Using Test-Path to check files on filer

drwoodberry

Does anyone know a way to use Test-path to see if a file lives on the filer? I think I someone need to use Read-NaFile, but I cannot quite figure it out. If the file is not there when reading, it throws an error. I would like to surpress the error, but set a variable to false.

Any help would be great.

Thanks in advance.

8 REPLIES 8

cknight
Using Test-Path would require a PowerShell provider, which isn't included in Toolkit 1.2.

But Toolkit 1.3 does include a PowerShell provider!

Try this:


PS C:\> Connect-NaController <name>

PS C:\> Mount-NaController

PS C:\> Test-Path <name>:/etc/host

False

PS C:\> Test-Path <name>:/etc/hosts

True

drwoodberry

I did not see tool kit 1.3. Is the available to the general public. I like the try catch method, but this mount cmdlet is nice. I have used the try,catch for other error checking and has proven invaluable.

cknight

Yes, Toolkit 1.3 is available on the downloads page; it merely replaces the older zip file.  You must be logged into the community with a NOW login ID to see that.

paleon

You can use Test-Path if you access the NetApp via CIFS.  Below is some code to test for the existence of the file...

# use the net.exe application to connect to \\filer\c$ using specified credentials

C:\Windows\System32\net.exe use \\filer\c$ /user:domain\username password

#test for existence of the hosts file on the NetApp

$result = Test-Path \\filer\c$\etc\hosts

cknight

You can use Read-NaFile in a try/catch block to see if a file exists:

PS C:\> try { Read-NaFile /vol/vol0/etc/host -Length 1 -Offset 0 } catch { Write-Host "Not found" }
Not found

Using Test-Path would require a PowerShell provider, which isn't included in Toolkit 1.2.

drwoodberry

I guess that is something I would like to see in the toolkit then.   

That process is not quite working for me. If the file is there, I would like to set a variable to true, otherwise to false. I tried it like this:

try{Read-NaFile /vol/vol0/etc/info}

catch{$configured = $false}

No matter what, I get $configured as false when this runs. Maybe I missed something, but I thought that if an exception was thrown, it only then did what was in the catch.

cknight

You still have to set the variable to true if the file is present.  This works for me:

PS C:\> try { Read-NaFile /vol/vol0/etc/hosts -Length 1 -Offset 0 | Out-Null; $configured = $true } catch { $configured = $false }

drwoodberry

Thanks so much. When I used your first solution to and checked the value of configured it always returned false, but the program ran correctly. I am going to test your second solution shortly as well. Thanks so much for all your help.

Announcements
NetApp on Discord Image

We're on Discord, are you?

Live Chat, Watch Parties, and More!

Explore Banner

Meet Explore, NetApp’s digital sales platform

Engage digitally throughout the sales process, from product discovery to configuration, and handle all your post-purchase needs.

NetApp Insights to Action
I2A Banner
Public