2016-11-29 124 views
0

我正在嘗試編寫一個腳本,該腳本會將當前位於「\ server \ share \%username%」中的所有用戶文檔移動到「\ server \ share \%username%\ 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 
} 
} 
} 

後的文件夾已經創建,我想創建另一個文件夾,如果該文件夾不存在的屬性設置爲隱藏。

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

一旦被排序然後我要檢查的文件夾路徑「\\服務器\共享\%USERNAME%\ Documents」文件夾存在。
如果確實如此,我希望將所有文檔從「%username%」文件夾移動到「Doc​​uments」文件夾,最後將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 
} 
} 
} 
+1

你已經包含了很多關於你的腳本(很棒!)的細節,但還沒有說明什麼是不起作用的。 –

+0

對不起詹姆斯 我遇到的問題是,腳本說,用戶有一個文檔文件夾,當他們不,也是說用戶沒有一個文件夾,當他們有一個。 – lellis

回答

0

因此,我看到你列出的唯一問題是在第一部分,它應該只爲用戶着色,如果他們沒有文檔文件夾。這裏的問題似乎是你的循環嵌套。您的$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" 
} 
} 

如果這些都不適用於您,您還可以循環訪問用戶集合併爲它們計算主驅動器。你只需要將兩者聯繫起來,你現在沒有在你的嵌套循環中做什麼。

0

這應該爲您排序,它會檢查並移動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" 
} 
+0

感謝您的推薦,這些可能會起作用,但是我遇到的問題是寫主機中的($ _。SamAccountName)無法輸入。 所以寫主機輸出說「文件夾已存在」,所以沒有帳戶名我無法檢查結果。 – lellis

+0

試試這個:'Write-Host「Documents文件夾已經存在$($ _。SamAccountName)」-ForegroundColor Red' –

0

我只想對所有的幫助表示非常感謝,這裏是我完成的腳本。 在任何階段它都會告訴我發生了什麼事情,以及是否有任何問題。

$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 
} 
} 
} 
相關問題