代码之家  ›  专栏  ›  技术社区  ›  Suncatcher

在robocopy期间遍历samba共享并排除一些

  •  0
  • Suncatcher  · 技术社区  · 8 年前

    要将远程共享与本地文件夹同步,我使用PS脚本

    $Path = Split-Path "\\192.168.1.100\asuscloud\*"
    $Dest = "E:\back_fold\asuscloud\"
    robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL
    

    要排除某些子文件夹,可以使用 /XD 转换

    $Path = Split-Path "\\192.168.1.100\asuscloud\*"
    $Dest = "E:\back_fold\asuscloud"
    robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL /XD "\\192.168.1.100\asuscloud\subdir"
    

    但是,此语法不适用于此主机的主机和共享

    $Path = Split-Path "\\192.168.1.100\*"
    $Dest = "E:\back_fold\"
    robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL /XD "\\192.168.1.100\asuscloud\"
    

    它抛出

    ------------------------------------------------------------------------------&燃气轮机-

    开始时间:2018年9月1日星期六晚上8:46:23 来源:D:\背面\ 目的地-

    文件夹: .

    选项: . /FP/NDL/三通/数据副本:数据/副本:DAT/Z/NP/IS/MT:64/R:1000000/W:30


    错误:未指定目标目录。

    enter image description here

    如何做到这一点?

    :nbtstat-A 192.168.1.100

    C:\Windows\system32>nbtstat -A 192.168.1.100
    
    Ethernet:
    Node IpAddress: [0.0.0.0] Scope Id: []
    
        Host not found.
    
    Ethernet 2:
    Node IpAddress: [0.0.0.0] Scope Id: []
    
        Host not found.
    
    Bluetooth Network Connection:
    Node IpAddress: [0.0.0.0] Scope Id: []
    
        Host not found.
    
    Wi-Fi:
    Node IpAddress: [192.168.1.105] Scope Id: []
    
               NetBIOS Remote Machine Name Table
    
           Name               Type         Status
        ---------------------------------------------
        PASCAL         <00>  UNIQUE      Registered
        PASCAL         <03>  UNIQUE      Registered
        PASCAL         <20>  UNIQUE      Registered
        ☺☻__MSBROWSE__☻<01>  GROUP       Registered
        WORKGROUP      <1E>  GROUP       Registered
        WORKGROUP      <00>  GROUP       Registered
        WORKGROUP      <1D>  UNIQUE      Registered
        WORKGROUP      <1B>  UNIQUE      Registered
    
        MAC Address = 00-00-00-00-00-00
    
    
    Local Area Connection* 3:
    Node IpAddress: [0.0.0.0] Scope Id: []
    
        Host not found.
    

    C:\Windows\system32>NET VIEW \\192.168.1.100
    Shared resources at \\192.168.1.100
    
    Pascal
    
    Share name  Type  Used as  Comment
    
    -------------------------------------------------------------------------------
    asuscloud   Disk           data's asuscloud in External USB3.0
    backup      Disk           data's backup in External USB3.0
    downloads   Disk           data's downloads in External USB3.0
    dropbox     Disk           data's dropbox in External USB3.0
    entware     Disk           data's entware in External USB3.0
    os          Disk           data's os in External USB3.0
    The command completed successfully.
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   lit    8 年前

    我认为您的基本误解是主机名(或IP地址)本身不是路径。它还必须有一个共享名。

    如果您将此另存为 Do-It.ps1 .\Do-It.ps1 -Verbose

    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true, position=0)]
        [string]$computername
    
        ,[Parameter(Mandatory=$true, position=1)]
        [string[]]$sharenames
    
        ,[Parameter(Mandatory=$false, position=2)]
        [string]$dest = 'E:/back_fold/asuscloud'
    )
    
    $excludeshares = @('SHARE1', 'OTHERSHARE')
    
    foreach ($sharename in $sharenames) {
        write-Verbose $sharename
        if ($excludeshares -notcontains $sharename.ToUpper()) {
            $Path = "//$computername/$sharename"
            $cmdline = { robocopy $Path $Dest /B /MT:64 /IS /Z /FP /MIR /TEE /NP /FP /NDL }
            Write-Verbose $cmdline.ToString()
            & $cmdline
        }
    }
    

    这可以用于包含共享名列表的文件。

    .\Do-It.ps1 -computername '192.168.1.100' -sharenames $(Get-Content -Path .\sharelist.txt)
    
        2
  •  0
  •   Robert Cotterman    8 年前

    记住\是转义字符。二等于一。尝试使用单引号 ' 而不是双引号 "

    这就是为什么它要查找\192.168.1.100\而不是\192.168.1.100\的原因\

    如果需要的话,我也会提供共享名。

    单引号将允许您不需要转义。例子:

    $Path = Split-Path '\\192.168.1.100\asuscloud\*'
    $Dest = 'E:\back_fold\asuscloud\'
    

    使用双引号可能需要转义。例子:

    $Path = Split-Path "\\\\192.168.1.100\\asuscloud\\*"
    $Dest = "E:\\back_fold\\asuscloud\\"
    

    包括共享名(如果需要)。例子:

    $Path = Split-Path '\\192.168.1.100\c$\asuscloud\*'