简体   繁体   English

将所有用户文档移到子文件夹中

[英]Move all users documents into a subfolder

I am trying to write a script that will move all my users documents, currently in "\\server\\share\\%username%" to "\\server\\share\\%username%\\Documents". 我试图编写一个脚本,将所有用户文档(当前在“ \\ server \\ share \\%username%”中)移动到“ \\ server \\ share \\%username%\\ Documents”中。
It will also check to see if the documents folder exists and if not it will create it. 它还将检查documents文件夹是否存在,如果不存在,则会创建它。

To test this works I have broken the script up to test each section and replaced the actual command with a Write-Host, this section should test to see if the documents folder exists. 为了测试这项工作,我已经分解脚本来测试每个部分,并用Write-Host替换了实际命令,该部分应该进行测试以查看documents文件夹是否存在。

when I run it and check the users home folders that are highlighted yellow, some user have a documents folder and some dont, but it should only highlight the ones that dont have a documents folder yellow. 当我运行它并检查突出显示为黄色的用户主文件夹时,一些用户有一个documents文件夹,而另一些则没有,但它只应突出显示那些没有黄色documents文件夹的用户。

$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
}
}
}

After the documents folder has been created I want to create another folder and set the attributes to Hidden if the folder does not exist already. 创建documents文件夹后,如果该文件夹尚不存在,我想创建另一个文件夹并将属性设置为Hidden。

# 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
}

Once this is sorted I then want to check that the folder path "\\\\server\\share\\%username%\\documents" folder exists. 排序后,我然后要检查文件夹路径“ \\\\ server \\ share \\%username%\\ documents”文件夹是否存在。
If it does I then want to move all the documents from the "%username%" folder to the "Documents" folder and finally change the AD home folder path to point to the new location. 如果可以,那么我想将所有文档从“%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
}
}
}

So the only problem I am seeing you list is in the first portion when it should only be coloring users yellow if they do not have a documents folder. 因此,我看到您列出的唯一问题是在第一部分,当用户没有文档文件夹时,它应该仅将用户涂成黄色。 The issue here would seem to be your loop nesting. 这里的问题似乎是您的循环嵌套。 your $users loop is inside of your $userhomes loop, so right now for each user home directory you are setting all of your variables, then getting a collection of all users in the domain, then looping through that collection and populating each of them with the variable that was set for that step of the $userhomes loop, which is unrelated to the user that is being worked on in the $users loop. 您的$users循环位于$userhomes循环内,因此,现在为每个用户主目录设置所有变量,然后获取域中所有用户的集合,然后遍历该集合并向其中填充每个用户这是对的那一步设置变量$userhomes循环,这是无关的是在正在处理的用户$users环路。 The solution here is just to eliminate $users loop and just output the names of the folders rather than the users samaccountname, like I have below, which should work if the names of their home drives reflect their account names. 这里的解决方案是消除$users循环并仅输出文件夹的名称,而不是输出用户samaccountname,就像我在下面提到的那样,如果其主驱动器的名称反映了他们的帐户名,则该名称应该起作用。 if not you could always do a lookup based on the homedirectory property in AD but you'll need to parse your existing filename into the network name. 如果不是,您总是可以基于AD中的homedirectory属性进行查找,但是您需要将现有文件名解析为网络名称。

$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"
}
}

If none of that will work for you you could also loop through the collection of users and calculate the home drive for them. 如果这些都不适合您,您还可以遍历用户集合并为其计算主驱动器。 You just need to be relating the two somehow, which you are currently not doing in your nested loops. 您只需要以某种方式将两者关联起来,而您目前不在嵌套循环中这样做。

This should sort it for you, it checks and moves any user folders in the D: drive. 这应该为您排序,它检查并移动D:驱动器中的所有用户文件夹。 Then goes through AD after and updates the Home Folders. 然后经过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"
}

I would just like to say a big thanks for all the help, here is my completed script. 我要感谢所有帮助,这是我完成的脚本。 At any stage it informs me what's going on and if there are any issues. 在任何阶段,它都会告诉我发生了什么以及是否有任何问题。

$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
}
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Powershell + EWS脚本将所有文本消息移至Inbox / SMS子文件夹 - Powershell + EWS script to move all text messages to Inbox/SMS subfolder 将文件移动到动态子文件夹 - Move file to dynamic subfolder 如何将文件夹中的特定pdf文档移动到新文件夹中? 我要移动的文件的名称中都有一个特定的关键字 - How to move specific pdf documents in a folder into a new folder? The documents I want to move all have a specific keyword in their name 将文件移动到新创建的子文件夹 - Move file to newly created subfolder 将所有 AD 用户从一个 OU 移动到另一个 - Move all AD users from one OU to another 根据文件名列表将文件批量移动到子文件夹 - Batch move files to a subfolder based on list of filenames 编写 PowerShell 脚本以将目录及其内容复制到所有用户的文档文件夹 - Writing a PowerShell script to copy directory and its contents to all users documents folder 使用 Powershell 获取 ACL 文件夹和子文件夹 + 用户 - Get ACL Folder & Subfolder + Users Using Powershell Powershell 脚本仅将子文件夹中的文件移动到另一个位置,一次一个子文件夹 - Powershell script to move only files in subfolder to another location ,one subfolder at a time 在 PowerShell 中获取文件夹和特定子文件夹中的所有文件 - Get all files in a folder and in a specific subfolder in PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM