如何在 Cmdlet 中获取当前工作目录

Hem*_*era 5 c# powershell cmdlet

我正在用 C# 为 PowerShell 编写一个 Cmdlet。我正在继承 aCmdlet而不是 a PSCmdlet

有没有办法从 PowerShell 获取当前目录?我可以PSCmdlet使用 using这样做GetVariableValue("pwd")。但是在 Cmd 类中我没有那个可用。

Environment.CurrentDiretory 将我指向 powershell 启动的路径,而不是 PowerShell 本身当前所在的位置。

编辑

例子:

我通过 - say - 启动powershell powershell_ise.exe。它在C:\Windows\System32\WindowsPowerShell\v1.0. 然后我使用更改路径cd c:\my\folder并运行我的命令Do-Something。在“Do-Something”(C# 端)的实现中,我希望能够检索当前路径 => c:\my\folder

如果可能,我想避免使用PSCmdlet.

Mar*_*rco 4

我开始于C:\Users\<myusername>. 如果我知道输入cd..我在C:\Users\

输入(Get-Location).Path返回C:\Users。这就是你想要的,不是吗?

或者尝试:

WriteObject(this.SessionState.Path.CurrentFileSystemLocation);
Run Code Online (Sandbox Code Playgroud)

参考:如何在 PowerShell cmdlet 中获取当前目录?