2016-03-03 71 views
0

我把一個Robocopy腳本放在一起備份數據,但是我遇到的問題是腳本正在讀取父文件夾並嘗試執行robocopy過濾器並出錯。 我想要做的是縮進一個,並開始複製子文件夾,並在那裏使用過濾器。文件結構是PowerShell讀取內部文件夾

用戶\蒂姆·瓊斯,薩姆·亞當斯

我只是想添瓊斯文件夾,並用過濾的Robocopy山姆·亞當斯的用戶文件夾添加到它。歡迎任何幫助

$Comp = Read-Host 'Please enter a computer name or IP' 

do{ 
If (Test-Connection $Comp -quiet -Count 1) { 
Write-host 'The host responded' -ForegroundColor "yellow" 
Read-Host 'Press any key to continue...' | Out-Null 
$ping = "ture" 
Clear-Host 

Get-WmiObject -ComputerName $Comp -class Win32_NetworkLoginProfile | Select- Object Name,@{label='LastLogon';expression={$_.ConvertToDateTime($_.LastLogon)}} 

New-Item -Path \\skyzone\t$\sky\ -ItemType directory 

$SourcePath = "\\$Comp\C$\Users\"; 

$TargetPath = "\\skyzone\t$\sky"; 

#Users libraries 
$PicturesLibrary = ("\Pictures"); 
$Downloads = ("\Downloads"); 
$Favorites = ("\Favorites"); 
$Documents = ("\Documents"); 
$Desktop = ("\Desktop"); 
$Video = ("\Videos"); 

#User Outlook files and logs 
$Outlook = ("\AppData\Local\Microsoft\Outlook") 

$argsFromFolders = ("$SourcePath\$PicturesLibrary","$SourcePath\$Downloads","$SourcePath\$Favorites","$SourcePath\$Documents","$SourcePath\$Desktop","$SourcePath\$Video" ,"$SourcePath\$Outlook"); 
$argsToFolders = ("$TargetPath\Pictures","$TargetPath\Downloads","$TargetPath\Favorites","$TargetPath\Documents","$TargetPath\Desktop","$TargetPath\Videos","$TargetPath\Outlook"); 


For ($i=0; $i -lt $argsFromFolders.Length; $i++) { 
Write-Host -ForegroundColor Green "Processing" $argsFromFolders[$i] "To" $argsToFolders[$i]; 


robocopy $argsFromFolders[$i] $argsToFolders[$i] *.* /MT:16 /XJ *.pst /R:3 /W:1 /NP /e /xf *.vmdk *.vmem *.iso *.exe *.ost desktop.ini /tee /Log+: 

} 

回答

0

您的問題不清楚,樣品不完整。你指的是$user這是從來沒有創建,並沒有robocopy -calls在這裏。正因爲如此,我無法給你一個完整的解決方案。

看看這個例子的源路徑在你的代碼:

$SourcePath = "\\localhost\C$\Users\"; 
$PicturesLibrary = ("\Pictures"); 
"$SourcePath\$PicturesLibrary" 

#Output 
\\localhost\C$\Users\\\Pictures 
  1. 有在你加入這兩個字符串當場太多的反斜槓。從變量中取出,或用Join-Path

    ​​

    或者

    $SourcePath = "\\localhost\C$\Users" 
    $PicturesLibrary = "Pictures" 
    "$SourcePath\$PicturesLibrary" 
    
    #Output 
    \\localhost\C$\Users\Pictures 
    
  2. 用戶名丟失。您需要在$sourcePath上使用Get-Childitem以獲取用戶名單並循環訪問。

    樣品:

    $SourcePath = "\\localhost\C$\Users\" 
    Get-ChildItem -Path $SourcePath | 
    #Get folders only... And exlude public? 
    Where-Object { $_.PSIsContainer -and $_.Name -ne 'Public' } | 
    ForEach-Object { 
        #Code to run for each user-folder 
        $PicturesLibrary = ("Pictures") 
        Join-Path $_.FullName $PicturesLibrary 
    } 
    \\localhost\C$\Users\Default.migrated\Pictures 
    \\localhost\C$\Users\defaultuser0\Pictures 
    \\localhost\C$\Users\frode\Pictures 
    

更新:嘗試是這樣的(未經測試)。

$Comp = Read-Host 'Please enter a computer name or IP' 

If (Test-Connection $Comp -quiet -Count 1) { 

    Write-host 'The host responded' -ForegroundColor "yellow" 
    Read-Host 'Press any key to continue...' | Out-Null 
    Clear-Host 

    Get-WmiObject -ComputerName $Comp -class Win32_NetworkLoginProfile | Select- Object Name,@{label='LastLogon';expression={$_.ConvertToDateTime($_.LastLogon)}} 

    $TargetPath = '\\skyzone\t$\sky' 
    if(-not (Test-Path -Path $TargetPath -PathType Container)) { New-Item -Path $TargetPath -ItemType Directory | Out-Null } 

    $Folderlist = ("Pictures","Downloads","Favorites","Documents","Desktop","Videos","Outlook"); 

    Get-ChildItem -Path "\\$Comp\C$\Users\" | 
    #Get folders only... And exclude public? 
    Where-Object { $_.PSIsContainer -and $_.Name -ne 'Public' } | 
    ForEach-Object { 
     $user = $_.Name 

     foreach ($folder in $Folderlist) { 
      $from = Join-Path $_.FullName $folder 
      $to = Join-Path $TargetPath "$user\$folder" 

      Write-Host -ForegroundColor Green "Processing '$from' To '$to'" 
      robocopy $from $to *.* /MT:16 /XJ *.pst /R:3 /W:1 /NP /e /xf *.vmdk *.vmem *.iso *.exe *.ost desktop.ini /tee /Log+:\\apgrwkate1090f\t$\dren\$user\backup.txt 
     } 
    } 
} 
+0

感謝您的回覆。我無法獲得完整的代碼發佈,所以我只提供了一半的代碼。讓我再試一次 – user2457713

+0

我加了代碼的重置。例2是在這一點上我正試圖完成的工作。我希望腳本在應用robocopy過濾器時將循環遍歷「users文件夾」中的所有用戶,並將結果放在服務器上。 – user2457713

+0

樣本仍不完整。 '$ user'和'$ outlook'仍然沒有定義,你的腳本塊沒有關閉。這個問題現在也太廣泛了。查看更新。這是我最後一次更新,因爲這個問題應該已經關閉了。 –