PowerShell - 双循环可能吗?

PowerShell - Double loop possible?

我想在特定的地方压缩一个目录。 源路径是:\$Computers\Users$Names

我想为每台计算机在每台计算机的源路径中复制每个用户目录

我尝试使用 foreach 循环,例如:

$Computers = Get-ADComputer -Filter "Name -like 'PC*'" | Select-Object -ExpandProperty Name
$Names = Get-aduser -filter * | Select-Object -ExpandProperty givenname 

Foreach($Computer in $Computers)
{
    Compress-Archive -Path \$Computer\Users\* -DestinationPath C:\Saves\$Computer\Test.zip -Force
}

这确实有效,但我不知道如何在循环内添加第二个循环。

如果有人可以向我解释这个功能或只是一些建议,请尝试这样做。

感谢您的宝贵时间。

您正在用错误的逻辑解决问题,您确实需要一个内部循环,但是,与其尝试压缩您不确定是否存在的用户配置文件,不如查询远程计算机的用户配置文件Users 文件夹以查看其中有哪些并仅压缩那些:

$Computers = (Get-ADComputer -Filter "Name -like 'PC*'").Name
# Add the profiles you want to exclude here:
$toExclude = 'Administrator', 'Public'
$params = @{
    Force = $true
    CompressionLevel = 'Optimal'
}

foreach($Computer in $Computers)
{
    $source = "\$Computer\Users"
    Get-ChildItem $source -Exclude $toExclude -Directory | ForEach-Object {
        $params.LiteralPath = $_.FullName
        # Name of the zipped file would be "ComputerExample - UserExample.zip"
        $params.DestinationPath = "C:\Saves$computer - {0}.zip" -f $_.Name
        Compress-Archive @params
    }
}