复制文件时收到错误

复制文件时收到错误

我有一个针对 1K 系统运行的脚本。在脚本中,我将文件复制到系统的临时文件夹中。当我使用 xcopy 时,1K 系统中的 1 或 2 个系统会返回“驱动器规范无效”错误。我尝试复制项目,但收到“未找到网络路径”错误。我运行了 dir 和 get-childitem,但收到“找不到路径‘\computername\c$\temp’,因为它不存在”错误。我可以在系统上输入 pssession 并转到该文件夹​​。我可以通过 rdp 进入它,然后直接将文件从共享 (gui) 放入文件夹中。当我输入 pssession 并尝试从 pssession 中复制它时,计算机的行为就像文件的共享驱动器路径不存在一样。它返回“找不到路径‘\sharedrive\folder\folder\file.ps1’,因为它不存在”错误。系统是运行 powershell 版本 5.1 的 Win 10。只是几个系统导致了问题。

有人见过这样的问题吗?


while($true){

    $script = "\\path\folder\folder\SystemInfo.ps1"
    $local = (get-item "$script").fullname.split("\")[-2]+"\"+(get-item "$script").fullname.split("\")[-1]

    $computers = (Get-ADComputer -Filter * -SearchBase "OU=Computers OU,OU=OU Container,DC=com" | Where-Object {$_.enabled -eq $true}).name | sort
    $computers += (Get-ADComputer -Filter * -SearchBase "OU=Computers OU,OU=OU Container,DC=com" | Where-Object {$_.DistinguishedName -like "*server related*" -and $_.enabled -eq $true}).name | sort
    $count = 0


    $computers | ForEach-Object {
        $alive = Test-Connection -ComputerName $_ -Quiet -Count 1 -ErrorAction SilentlyContinue
        $checked = $null
        $checked = (ls "\\path\folder\folder\Software_List\$_.csv").LastWriteTime
        $Date = (get-date).AddDays(-1)

        if ($alive){

            $count += 1

            if($checked -lt $Date -or $checked -eq $null){

                write-host "$_ Online and info needs to be updated`t" -NoNewline -ForegroundColor "Green" -BackgroundColor "Black"

                $copy = xcopy /y /f $script "\\$_\c$\temp\$($local.split("\")[0])\"
                #$copy
                if($copy -notlike "0*")
                    {
                    $psexec = psexec -s -h -d \\$_  powershell.exe C:\temp\$local -accepteula

                    if($error[0] -like "*with process ID*")
                        {
                        write-host "Success`t" -NoNewline -ForegroundColor "DarkGreen" -BackgroundColor "Black"
                        ""
                        }

                    else
                        {
                        write-host "$_ Copy Failure`t" -NoNewline -ForegroundColor "DarkMagenta" -BackgroundColor "Black"
                        ""
                        }
                    }
                else
                    {
                    write-host "$_ had a Copy error`t" -NoNewline -ForegroundColor "DarkMagenta" -BackgroundColor "Black"
                    ""
                    "$_" >> "\\path\folder\folder\Invalid_Drive.txt"
                    }

                }
            else
                {
                write-host "$_ Online, but info does not need to be updated`t" -NoNewline -ForegroundColor "Yellow" -BackgroundColor "Black"
                ""
                }

        }

        else
            {
            write-host "$_ is offline`t" -NoNewline -ForegroundColor "Magenta" -BackgroundColor "Black"
            ""
            }

    }
    write-host "$count systems are online"
    Start-Sleep -Seconds 3600
}

答案1

您能发布脚本和收到的错误吗?此外,xcopy 不是 PowerShell 命令,它是 Windows 附带的复制工具。如果您想使用 PowerShell cmdlet 进行复制,则应使用:

复制项目

或类似如下

复制项目 .\documents\ -目标 .\my-file-$(Get-Date -format "yyyy_MM_dd_hh_mm_ss")

相关内容