我目前正在运行下面的PowerShell脚本来捕获错误:
# Declare Variables
$Information = Import-Csv "c:\scripts\GUIDIssue\UPNList.csv"
# Connect to O365 Tenant
Connect-MsolService
foreach ($Info in $Information) {
(Get-MsolUser -UserPrincipalName $Info.UPN).errors.errordetail.objecterrors.errorrecord| fl > "C:\scripts\GUIDIssue\error.txt"
}
输出如下: 错误代码:ExA77C31 ErrorParameters:错误参数 错误描述:由于另一个存档,因此无法启用邮箱b306e73d-4fdc-43e5-af00-518b13e962ab的邮箱新的云存档00000000-0000-0000-0000-000000000000 存在852910fe-67ed-4b7b-9e1a-ef70289d4c36。要启用新存档,请首先禁用本地存档。下一个目录同步同步周期后,启用存档 再次部署。
在“错误描述:”中,我需要获取852910fe-67ed-4b7b-9e1a-ef70289d4c36并将其分配为变量。我该怎么办?
答案 0 :(得分:0)
怎么样?
echo "ErrorCode : ExA77C31 ErrorParameters : ErrorParameters ErrorDescription : Failed to enable the new cloud archive 00000000-0000-0000-0000-000000000000 of mailbox b306e73d-4fdc-43e5-af00-518b13e962ab because a different archive 852910fe-67ed-4b7b-9e1a-ef70289d4c36 exists. To enable the new archive, first disable the archive on-premises. After the next Dirsync sync cycle, enable the archive on-premises again." >test.txt
gc test.txt | `
where { $_ -match "because a different archive (?<id>.*) exists" } | `
%{ $matches.id }
答案 1 :(得分:0)
以下内容对我有用。
# Split the error message on spaces so we can compare each word to our regex
$SplitErrorMessage = ((Get-Content -Path 'C:\scripts\GUIDIssue\error.txt') -split "\s")
# Assign the output from within our loop to the $ErrorMatches variable
$ErrorMatches = foreach ($Word in $SplitErrormessage) {
if ($Word -match '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}') {
# Write-Output inside the loop adds the output to our loop variable $ErrorMatches
Write-Output $Word
}
}
#Return the last item in the $ErrorMatches variable as this should be the GUID you are targeting
$ErrorMatches[-1]
'852910fe-67ed-4b7b-9e1a-ef70289d4c36'
这使用了简单的正则表达式匹配和PowerShell的强大功能。为变量分配循环会将写入标准输出的所有内容保存到循环变量中。
然后,我们返回该数组中的最后一个索引,因为所需的值始终是与我们的正则表达式匹配的最后一个索引。希望这能满足您的需求。