【问题标题】:Move all users documents into a subfolder将所有用户文档移动到子文件夹中
【发布时间】:2016-12-01 22:34:32
【问题描述】:

我正在尝试编写一个脚本,将当前在“\server\share\%username%”中的所有用户文档移动到“\server\share\%username%\Documents”。
它还将检查文档文件夹是否存在,如果不存在,它将创建它。

为了测试这个作品,我已经分解了脚本来测试每个部分,并用 Write-Host 替换了实际的命令,这个部分应该测试文档文件夹是否存在。

当我运行它并检查以黄色突出显示的用户主文件夹时,一些用户有一个文档文件夹而一些没有,但它应该只突出那些没有黄色文档文件夹的用户。

$userhomes = Get-ChildItem "D:\" | where {$_.Attributes -like '*Directory*'}
foreach($userhome in $userhomes) {
$exclude = "Documents"
$movesource = "D:\" + $userhome.Name
$movedest = "D:\"+ $userhome.Name +"\Documents"
$autodest = "D:\"+ $userhome.Name +"\Documents"+"\Autorecovery"
$docexists = Test-Path $movedest
$autoexists = Test-Path $autodest
$Search = "OU=Users,DC=domain,DC=co,DC=uk"
$Users = Get-ADUser -Filter * -SearchBase $Search

$users | % {

# Check if Documents folder already exists
If ($docexists -eq $false)
    {
# Create the Documents folder
# Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents"
    Write-Host Documents folder does not exists for ($_.SamAccountName) -ForegroundColor Yellow
}
else
{
    Write-Host Documents folder already exists for ($_.SamAccountName) -ForegroundColor Red
}
}
}

创建文档文件夹后,如果该文件夹不存在,我想创建另一个文件夹并将属性设置为隐藏。

# Check if Autorecovery folder already exists
If ($autoexists -eq $false)
{ 
    # Create the Autorecovery folder and set attributes
    Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents\Autorecovery" | %{$_.Attributes="hidden"}
    Write-Host Documents and Autorecovery folder for ($_.SamAccountName) created -ForegroundColor Green
}
 else
{
 Write-Host Autorecovery folder already exists for ($_.SamAccountName) -ForegroundColor Red
}

排序完成后,我想检查文件夹路径“\\server\share\%username%\documents”文件夹是否存在。
如果存在,我想从“% username%" 文件夹到 "Documents" 文件夹,最后将 AD 主文件夹路径更改为指向新位置。

# Move Documents to new location
If ($docexists = $true)
{
Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest

    # Set user new home folder path
    Get-ADUser -Filter * -SearchBase $Search | Foreach-Object{

    $sam = $_.SamAccountName
    Set-ADuser -Identity $_ -HomeDrive "H:" -HomeDirectory "\\server\share\$sam\Documents"
}
else
{
Write-Host Documents folder does not exist for ($_.SamAccountName) -ForegroundColor Red
}
}
}

【问题讨论】:

  • 你已经包含了很多关于你的脚本做什么的细节(太棒了!),但没有说清楚什么不起作用。
  • Sorry James 我遇到的问题是脚本说用户有一个文档文件夹,而他们没有,它也说用户没有文档文件夹,而他们确实有一个。跨度>

标签: powershell


【解决方案1】:

因此,我看到您列出的唯一问题是在第一部分,如果用户没有文档文件夹,则应该只将用户着色为黄色。这里的问题似乎是您的循环嵌套。你的 $users 循环在你的 $userhomes 循环内,所以现在为每个用户主目录设置所有变量,然后获取域中所有用户的集合,然后循环遍历该集合并填充每个他们使用为$userhomes 循环的该步骤设置的变量,该变量与$users 循环中正在处理的用户无关。此处的解决方案只是消除$users 循环并仅输出文件夹的名称而不是用户samaccountname,如下所示,如果他们的家庭驱动器的名称反映了他们的帐户名称,这应该可以工作。如果不是,您始终可以根据 AD 中的主目录属性进行查找,但您需要将现有文件名解析为网络名称。

$userhomes = Get-ChildItem "D:\" | where {$_.Attributes -like '*Directory*'}
foreach($userhome in $userhomes) {
$exclude = "Documents"
$movesource = "D:\" + $userhome.Name
$movedest = "D:\"+ $userhome.Name +"\Documents"
$autodest = "D:\"+ $userhome.Name +"\Documents"+"\Autorecovery"
$docexists = Test-Path $movedest
$autoexists = Test-Path $autodest


# Check if Documents folder already exists
If ($docexists -eq $false)
{
# Create the Documents folder
# Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents"
Write-Host "Documents folder does not exists for $($userhome) -ForegroundColor Yellow"
}
else
{
Write-Host "Documents folder already exists for $($userhome) -ForegroundColor Red"
}
}

如果这些都不适合您,您还可以遍历用户集合并为他们计算主驱动器。您只需要以某种方式将两者关联起来,而您目前在嵌套循环中没有这样做。

【讨论】:

    【解决方案2】:

    这应该会为您排序,它会检查并移动 D: 驱动器中的所有用户文件夹。然后通过 AD 更新主文件夹。

    $userhomes = Get-ChildItem "D:\" | where {$_.Attributes -like '*Directory*'}
    foreach($userhome in $userhomes) {
        $movesource = "D:\" + $userhome.Name
        $movedest = "$movesource\Documents"
        $autodest = "$movedest\Autorecovery"
        $exclude = "Documents"
    
        # Check if Documents folder already exists
        If (Test-Path $movedest)
        {
            Write-Host Documents folder already exists for ($_.SamAccountName) -ForegroundColor Red
        }
        else
        {
            # Create the Documents folder
            # New-Item -ItemType Directory -Path $movedest
            Write-Host Documents folder does not exists for ($_.SamAccountName) -ForegroundColor Yellow
        }
    
        # Check if Autorecovery folder already exists
        If (Test-Path $autodest)
        {
         Write-Host Autorecovery folder already exists for ($_.SamAccountName) -ForegroundColor Red
        }
        else
        { 
            # Create the Autorecovery folder and set attributes
            New-Item -ItemType Directory -Path $autodest | %{$_.Attributes="hidden"}
            Write-Host Documents and Autorecovery folder for ($_.SamAccountName) created -ForegroundColor Green
        }
    
        # Move Documents to new location
        if (Test-Path $movedest)
        {
            Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest
        }
        else
        {
        Write-Host Documents folder does not exist for ($_.SamAccountName) -ForegroundColor Red
        }
    }
    
    # Set user new home folder path
    Get-ADUser -Filter * -SearchBase $Search | Foreach-Object{
        $sam = $_.SamAccountName
        Set-ADuser -Identity $_ -HomeDrive "H:" -HomeDirectory "\\server\share\$sam\Documents"
    }
    

    【讨论】:

    • 感谢您的建议,这些可能有效,但我遇到的问题是写入主机中的 ($_.SamAccountName) 没有可输入的内容。所以写主机输出显示“文档文件夹已经存在”,所以没有帐户名我无法检查结果。
    • 试试这个:Write-Host "Documents folder already exists for $($_.SamAccountName)" -ForegroundColor Red
    【解决方案3】:

    非常感谢大家的帮助,这是我完成的脚本。 在任何阶段,它都会告诉我发生了什么以及是否有任何问题。

    $userhomes = Get-ChildItem "D:\" | where {$_.Attributes -like '*Directory*'}
    foreach($userhome in $userhomes) {
    $exclude = "Documents"
    $movesource = "D:\" + $userhome.Name
    $movedest = "D:\"+ $userhome.Name +"\Documents"
    $autodest = "D:\"+ $userhome.Name +"\Documents"+"\Autorecovery"
    $testpath = "D:\"+ $userhome.Name +"\Desktop"
    $Search = "OU=Users,DC=domain,DC=co,DC=uk"
    $Users = Get-ADUser -Filter * -SearchBase $Search
    
    # Test if Documents folder exists, create folder if it doesnt exist
    If (Test-Path $movedest)
    {
    Write-Host Documents folder exists for $($userhome)
    
    # Test if Autorecovery folder exists, create folder if it doesnt exist
    If (Test-Path $autodest)
    {
    Write-Host Autorecovery folder exist for $($userhome)
    
        # Move Documents to new location
        If (Test-Path $testpath)
        {
        Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest
        Get-ChildItem -Path  $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force 
        Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow
    
    
        }
        else
        {
        Write-Host Documents already moved for $($userhome)
        }
    
    }    
    else
    {
    # Create the Autorecovery folder and set hidden attribute
    Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents\Autorecovery" | %{$_.Attributes="hidden"}
    Write-Host Autorecovery folder being created for $($userhome) -ForegroundColor Green
    
        # Move Documents to new location
        If (Test-Path $testpath)
        {
        Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest
        Get-ChildItem -Path  $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force 
        Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow
    
    
        }
        else
        {
        Write-Host Documents already moved for $($userhome)
        }
    
    }
    
    }
    else
    {
    # Create the Documents folder
    Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents"
    Write-Host Documents folder being created for $($userhome) -ForegroundColor Green
    
    # Check that Documents folder now exists
    If (Test-Path $movedest)
    {
    Write-Host Documents folder now exists for $($userhome) -ForegroundColor Magenta
    Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents\Autorecovery" | %{$_.Attributes="hidden"}
    
        # Test if Autorecovery folder now exists
        If (Test-Path $autodest)
        {
        Write-Host Autorecovery folder Created for $($userhome) -ForegroundColor Green
    
            # Move Documents to new location
            If (Test-Path $testpath)
            {
            Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest
            Get-ChildItem -Path  $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force 
            Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow
    
    
            }
            else
            {
            Write-Host Documents already moved for $($userhome)
            }
    
        }
        else
        {
        # Create the Autorecovery folder and set hidden attribute
        Get-Item -path $movesource | New-Item -ItemType Directory -Path "$movesource\Documents\Autorecovery" | %{$_.Attributes="hidden"}
        Write-Host Autorecovery folder being created for $($userhome) -ForegroundColor Green
    
           # Check if Autorecovery folder exists 
           If (Test-Path $autodest)
           {
           Write-Host Autorecovery folder now exists for $($userhome) -ForegroundColor Magenta
    
                # Move Documents to new location
                If (Test-Path $testpath)
                {
                Get-Childitem -path $movesource -exclude $exclude | Move-Item -Dest $movedest
                Get-ChildItem -Path  $movesource -Exclude "Documents" | ? {$_.PSIsContainer} |Remove-Item -Recurse -force 
                Write-Host Documents being moved for $($userhome) -ForegroundColor Yellow
    
    
                }
                else
                {
                Write-Host Documents already moved for $($userhome)
                }
           }
           else
           {
           Write-Host ERROR Autorecovery folder still does not exist for $($userhome) -ForegroundColor Red
           }
    
        }
    }
    else
    {
    Write-Host ERROR Documents folder still does not exist for $($userhome) -ForegroundColor Red
    }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      相关资源
      最近更新 更多