【发布时间】: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