Possible issue with Test-Path in Windows forms application

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
PGomersall
Posts: 120
Last visit: Thu Sep 09, 2021 8:46 am

Possible issue with Test-Path in Windows forms application

Post by PGomersall »

Product: PowerShell Studio 2021 (64 Bit)
Build: v5.8.193
OS: Windows 10 Education (64 Bit)
Build: v10.0.22000.0

So I have a Windows forms application that takes information from use and queries a MS SQL database for information.
The application uses the Button Job Start Control Set. The job works fine and Exports returned data to an Excel workbook in a remote share.
The completed job returns at text string with the file path for the Excel book:
$OutFile = "\\servername.sub.domain.com\Shared\F1\F2\F3\etc\csg266-MAT100-07_2021-08-26_09-12AM.xlsx"
There are no spaces in paths, but some underscores as in the above example. The server name is a FQDN.

If I run Start-Process "$OutFile" the Excel workbook opens in Excel correctly.

If I do the following it does not work:
If (Test-Path $OutFile) {Start-Process "$OutFile"} or (Test-Path "$OutFile")
As the If returns False.

However, the following does work:
If ([System.IO.File]::Exists($OutFile)){Start-Process "$OutFile"}
Test-Path with $OutFile = "\\servername.sub.domain.com\Shared\F1\F2\F3\etc\csg266-MAT100-07_2021-08-26_09-12AM.xlsx" works fine in Console 5.1, ISE and PS7.1.4 just not in this PSS application.

I have a work around using System.IO.File but wanted to know if this is a PSS issue and may be specifically related to the type of file?

Regards,
Pete

User avatar
Alexander Riedel
Posts: 7787
Last visit: Thu Oct 21, 2021 11:50 am
Answers: 6
Been upvoted: 11 times

Re: Possible issue with Test-Path in Windows forms application

Post by Alexander Riedel »

AFAIK, Test-Path takes a file object as a parameter. If you want to specify a string you need to use the -path parameter.
PowerShell Studio does not run any different type of PowerShell. It is the same PowerShell. In a console you may have a case of runspace contamination that you do not see in a script though.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
PGomersall
Posts: 120
Last visit: Thu Sep 09, 2021 8:46 am

Re: Possible issue with Test-Path in Windows forms application

Post by PGomersall »

Yes I understand that David, but explicitly adding -Path doesn't make any difference either and therefore doesn't explain why Test-Path -Path $OutFile fails and [System.IO.File]::Exists($OutFile) works, when all I change is the statements in the If evaluation in PSS.

User avatar
Alexander Riedel
Posts: 7787
Last visit: Thu Oct 21, 2021 11:50 am
Answers: 6
Been upvoted: 11 times

Re: Possible issue with Test-Path in Windows forms application

Post by Alexander Riedel »

I will try to track David down and let him know.

I did some searching on this and it seems Test-Path has issues with UNC paths for a lot of folks. I'd suggest to contact the PowerShell peeps.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
PGomersall
Posts: 120
Last visit: Thu Sep 09, 2021 8:46 am

Re: Possible issue with Test-Path in Windows forms application

Post by PGomersall »

Alex, my bad in last post - I meant you not David.

User avatar
Alexander Riedel
Posts: 7787
Last visit: Thu Oct 21, 2021 11:50 am
Answers: 6
Been upvoted: 11 times

Re: Possible issue with Test-Path in Windows forms application

Post by Alexander Riedel »

I have been called worse things :D
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
PGomersall
Posts: 120
Last visit: Thu Sep 09, 2021 8:46 am

Re: Possible issue with Test-Path in Windows forms application

Post by PGomersall »

I am sure Alex ;) , but you haven't addressed my point in the post though.
Maybe - you did - UNC path issues? Maybe it is worse with files as opposed to say a folder?
I'll just use the .Net test.

User avatar
Alexander Riedel
Posts: 7787
Last visit: Thu Oct 21, 2021 11:50 am
Answers: 6
Been upvoted: 11 times

Re: Possible issue with Test-Path in Windows forms application

Post by Alexander Riedel »

I, just as anyone else who seems to struggle with Test-Path failing for some unknown reason, have no idea.
Unfortunately I have no insight in the PowerShell implementation. It is open source now, but unfortunately I do not have the time to dive into that.
I found plenty of complaints online about it failing with UNC paths. Or mapped drives. Or both.
Some say it is worse for folders. Some say it is worse for files.
The consensus seems to be to use [System.IO.File]::Exists, which I would imagine is faster anyway.
Alexander Riedel
SAPIEN Technologies, Inc.