从变量创建Powershell文件夹

时间:2014-03-12 17:25:41

标签: powershell

这是我到目前为止所做的工作:

$extensions = '*.xls*', '*.doc*', '*.txt', '*.pdf', '*.jpg', '*.lnk', '*.pub', '*.pst', '*.pps', '*.ppt'

Get-Content C:\computers.txt | % {
  $ComputerName = $_

  $dst = "\\Server\share\$ComputerName"
  $src = "\\$ComputerName\c$\Documents and Settings\**\Desktop",
         "\\$ComputerName\c$\Documents and Settings\**\My Documents"

  New-Item -ItemType Directory $dst

  Get-Childitem $src -Include $extensions -Recurse -Force |
    Copy-Item -Destination $dst\
}

我的目标是从计算机列表中备份特定的文件类型。这些机器上有大约10-20个型材。是否可以将这些文件安排在它们来自的配置文件的计算机名称目录下的目录中?例如,在创建的计算机名称目录下创建配置文件名称目录,然后将目标文件转储到相应的文件夹中。

示例:

\\server\share\computername1\profile1\document.doc
\\server\share\computername2\Profile2\document.doc

1 个答案:

答案 0 :(得分:0)

如果你想使用路径的一部分,你不能在你的源路径中使用通配符(从技术上讲,你可以使用通配符,但后面会很痛苦)。改为使用内循环:

$extensions = '*.xls*', '*.doc*', ...

Get-Content C:\computers.txt | % {
  $ComputerName = $_

  Get-ChildItem "\\$ComputerName\c$\Documents and Settings" | ? {
    $_.PSIsContainer
  } | % {
    $dst = Join-Path "\\Server\share\$ComputerName" $_.Name

    New-Item -ItemType Directory $dst
    # no need to create $dst first, b/c New-Item will auto-create missing
    # parent folders

    $src = (Join-Path $_.FullName 'Desktop'),
           (Join-Path $_.FullName 'My Documents')

    Get-Childitem $src -Include $extensions -Recurse -Force |
      Copy-Item -Destination $dst\
  }
}
相关问题