Microsoft Virtualization Discussions

Using Test-Path to check files on filer

drwoodberry
6,749 Views

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
6,750 Views

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
6,750 Views

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
6,750 Views

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
6,750 Views

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.

paleon
6,750 Views

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
6,750 Views
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
6,750 Views

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
6,750 Views

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.

Public