我正在尝试编写一个脚本,该脚本会将当前位于“\ 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%”文件夹移动到“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
}
}
}
你已经包含了很多关于你的脚本(很棒!)的细节,但还没有说明什么是不起作用的。 –
对不起詹姆斯 我遇到的问题是,脚本说,用户有一个文档文件夹,当他们不,也是说用户没有一个文件夹,当他们有一个。 – lellis