【问题标题】:path not found?找不到路径?
【发布时间】:2013-02-26 18:56:07
【问题描述】:

我正在尝试使用 Powershell 3.0 对 multilpe 服务器运行以下命令,但由于某种原因,我发现路径不存在......即使它存在?

有什么想法吗?

代码:

clear
$computer = Get-Content -path c:\temp\servers.txt

foreach ($computer1 in $computer){

Write-Host $computer1
Get-Content -Path '\\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log' -Tail 10

}

错误:

SV191267 获取内容:找不到路径 '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log',因为它没有 存在。在 C:\Users\gaachm5\AppData\Local\Temp\e4651274-dcab-4a87-95a6-0f11437a7187.ps1:7 字符:1 + 获取内容-路径'\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rs ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (\$computer1\C$...c\RSCD\rscd.log:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

SV193936 获取内容:找不到路径 '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log',因为它没有 存在。在 C:\Users\gaachm5\AppData\Local\Temp\e4651274-dcab-4a87-95a6-0f11437a7187.ps1:7 字符:1 + 获取内容 -Path '\$computer1\C$\Program Files\BMC Software\BladeLogic\RSCD\rs ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (\$computer1\C$...c\RSCD\rscd.log:String) [Get-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

【问题讨论】:

    标签: powershell powershell-3.0


    【解决方案1】:

    尝试:

    "\\$computer1\C`$\Program Files\BMC Software\BladeLogic\RSCD\rscd.log" -Tail 10
    

    在单引号' 中,变量不会被扩展,而是被当作$computer1 处理,$admin share$ 必须用反引号转义`

    【讨论】:

    • 正如@HungryHippos 的回答中所指出的,管理员共享的美元符号不需要转义,因为后面跟着一个 \ 字符,但我认为这是一个很好的做法,将美元符号转义为“ string" 当它需要作为文字时。
    【解决方案2】:

    找不到的路径前面只有一个\,UNC共享必须以两个\开头。我可以从您的代码中看到,您似乎在为两个 \ 加上前缀,但看起来并没有被继承。

    如前所述,我会使用双引号而不是单引号,如果你这样做了,那么你也不应该需要转义 $ 字符。

    假设本地计算机的 C:\temp 文件夹中存在一个名为 file.txt 的文件。

    这失败了:

    $computer1 = "localhost"
    Test-Path '\\$computer1\c$\temp\file.txt'
    

    这行得通:

    $computer1 = "localhost"
    Test-Path "\\$computer1\c$\Temp\file.txt"
    

    【讨论】:

    • 我已经编辑了您的答案以修复第一个示例中的引号(这两个示例是相同的,除了第二个示例中的大写字母 T :))
    • 我已经再次编辑它以实际工作。不是$computer1 需要双引号,而是Test-Path 字符串需要扩展一个变量:-)
    猜你喜欢
    • 1970-01-01
    • 2019-06-04
    • 2013-05-27
    • 2013-02-26
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多