使用PowerShell编辑任务栏布局修改。

huangapple go评论72阅读模式
英文:

Editing Taskbar Layout modification with Powershell

问题

我正在为我的公司准备Windows 11的部署,并找到了一个可以用于设置自定义图标固定到任务栏的XML文件。

我想在安装过程中编辑XML文档,比如说,如果计算机已安装了Office,我想将Word和Outlook图标固定到任务栏。

以下是XML内容:

<?xml version="1.0" encoding="utf-8"?>
<LayoutModificationTemplate
    xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"
    xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout"
    xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout"
    xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout"
    Version="1">
    <CustomTaskbarLayoutCollection PinListPlacement="Replace">
        <defaultlayout:TaskbarLayout>
            <taskbar:TaskbarPinList>
                <taskbar:DesktopApp DesktopApplicationID="Microsoft.Windows.Explorer"/>
                <taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"/>
                <taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" />
                <taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" />
            </taskbar:TaskbarPinList>
        </defaultlayout:TaskbarLayout>
    </CustomTaskbarLayoutCollection>
</LayoutModificationTemplate>

我应该如何在前4个图标之后添加以下行:

<taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk" />

我已经找到了如何在PowerShell中打开XML文件,但我不知道如何在XML的正确部分添加行。

谢谢你的帮助!

英文:

I am preparing Windows 11 deployment for my company and I found an XML that I can use for setting up customised icons pinned to taskbar .

I want to edit XML document during install process - let's say - if computer has Office installed I want Word and Outlook icons pinned to taskbar.

This is the XML:

 &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;LayoutModificationTemplate
	xmlns=&quot;http://schemas.microsoft.com/Start/2014/LayoutModification&quot;
	xmlns:defaultlayout=&quot;http://schemas.microsoft.com/Start/2014/FullDefaultLayout&quot;
	xmlns:start=&quot;http://schemas.microsoft.com/Start/2014/StartLayout&quot;
	xmlns:taskbar=&quot;http://schemas.microsoft.com/Start/2014/TaskbarLayout&quot;
    Version=&quot;1&quot;&gt;
	&lt;CustomTaskbarLayoutCollection PinListPlacement=&quot;Replace&quot;&gt;
		&lt;defaultlayout:TaskbarLayout&gt;
			&lt;taskbar:TaskbarPinList&gt;
				&lt;taskbar:DesktopApp DesktopApplicationID=&quot;Microsoft.Windows.Explorer&quot;/&gt;
				&lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk&quot;/&gt;
				&lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk&quot; /&gt;
				&lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk&quot; /&gt;
			&lt;/taskbar:TaskbarPinList&gt;
		&lt;/defaultlayout:TaskbarLayout&gt;
	&lt;/CustomTaskbarLayoutCollection&gt;
&lt;/LayoutModificationTemplate&gt; 

How can I add a line

				&lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk&quot; /&gt;

to this XML after first 4 icons?

I found out how to open XML in Powershell, but I am lost how to add line in the correct part of XML.

Thank you for your help!

答案1

得分: 1

以下代码可以实现你的要求,你只需要根据你的IF语句或其他条件来决定要添加的内容。目前它只添加了你提供的Outlook示例,但你可以定义任意数量的NewChilds,并在foreach中导入它们:

# 定义XML内容
[xml]$xmlDocument = '<?xml version="1.0" encoding="utf-8"?>
<LayoutModificationTemplate
    xmlns="http://schemas.microsoft.com/Start/2014/LayoutModification"
    xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout"
    xmlns:start="http://schemas.microsoft.com/Start/2014/StartLayout"
    xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout"
    Version="1">
    <CustomTaskbarLayoutCollection PinListPlacement="Replace">
        <defaultlayout:TaskbarLayout>
            <taskbar:TaskbarPinList>
                <taskbar:DesktopApp DesktopApplicationID="Microsoft.Windows.Explorer"/>
                <taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"/>
                <taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk" />
                <taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" />
            </taskbar:TaskbarPinList>
        </defaultlayout:TaskbarLayout>
    </CustomTaskbarLayoutCollection>
</LayoutModificationTemplate>'

# 定义要添加的子节点
[xml]$NewChild = @"
<Grid xmlns:taskbar="http://schemas.microsoft.com/Start/2014/TaskbarLayout">
    <taskbar:DesktopApp DesktopApplicationLinkPath="%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk" />
</Grid>
"@

# 创建命名空间管理器,并在TaskbarPinList的最后一个子节点后插入新的子节点
# 由于非默认命名空间"taskbar",所以需要命名空间
$nsManager = New-Object System.Xml.XmlNamespaceManager($xmlDocument.NameTable)
$nsManager.AddNamespace("taskbar", "http://schemas.microsoft.com/Start/2014/TaskbarLayout")
# 将新的子节点导入XML文档以创建节点
$targetNode = $xmlDocument.SelectSingleNode("//taskbar:TaskbarPinList", $nsManager)
# 在目标位置插入节点
$newNode = $xmlDocument.ImportNode($NewChild.Grid.SelectSingleNode('taskbar:DesktopApp', $nsManager),$true)
[VOID]$targetNode.InsertAfter($newNode, $targetNode.LastChild)

# 将XML保存到所需的位置
$xmlDocument.Save('文件路径或包含路径的变量')

请将"文件路径或包含路径的变量"替换为你想要保存XML的路径。

英文:

The below should do the trick, you will just need to add if your IF statements or whatever to decide what you want to add. Currently it only adds the one example you provided for Outlook, but you can define as many NewChilds as you want, and import them in a foreach:

                # Define the XML content
            [xml]$xmlDocument = &#39;&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
            &lt;LayoutModificationTemplate
                xmlns=&quot;http://schemas.microsoft.com/Start/2014/LayoutModification&quot;
                xmlns:defaultlayout=&quot;http://schemas.microsoft.com/Start/2014/FullDefaultLayout&quot;
                xmlns:start=&quot;http://schemas.microsoft.com/Start/2014/StartLayout&quot;
                xmlns:taskbar=&quot;http://schemas.microsoft.com/Start/2014/TaskbarLayout&quot;
                Version=&quot;1&quot;&gt;
                &lt;CustomTaskbarLayoutCollection PinListPlacement=&quot;Replace&quot;&gt;
                    &lt;defaultlayout:TaskbarLayout&gt;
                        &lt;taskbar:TaskbarPinList&gt;
                            &lt;taskbar:DesktopApp DesktopApplicationID=&quot;Microsoft.Windows.Explorer&quot;/&gt;
                            &lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk&quot;/&gt;
                            &lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Firefox.lnk&quot; /&gt;
                            &lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk&quot; /&gt;
                        &lt;/taskbar:TaskbarPinList&gt;
                    &lt;/defaultlayout:TaskbarLayout&gt;
                &lt;/CustomTaskbarLayoutCollection&gt;
            &lt;/LayoutModificationTemplate&gt;&#39;

            # Define the child to be added
            [xml]$NewChild = @&quot;
            &lt;Grid xmlns:taskbar=&quot;http://schemas.microsoft.com/Start/2014/TaskbarLayout&quot;&gt;
                &lt;taskbar:DesktopApp DesktopApplicationLinkPath=&quot;%ALLUSERSPROFILE%\Microsoft\Windows\Start Menu\Programs\Outlook.lnk&quot; /&gt;
            &lt;/Grid&gt;
            &quot;@

            # Create a namespace manager and Insert the new child after the last one under TaskbarPinList
            # Namespace is required due to none default namespace &quot;taskbar&quot;
            $nsManager = New-Object System.Xml.XmlNamespaceManager($xmlDocument.NameTable)
            $nsManager.AddNamespace(&quot;taskbar&quot;, &quot;http://schemas.microsoft.com/Start/2014/TaskbarLayout&quot;)
            #Import the new child into the XML doc to create a node
            $targetNode = $xmlDocument.SelectSingleNode(&quot;//taskbar:TaskbarPinList&quot;, $nsManager)
            #Insert the node where we want it
            $newNode = $xmlDocument.ImportNode($NewChild.Grid.SelectSingleNode(&#39;taskbar:DesktopApp&#39;, $nsManager),$true)
            [VOID]$targetNode.InsertAfter($newNode, $targetNode.LastChild)

            #Save the XML where ever you require
            $xmlDocument.Save(&#39;FILE PATH OR VARIABLE CONTAINING A PATH HERE&#39;)

huangapple
  • 本文由 发表于 2023年7月27日 17:29:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定