Powershell读取内部文件夹

Powershell读取内部文件夹,powershell,Powershell,我编写了一个Robocopy脚本来备份数据,但我遇到的问题是,该脚本正在读取父文件夹并尝试执行Robocopy过滤器,然后出错。 我想让它做的是缩进一个,开始复制子文件夹并在那里使用过滤器。文件结构是 用户\蒂姆·琼斯、萨姆·亚当斯 我只想添加tim jones文件夹和sam adams用户文件夹以及Robocopy过滤器。欢迎任何帮助 $Comp = Read-Host 'Please enter a computer name or IP' do{ If (Test-Connection

我编写了一个Robocopy脚本来备份数据,但我遇到的问题是,该脚本正在读取父文件夹并尝试执行Robocopy过滤器,然后出错。 我想让它做的是缩进一个,开始复制子文件夹并在那里使用过滤器。文件结构是

用户\蒂姆·琼斯、萨姆·亚当斯

我只想添加tim jones文件夹和sam adams用户文件夹以及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+:

}  

你的问题不清楚,样品也不完整。您指的是从未创建过的
$user
,这里没有
robocopy
-调用。因此,我不能给你一个完整的解决方案

请看一下代码中的示例源路径:

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

#Output
\\localhost\C$\Users\\\Pictures
  • 在你连接两条弦的地方有太多的反斜杠。从变量中删除它们或使用
    连接路径

    $SourcePath = "\\localhost\C$\Users\"
    $PicturesLibrary = "\Pictures" 
    Join-Path $SourcePath $PicturesLibrary
    
    #Output
    \\localhost\C$\Users\Pictures
    

  • 缺少用户名。您需要在
    $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
    
  • 更新:尝试类似的方法(未测试)


    谢谢你的回复。我在发布完整的代码时遇到了麻烦,所以我只发布了一半的代码。让我试试阿加尼添加的重置代码。例2是我在这一点上要做的事情。我希望脚本在应用robocopy过滤器的同时遍历“users文件夹”中的所有用户,并将结果放在服务器上。示例仍然不完整<代码>$user和
    $outlook
    仍未定义,脚本块也未关闭。这个问题现在也太宽泛了。请参阅更新。这是我的最后一次更新,因为此问题本应已关闭。$user和$outlook来自我测试enter individual user时,已被删除。$user和$outlook来自我测试enter individual user时,已被删除。我对PowerShell还是一个新手,我不明白你所说的“现在也太棒了”是什么意思?文件的位置将保持不变,唯一改变的变量是计算机名。如果我遗漏了什么,请同样如此,
    $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
            }
        }
    }