powershell - 检查文件在远程计算机上是否存在。如果没有,请复制

标签 powershell powershell-2.0

我对Powershell还是很陌生,想知道是否有人可以指出正确的方向。

我一直在寻找一个脚本,可以帮助我实现以下目标:

  • 我有一个需要推送到远程计算机的文件。
  • 我有一个远程计算机名称列表,我想检查一下该文件是否存在。
  • 如果文件存在并且大小和日期相同,则什么也不做。
  • 如果文件不存在,或日期/大小不同,则复制文件。
  • 我也可能想写某种日志/文本文件来指示状态,例如“PC1-未找到文件。正在复制”或“PC2-找不到精确文件,未复制任何文件”
  • 最佳答案

    有遵循您的逻辑的powershell脚本。目标可以是UNC路径。

    $source =  "C:\Tmp\Test.txt"
    $destination = "C:\Tmp\Destination\Test.txt"
    
    $TestPath = Test-Path $destination
    IF (!$TestPath)
    {Copy-Item $source $destination -Verbose
    PC1 - no file found. Copying}
    ELSE
    {
    IF (((Get-ChildItem $source).Length -ne (Get-ChildItem $destination).Length) -or ((Get-ChildItem $source).LastWriteTime -ne (Get-ChildItem $destination).LastWriteTime))
    {Copy-Item $source $destination -Force -Verbose}
    ELSE
    {"PC2 - exact file found, nothing copied"}
    }
    

    关于powershell - 检查文件在远程计算机上是否存在。如果没有,请复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19140820/

    相关文章:

    html - 如何将控制台应用程序(如 MSBuild 或 PowerShell)的彩色输出转换为保留颜色的 HTML 格式?

    powershell - 无法作为azure webjob执行powershell 5.0脚本

    powershell - 带括号的环境变量导致 “unexpected token”错误

    powershell - 选择对象省略了选择字符串Powershell的输出

    powershell - 在 Windows 任务计划程序中运行 Powershell 脚本(Windows 7)

    PowerShell:检测脚本函数中的错误

    javascript - 检查应用程序是否已经运行或重新启动

    powershell - 如何为要从提示符调用的 PowerShell 脚本设置别名?

    powershell - 如何设置 PowerShell 脚本运行的时间限制?

    python - 有没有更好的方法利用 redshift、python 和 powershell 来自动化我的报告?