我正在将文件夹重定向存储移动到新服务器。
但是,appdata的结构现在似乎是users\AppData\Roaming\,而不是旧结构users\AppData(Roaming)\
我正在尝试将所有文件夹从旧结构移动到一堆用户文件夹的新结构中。
我假设我需要使用某种循环来检查每个文件夹。
像这样的东西可以工作吗?
$folderlist = ("foldertwo", "folderthree")
foreach ($folder in $folderlist)
{
if (!(Test-Path "P:\users$\%username%\AppData\Roaming"))
{
mkdir ("P:\users$\%username%\AppData\Roaming") | Out-Null
}
Copy-Item P:\users$\%username%\AppData(Roaming)\* P:\users$\%username%\AppData\Roaming\ -recurse -Container
}我是powershell的新手,所以编写脚本不是我的拿手好戏。
发布于 2021-03-13 23:46:17
在阅读了注释之后,这里是一段部分代码,因为我认为您最好使用RoboCopy来做繁重的工作,将所有内容从AppData(Roaming)文件夹复制到新的AppData\Roaming文件夹。
请先在虚拟用户上测试,以确定在robocopy上使用哪些开关。当然,还要测试新路径上的用户权限是否正确(从P:\Users$\<username>文件夹继承)
$userShare = 'P:\Users$' # if running on the server, otherwise best use the UNC path
# loop through tyhe folders inside the user share (1st level only)
Get-ChildItem -Path $userShare -Directory | ForEach-Object {
Write-Host "Processing user $($_.Name)"
$destinationDir = Join-Path $_.FullName -ChildPath ('AppData\Roaming')
if (!(Test-Path -Path $destinationDir -PathType Container)) {
Write-Host "Creating folder '$destinationDir'"
$null = New-Item -Path $destinationDir -ItemType Directory
}
$wrongRoamingDir = Join-Path $_.FullName -ChildPath ('AppData(Roaming)')
if (Test-Path -Path $wrongRoamingDir -PathType Container) {
#############################################################################################
# here is where you start copying everything from the $wrongRoamingDir to the $destinationDir.
# I would suggest using RoboCopy for that using switches like
# Robocopy /MIR $wrongRoamingDir $destinationDir
# or
# Robocopy /S /E $wrongRoamingDir $destinationDir
#
# Please test on a dummy user first. For more robocopy switches, have a look at
# https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
# https://www.windows-commandline.com/robocopy-command-syntax-examples/
#############################################################################################
}
}https://stackoverflow.com/questions/66612747
复制相似问题