Powershell "UNC paths are not supported" 但我正在使用 Push-Location

Powershell "UNC paths are not supported" but I'm using Push-Location

我是 运行 一个 powershell 命令行程序,在我们的 \servername\files 文件夹上作为 运行 文件夹工作。即使我使用 Push-Location(这是我在谷歌搜索时找到的唯一解决方案),我也会收到 "UNC paths are not supported" 错误。给我这个错误的最简单的代码如下:

Push-Location \servername\files
cmd.exe \c ping 10.1.10.10
Pop-Location

Push-Location 会保存你所在的位置,但由于你没有将位置更改为 non-UNC 位置来执行 cmd.exe 程序,它会抱怨。

只需在 cmd.exe 调用之前放置一个 Set-Location 以确保您不在 UNC 路径上。类似于:

Set-Location $env:APPDATA

会起作用。

PetSerAl 是正确的,您从 cmd 而非 Powershell 获得了此响应。如果您先配置了 PSDrive,这会起作用,但我不知道这对您的用例是否非常有效:

New-PSDrive -Name S -PSProvider FileSystem -Root \servername\files -Persist
Push-Location
Set-Location S:\
cmd.exe /c ping 10.1.1.1
Pop-Location
Get-PSDrive S | Remove-PSDrive

*-位置按设计工作,我将在下面进行演示。

演示---

PS C:\Scripts> (Get-CimInstance -ClassName CIM_OperatingSystem).Caption
Microsoft Windows Server 2012 R2 Standard

PS C:\Scripts> $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
4      0      -1     -1      



PS C:\Scripts> Test-Path -Path '\FileServer01\Data'
True

PS C:\Scripts> $pwd

Path
----
C:\Scripts



PS C:\Scripts> Push-Location -Path '\FileServer01\Data'

PS Microsoft.PowerShell.Core\FileSystem::\FileServer01\Data> $pwd

Path
----
Microsoft.PowerShell.Core\FileSystem::\FileServer01\Data 

至于这个...

cmd.exe \c ping 10.1.10.10

... 不是来自 UNC 的 运行ning,而是来自您的主机的 运行ning,您推送到共享的事实毫无意义。

如果共享中有您想要 运行 的 exe,则没有理由将其更改为 运行 的位置。只需通过 PowerShell 将定义的方法用于 运行 外部命令(请参阅下面的 link),并仅使用 UNC 路径而无需先切换到那里。即使 运行 宁一个远程 exe 仍然意味着正在使用来自你的主机的 cmd.exe。

PS Microsoft.PowerShell.Core\FileSystem::\FileServer01\Data> Pop-Location

PS C:\Scripts> $pwd

Path
----
C:\Scripts



PS C:\Scripts> 

Running external commands, always require special consideration.

PowerShell: Running Executables https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

Solve Problems with External Command Lines in PowerShell https://devblogs.microsoft.com/scripting/solve-problems-with-external-command-lines-in-powershell

Top 5 tips for running external commands in Powershell https://powershelleverydayfaq.blogspot.com/2012/04/top-5-tips-for-running-external.html

Using Windows PowerShell to run old command line tools (and their weirdest parameters) https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters

Execution of external commands in PowerShell done right https://mnaoumov.wordpress.com/2015/01/11/execution-of-external-commands-in-powershell-done-right https://mnaoumov.wordpress.com/2015/03/31/execution-of-external-commands-native-applications-in-powershell-done-right-part-2 https://mnaoumov.wordpress.com/2015/04/05/execution-of-external-commands-native-applications-in-powershell-done-right-part-3

Quoting specifics

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules https://trevorsullivan.net/2016/07/20/powershell-quoting