Closed7

Windows libraries の仕様

wintwint

モチベ

どうなってるのか確認したい。そして PowerShell で操作したい。

対象

%AppData%\Microsoft\Windows\Libraries\*.library-ms

wintwint

概要

実態としては、ただの XML ファイルの様だ。

仕様

https://learn.microsoft.com/ja-jp/windows/client-management/windows-libraries

https://learn.microsoft.com/ja-jp/windows/win32/shell/library-schema-entry

https://learn.microsoft.com/ja-jp/windows/win32/shell/schema-library-searchconnectordescription

xpath: /libraryDescription/searchConnectorDescriptionList/searchConnectorDescription

検索

https://learn.microsoft.com/en-us/windows/win32/search/search-schema-sconn-simplelocation

xpath: /libraryDescription/searchConnectorDescriptionList/searchConnectorDescription[*]/simpleLocation

https://learn.microsoft.com/ja-jp/previous-versions/bb233505(v=msdn.10)

具体例

<?xml version="1.0" encoding="UTF-8"?>
<libraryDescription xmlns="http://schemas.microsoft.com/windows/2009/library">
    <name>@shell32.dll,-34575</name>
    <ownerSID>S-1-5-21-379071477-2495173225-776587366-1000</ownerSID>
    <version>1</version>
    <isLibraryPinned>true</isLibraryPinned>
    <iconReference>imageres.dll,-1002</iconReference>
    <templateInfo>
        <folderType>{7d49d726-3c21-4f05-99aa-fdc2c9474656}</folderType>
    </templateInfo>
    <searchConnectorDescriptionList>
        <searchConnectorDescription publisher="Microsoft" product="Windows">
            <description>@shell32.dll,-34577</description>
            <isDefaultSaveLocation>true</isDefaultSaveLocation>
            <simpleLocation>
                <url>knownfolder:{FDD39AD0-238F-46AF-ADB4-6C85480369C7}</url>
                <serialized>MBAAAEAFCAAA...MFNVAAAAAA</serialized>
            </simpleLocation>
        </searchConnectorDescription>
        <searchConnectorDescription publisher="Microsoft" product="Windows">
            <description>@shell32.dll,-34579</description>
            <isDefaultNonOwnerSaveLocation>true</isDefaultNonOwnerSaveLocation>
            <simpleLocation>
                <url>knownfolder:{ED4824AF-DCE4-45A8-81E2-FC7965083634}</url>
                <serialized>MBAAAEAFCAAA...HJIfK9AAAAAA</serialized>
            </simpleLocation>
        </searchConnectorDescription>
    </searchConnectorDescriptionList>
</libraryDescription>
wintwint

serialized をムリヤリ読んでも、バイナリだから意味不明だった。

xpath: /libraryDescription/searchConnectorDescriptionList/searchConnectorDescription[*]/simpleLocation/serialized

https://learn.microsoft.com/en-us/windows/win32/search/search-schema-sconn-simplelocation

serialized: This element contains the base64-encoded ShellLink pointing to the location defined in the <url> element. Windows 7 creates the ShellLink from the value of the <url> element and properly updates this field on the first load of this library, so it should be left empty by the author.

wintwint

PowerShell で XML

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml?view=powershell-7.2

問題

なにも返ってこないぞ…

解決

https://stackoverflow.com/a/4314862/3576692

Even a "default" namespace has to be specified

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/select-xml?view=powershell-7.2#-namespace

-Namespace

$ns = @{lib = 'http://schemas.microsoft.com/windows/2009/library'}
Select-Xml -Namespace $ns -XPath '//lib:libraryDescription'
wintwint

完成

$ns = @{lib = 'http://schemas.microsoft.com/windows/2009/library'};
ls .\Books.library-ms |
  Select-Xml -Namespace $ns -XPath '//lib:url' |
  ls -LiteralPath { $_.Node.InnerText } -File -Recurse |
  where -not IsReadOnly |
  Set-ItemProperty -Name IsReadOnly -Value $true

ref.

https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.2

https://learn.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_script_blocks?view=powershell-7.2

このスクラップは2022/10/19にクローズされました