【发布时间】:2016-06-08 11:08:13
【问题描述】:
使用正则表达式测试器:(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?>userDataXP>[a-zA-Z 0-9]*\$\$)) 匹配 \\server\e$\users\user1$$
使用 PowerShell 我必须在用户文件路径中的第二个 $ 之前添加一个反引号以使其匹配:
"\\server\e$\users\user1$`$" -match "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))"
True
否则失败:
"\\server\e$\users\user1$$" -match "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))"
False
以下脚本的输入文件包含用户名都包含$$ 的文件路径。我希望正则表达式能够捕获user$$,创建一个仍然带有$$ 的目录,并将文件复制到其中。如何在以下脚本中解决此问题?
$paths = get-content "E:\paths.txt"
$ArchiveLocation = "\\Server1\Archive"
$LogsData = Test-Path "$ArchiveLocation\Archived_UserData\Logs"
$LogsProfiles = Test-Path "$ArchiveLocation\Archived_UserProfiles\Logs"
$regxUsrProfPthXP = "(?<UserProfilePathXP>^((?![a-zA-Z 0-9]*\$\$).)*\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userProfileXP>[a-zA-Z 0-9]*\$))"
$regxUsrDataPthXP = "(?<UserDataPathXP>\\\\[a-zA-Z 0-9]*\\[a-zA-Z 0-9]\$\\[a-zA-Z 0-9]*\\(?<userDataXP>[a-zA-Z 0-9]*\$\$))"
if (-not $LogsProfiles) {
md "$ArchiveLocation\Archived_UserProfiles\Logs"
}
if (-not $LogsData) {
md "$ArchiveLocation\Archived_UserData\Logs"
}
foreach ($path in $paths) {
if ($path -match $regxUsrProfPthXP) {
$path = $matches.UserProfilePathXP
$user = $matches.userProfileXP
RoboCopy $path "$ArchiveLocation\Archived_UserProfiles\$user" /MIR /B /COPY:DATSOU /MOVE /LOG+:$ArchiveLocation\Archived_UserProfiles\Logs\$user.log /R:1 /W:1
}
if ($path -match $regxUsrDataPthXP) {
$path = $matches.UserDataPathXP
$user = $matches.userDataXP
RoboCopy $path "$ArchiveLocation\Archived_UserData\$user" /MIR /B /COPY:DATSOU /MOVE /LOG+:$ArchiveLocation\Archived_UserData\Logs\$user.log /R:1 /W:1
}
}
【问题讨论】:
标签: regex powershell