![](/img/trans.png)
[英]Powershell + EWS script to move all text messages to Inbox/SMS subfolder
[英]Move all users documents into a subfolder
我試圖編寫一個腳本,將所有用戶文檔(當前在“ \\ server \\ share \\%username%”中)移動到“ \\ server \\ share \\%username%\\ Documents”中。
它還將檢查documents文件夾是否存在,如果不存在,則會創建它。
為了測試這項工作,我已經分解腳本來測試每個部分,並用Write-Host替換了實際命令,該部分應該進行測試以查看documents文件夾是否存在。
當我運行它並檢查突出顯示為黃色的用戶主文件夾時,一些用戶有一個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
}
}
}
創建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
}
排序后,我然后要檢查文件夾路徑“ \\\\ 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
}
}
}
因此,我看到您列出的唯一問題是在第一部分,當用戶沒有文檔文件夾時,它應該僅將用戶塗成黃色。 這里的問題似乎是您的循環嵌套。 您的$users
循環位於$userhomes
循環內,因此,現在為每個用戶主目錄設置所有變量,然后獲取域中所有用戶的集合,然后遍歷該集合並向其中填充每個用戶這是對的那一步設置變量$userhomes
循環,這是無關的是在正在處理的用戶$users
環路。 這里的解決方案是消除$users
循環並僅輸出文件夾的名稱,而不是輸出用戶samaccountname,就像我在下面提到的那樣,如果其主驅動器的名稱反映了他們的帳戶名,則該名稱應該起作用。 如果不是,您總是可以基於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"
}
}
如果這些都不適合您,您還可以遍歷用戶集合並為其計算主驅動器。 您只需要以某種方式將兩者關聯起來,而您目前不在嵌套循環中這樣做。
這應該為您排序,它檢查並移動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"
}
我要感謝所有幫助,這是我完成的腳本。 在任何階段,它都會告訴我發生了什么以及是否有任何問題。
$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.