当我试图扫描CBS日志文件和注册表中的错误时,我被这个Powershell脚本卡住了,它给了我一个null值错误



我正试图扫描CBS日志文件中的某个字符串,然后清理注册表,但收到一个空值错误。我在匹配(ERROR_SXS_ASSEMBLY_MISSING(部分时遇到问题,该部分确实存在于括号内的日志文件中,但不包含'符号/标记。

这是完整的代码:

#Read CBS log file contents into memory
try {$Contents = [System.IO.File]::ReadLines('C:WindowsLogsCBSCBS.log')} catch {"File is currently in use.  Re-run script in 2 minutes.";return}
#Parse log file for missing assemblies
$InterestingLines = $Contents | Select-String -SimpleMatch '(ERROR_SXS_ASSEMBLY_MISSING)'
#Retrieve unique Package names from error messages
$Packages = @()
foreach ($Line in $InterestingLines) {
$Package = [regex]"^([ws.-()]+)$".Substring(0,$Package.Length - ($Package.split(".")[4]).Length - 1)
if ($Packages -notcontains $Package) { $Packages += $Package }
}
$AllKeys = @('HKLM:SOFTWAREMicrosoftWindowsCurrentVersionComponent Based ServicingPackageDetect', 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionComponent Based ServicingPackages')
foreach ($RegRoot in $AllKeys) {
$Keys = Get-ChildItem $RegRoot | where {$_.PSIsContainer}
foreach ($Key in $Keys) {
#write-host "Checking $($Key.name)" -ForegroundColor Blue
foreach ($Package in $Packages) {
foreach ($Property in $Key.Property) {
#write-Host "$Property ? $Package" -ForegroundColor blue
if ($Property -match $Package) {
$ShortTarget = $($Key.Name).Substring(87) 
write-host "Found $Package in $ShortTarget...  " -ForegroundColor Yellow -NoNewline
$Target = $($Key.Name).TrimStart("HKEY_LOCAL_MACHINE\")
try {
# Attempt to give Admins full control of key and delete key.
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey($Target,[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::ChangePermissions)
$acl = $key.GetAccessControl()
$rule = New-Object System.Security.AccessControl.RegistryAccessRule ("BUILTINAdministrators","FullControl","Allow")
$acl.SetAccessRule($rule)
$key.SetAccessControl($acl)
Remove-ItemProperty -Path "HKLM:$Target" -Name $Package -Force
Write-Host "delete successful." -ForegroundColor Green
} catch {
Write-Host "delete failed.  Delete manually." -ForegroundColor Red
}
}
}
}
}
}
You cannot call a method on a null-valued expression.
At line:4 char:19
+     $Package  = $(($Line -split("'") )[1]).Substring(0,$Package.Lengt ...
+                   ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
'Package_5287_for_KB4525236~31bf3856ad364e35~amd64~~10.0.1.5.4525236-9034_neutral', rah = '2', manpath = (null), catpath = (null), ed = 0, disp = 0)[gle=0x80073701]
2021-05-09 16:42:50, Info                  CBS    Failed to pin deployment while resolving Update: Package_5287_for_KB4525236~31bf3856ad364e35~amd64~~10.0.1.5.4525236-9034_neutral from file: (null) [HRESULT = 0x80073701 - ERROR_SXS_ASSEMBLY_MISSING]
2021-05-09 16:42:50, Info                  CBS    Failed to bulk stage deployment manifest and pin deployment for package:Package_8014_for_KB5001347~31bf3856ad364e35~amd64~~10.0.1.4 [HRESULT = 0x80073701 - ERROR_SXS_ASSEMBLY_MISSING]
2021-05-09 16:42:50, Info                  CBS    CommitPackagesState: Started persisting state of packages
2021-05-09 16:42:50, Info                  CBS    Not able to add poqexec.log to Windows Error Report. [HRESULT = 0x80070002 - ERROR_FILE_NOT_FOUND]
2021-05-09 16:42:50, Error                 CSI    00000009 (F) STATUS_SXS_ASSEMBLY_MISSING #33584# from CCSDirectTransaction::OperateEnding at index 0 of 1 operations, disposition 2[gle=0xd015000c]
2021-05-09 16:42:50, Error                 CSI    0000000a (F) HRESULT_FROM_WIN32(ERROR_SXS_ASSEMBLY_MISSING) #33432# from Windows::ServicingAPI::CCSITransaction::ICSITransaction_PinDeployment(Flags = 0, a = c39e63ddd18df5c405b8f3627f4d40eb, version 10.0.14393.2608, arch amd64, nonSxS, pkt {l:8 b:31bf3856ad364e35}, cb = (null), s = (null), rid = 'Package_5287_for_KB4525236~31bf3856ad364e35~amd64~~10.0.1.5.4525236-9034_neutral', rah = '2', manpath = (null), catpath = (null), ed = 0, disp = 0)[gle=0x80073701]

我不能100%确定您想从CBS.log中匹配/提取什么,所以我假设您想要得到的是Name of the Package,直到字符串ERROR_SXS_ASSEMBLY_MISSING出现的行上的~符号。

因此,我已经捕获了您在问题中添加到数组中的日志的两个示例,这与[System.IO.File]::ReadLines('C:WindowsLogsCBSCBS.log')在代码中所做的类似。

看起来像这样:

'Package_5287_for_KB4525236~31bf3856ad364e35~amd64~~10.0.1.5.4525236-9034_neutral', rah = '2', manpath = (null), catpath = (null), ed = 0, disp = 0)[gle=0x80073701]
2021-05-09 16:42:50, Info                  CBS    Failed to pin deployment while resolving Update: Package_5287_for_KB4525236~31bf3856ad364e35~amd64~~10.0.1.5.4525236-9034_neutral from file: (null) [HRESULT = 0x80073701 - ERROR_SXS_ASSEMBLY_MISSING]
2021-05-09 16:42:50, Info                  CBS    Failed to bulk stage deployment manifest and pin deployment for package:Package_8014_for_KB5001347~31bf3856ad364e35~amd64~~10.0.1.4 [HRESULT = 0x80073701 - ERROR_SXS_ASSEMBLY_MISSING]
2021-05-09 16:42:50, Info                  CBS    CommitPackagesState: Started persisting state of packages
2021-05-09 16:42:50, Info                  CBS    Not able to add poqexec.log to Windows Error Report. [HRESULT = 0x80070002 - ERROR_FILE_NOT_FOUND]
2021-05-09 16:42:50, Error                 CSI    00000009 (F) STATUS_SXS_ASSEMBLY_MISSING #33584# from CCSDirectTransaction::OperateEnding at index 0 of 1 operations, disposition 2[gle=0xd015000c]
2021-05-09 16:42:50, Error                 CSI    0000000a (F) HRESULT_FROM_WIN32(ERROR_SXS_ASSEMBLY_MISSING) #33432# from Windows::ServicingAPI::CCSITransaction::ICSITransaction_PinDeployment(Flags = 0, a = c39e63ddd18df5c405b8f3627f4d40eb, version 10.0.14393.2608, arch amd64, nonSxS, pkt {l:8 b:31bf3856ad364e35}, cb = (null), s = (null), rid = 'Package_5287_for_KB4525236~31bf3856ad364e35~amd64~~10.0.1.5.4525236-9034_neutral', rah = '2', manpath = (null), catpath = (null), ed = 0, disp = 0)[gle=0x80073701]

例如,如果你从每一行中提取Date & TimeName of the Package,你可以使用这样的东西(请记住,我远不是regex专家(:

$regex = [regex]'d{4}-d{2}-d{2}sd{2}:d{2}:d{2}|Package_d+_for_KBd+(?=~)'
$result = foreach($line in $log | Select-String 'ERROR_SXS_ASSEMBLY_MISSING')
{
$isMatch = $regex.Matches($line)
if($isMatch)
{
[PScustomObject]@{
Date = $isMatch[0].Value
Package = $isMatch[1].Value
}
}
}

这将返回一个看起来像这样的对象:

PS /> $result
Date                Package                   
----                -------                   
2021-05-09 16:42:50 Package_5287_for_KB4525236
2021-05-09 16:42:50 Package_8014_for_KB5001347
2021-05-09 16:42:50 Package_5287_for_KB4525236

这很容易操作。即:检查包裹是否重复:

PS /> $result.Package | Select-Object -Unique
Package_5287_for_KB4525236
Package_8014_for_KB5001347

相关内容

  • 没有找到相关文章

最新更新