【问题标题】:Get-ChildItem : The network path was not foundGet-ChildItem : 未找到网络路径
【发布时间】:2021-10-26 09:17:27
【问题描述】:

在运行我的 power shell 脚本以从多个服务器获取最大文件时,我遇到了这个错误。谁能帮我解决这个错误。

脚本:

$computers = get-content -Path "D:\Automation_space_check\Servers.txt."
foreach($_ in (Get-WmiObject win32_volume -ComputerName $computer)){
if($_.drivetype -eq 3 -and $_.driveletter -ne $null){
Get-ChildItem "\\$computer\$($_.driveletter.replace(':','$'))" -force -recurse -ErrorAction silentlycontinue | select Name,DirectoryName,@{Name="size";Expression={"{0:N1}" -f($_.length/1mb)}} |
 Sort Size -descending | select -first 10}}

错误:

Get-ChildItem : 未找到网络路径 D:\Automation_space_check\Untitled3.ps1:18 char:1 + Get-ChildItem "\$computer$($_.driveletter.replace(':','$'))" -force ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Get-ChildItem], IOException + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.GetChildItemCommand

Servers.txt

Computer1
computer2
computer3
computer4

【问题讨论】:

    标签: powershell


    【解决方案1】:
    $computers = get-content -Path "D:\Automation_space_check\Servers.txt."
    foreach($computer in  $computers) {
      foreach($drive in (Get-WmiObject win32_volume -ComputerName $computer)) {
        if($drive.drivetype -eq 3 -and $drive.driveletter -ne $null){
            Get-ChildItem "\\$computer\$($_.driveletter.replace(':','$'))" -force -recurse -ErrorAction silentlycontinue | select Name,DirectoryName,@{Name="size";Expression={"{0:N1}" -f($_.length/1mb)}} |
            Sort Size -descending | select -first 10
         }
      }
    }
    

    【讨论】:

    • 现在出现另一个错误,例如:您不能在空值表达式上调用方法。在 D:\Automation_space_check\Untitled3.ps1:19 char:39 + ... Get-ChildItem "\\$computers\$($_.driveletter.replace(':','$'))" -forc .. . + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
    • $computers 是一个集合。如果您遵循示例,则路径使用\\$computer。我建议您在代码上设置断点并检查各种变量的值以找到为空的变量。您可能必须包含一些 if 语句甚至 try {} catch {} 块来处理一些错误。
    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多