1

I have a module of PS commands written in C#. It communicates with another DLL, that eventually writes a file.

Let's assume Powershell from the Windows Terminal. It starts in C:\Users\<myname>\.

Export-Yadda -filename .\bla.txt will create a file bla.txt in C:\Users\<myname>\

When I cd to C:\users\<myname>\foo\ and repeat the command, it will still write to C:\users\<myname>\

This has me a at a bit of a loss.

The part of the code that receives the bla.txt string could not be simpler, it is just:

private string _path;
public string OutputPath
{
    get { return _path; }
    set { _path = value; }
}

Magic is being applied to bla.txt to make it a FQPN. How do I make it reflect the path the command was issued from? So that I can call it from say C:\<whatever> or E:\temp\?

Rno
  • 784
  • 1
  • 6
  • 16
  • 1
    There's no magic. PowerShell's concept of "current directory" doesn't coincide with the *process*'s because the current directory in PowerShell can be non-file system locations, such as the registry. Your module needs to get the current directory from PowerShell. – madreflection Feb 26 '22 at 23:29
  • 1
    .NET's working directory usually differs from PowerShell's, so you should always pass _full, file-system-native_ paths to .NET method calls. Use [`Convert-Path`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/convert-path) to convert a relative path to a full file-system-native one, assuming it already exists. To specify a file to be created in PowerShell's current location, use something like `"$PWD\file.txt"`, except from PS-specific drives. For more information, see [this answer](https://stackoverflow.com/a/57791227/45375) to the linked duplicate. – mklement0 Feb 27 '22 at 00:03
  • Thank you for your comments. The point with the helpful duplicate link is, that in my case, the string passes directly to C# code, so none of the PS trickery applies I think. – Rno Feb 28 '22 at 00:50
  • The answer that I unfortunately cannot provide I found in [this repository](https://github.com/fireflycons/PSCloudFormation/blob/57f0dc0a7cdcb54dc95708e2bbd44a7541adae20/src/Firefly.PSCloudFormation/PSPathResolver.cs). The essence is using the `SessionState` from the `PSCmdlet` class. – Rno Mar 03 '22 at 23:51

0 Answers0