1

更新:我现在尝试使用 Get-StartApps 命令,而不是使用过滤器递归地浏览文件。现在我需要帮助重写下面#3 中提到的插入部分背后的逻辑。任何建议都会很棒!

我发现并一直在使用一系列可用于创建开始菜单 XML 的脚本。我确定的版本如下。不幸的是,我似乎无法让它处理“.URL”、“.Website”、“.shortcut”扩展名。

我尝试了以下方法(一次一个并一起尝试):

  1. 在第 90 行删除 -filter "*.lnk"
  2. 添加 -filter " .website", -Filter " .shortcut"... 到第 90 行
  3. 为第 107-116 行建模的每个扩展创建 ifelse 语句

      if ($SoftwareLinks.Name -like "$Software.lnk") {
        $SoftwareLink = $SoftwareLinks | where {$_ -like "$Software.lnk"} 
        $child = $StartMenuXml.CreateElement("start","DesktopApplicationTile","http://schemas.microsoft.com/Start/2014/StartLayout")
    
        if ($SoftwareLink.FullName.GetType().BaseType -eq [System.Array]) { ## If multiple links, use the first one
            $child.SetAttribute('DesktopApplicationLinkPath',$SoftwareLink.FullName[0]) 
        } else {
            $child.SetAttribute('DesktopApplicationLinkPath',$SoftwareLink.FullName) 
        }
    }
    

尝试获取放置在开始菜单文件夹中的链接时,这些似乎都不起作用。有没有人遇到过这个?你对如何解决这个问题有什么建议吗?整个脚本如下:

        # Where to save start menu xml
#$StartMenuFile = "$ENV:programfiles\DESKTOPENGINEERING\startmenu\startmenu.xml"
$StartMenuFile = "$ENV:programfiles\DESKTOPENGINEERING\startmenu\startmenu.xml"
#$OldStartMenuFile = "$ENV:programfiles\DESKTOPENGINEERING\startmenu\startmenu.xml"
$OldStartMenuFile = "$programfiles\DESKTOPENGINEERING\startmenu\startmenu.old"


# Set this to SilentlyContinue for no debug, or Continue for debug output
$DebugPreference = "SilentlyContinue"

# Remove old startmenu.old file
IF (Test-path $OldStartMenuFile) { 
    Write-Debug "The file `"$OldStartMenuFile`" already exists and will be removed!"
    Remove-item $OldStartMenuFile -Force
} Else { 
    Write-Debug "The file `"$OldStartMenuFile`" does not exist! Lets move along then..."
}

# Rename startmenu.xml to startmenu.old
IF (Test-path $StartMenuFile) { 
    Write-Debug "renaming file `"$OldStartMenuFile`"..."
    Rename-Item -Path $StartMenuFile -NewName $OldStartMenuFile -Force
} Else { 
    Write-Debug "The file `"$OldStartMenuFile`" does not exist! Lets move along then..."
}

# One last check to see if file exists or not
IF (Test-path $StartMenuFile) { 
    Write-Error "Could not rename `"$OldStartMenuFile`", script aborted"
    Break
} Else { 
    Write-Debug "The file `"$OldStartMenuFile`" does not exist! Lets move along then..."
}

# Make sure folder exist and halt if it can't be created
$StartMenuFolder=(Split-path -parent $StartMenuFile)
IF (Test-path $StartMenuFolder) { } ELSE  { New-Item -ItemType Directory -Path $StartMenuFolder }
IF (Test-path $StartMenuFolder) { } ELSE  { Write-Error "Could not create `"$StartMenuFolder`", script aborted" ; Break }

# Specify number of cols in startmenu
$NumCol = 6
# Add the new group in $MenuGroups
# Format: "order. group title" = "list of Software Links", "Followed by other links"
$MenuGroups = @{
    "1. Internet & Network tools" = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"
    "2. Microsoft Office" = "Access 2016","Excel 2016","Outlook 2016","PowerPoint 2016","Project 2016","Publisher 2016","Word 2016"
    "3. Text, file & programming tools" = "Calculator","Notepad"
    "4. Media tools" = "Paint"
    "5. Scientific software" = "Calculator"
    "6. Administrator" = "Powershell"
    "7. Other tools" = "Google Chrome", "Google Link"
}

# Building up base startmenu xml
[xml]$StartMenuXml = '<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">
  <LayoutOptions StartTileGroupsColumnCount="1" StartTileGroupCellWidth="'+$NumCol+'" />
  <DefaultLayoutOverride LayoutCustomizationRestrictionType="OnlySpecifiedGroups">
    <StartLayoutCollection>
      <defaultlayout:StartLayout GroupCellWidth="'+$NumCol+'" xmlns:defaultlayout="http://schemas.microsoft.com/Start/2014/FullDefaultLayout">
      </defaultlayout:StartLayout>
    </StartLayoutCollection>
  </DefaultLayoutOverride>
  <CustomTaskbarLayoutCollection PinListPlacement="Replace">
  <defaultlayout:TaskbarLayout>
   <taskbar:TaskbarPinList>
    <taskbar:UWA AppUserModelID="Microsoft.WindowsCalculator_8wekyb3d8bbwe!App"/>
   </taskbar:TaskbarPinList>
  </defaultlayout:TaskbarLayout>
 </CustomTaskbarLayoutCollection>
</LayoutModificationTemplate>' 
# Selecting XML element where all software will be placed
$DefaultLayoutElement = $StartMenuXml.GetElementsByTagName("defaultlayout:StartLayout")
# Fetching all software links on start menu
$SoftwareLinks = Get-ChildItem "$env:PROGRAMDATA\Microsoft\Windows\Start Menu" -recurse -filter "*.lnk"
# Looping all menu groups defined above
foreach ($MenuGroup in $MenuGroups.Keys | Sort-Object) {
    # Init xml element for software group
    $SoftwareGroupXml = $StartMenuXml.CreateElement("start","Group", "http://schemas.microsoft.com/Start/2014/StartLayout")
    $SoftwareGroupXml.SetAttribute('Name',$MenuGroup.Substring(3))
    # Init row and col
    $col = 0
    $row = 0
    # Looping all software links in start menu
    foreach ($Software in $MenuGroups[$MenuGroup]) {
        # Check if it is time for a new col
        if (($col%($NumCol-1) -eq 1) -and ($col -ne 1)) {
            $row +=1
            $col = 0
        }
        # Check if specified software is found in start menu. If so, add software element
        if ($SoftwareLinks.Name -like "$Software.lnk") {
            $SoftwareLink = $SoftwareLinks | where {$_ -like "$Software.lnk"} 
            $child = $StartMenuXml.CreateElement("start","DesktopApplicationTile","http://schemas.microsoft.com/Start/2014/StartLayout")

            if ($SoftwareLink.FullName.GetType().BaseType -eq [System.Array]) { ## If multiple links, use the first one
                $child.SetAttribute('DesktopApplicationLinkPath',$SoftwareLink.FullName[0]) 
            } else {
                $child.SetAttribute('DesktopApplicationLinkPath',$SoftwareLink.FullName) 
            }
        }
        # Or check if Microsoft app is specified. If so add app element    
         elseif ($Software -like "Microsoft.*!*") {
            $child = $StartMenuXml.CreateElement("start","Tile","http://schemas.microsoft.com/Start/2014/StartLayout")
            $child.SetAttribute('AppUserModelID',$Software)
        }
        # Add common attributes is software or app is found and append xml element
        if (($child.HasAttributes) -and (($Software -like "Microsoft.*!*") -or ($SoftwareLinks.Name -like "$Software.lnk"))) {
            $child.SetAttribute('Size','2x2')
            $child.SetAttribute('Column',$col)
            $child.SetAttribute('Row',$row)
            $SoftwareGroupXml.AppendChild($child) | Out-Null
            $col +=1
        }
    }
    # If a software group is not null, add it!
    if ($SoftwareGroupXml.HasChildNodes) {
        $DefaultLayoutElement.AppendChild($SoftwareGroupXml) | Out-Null
    }
}

# Save to file
$StartMenuXml.Save($StartMenuFile)### Script ends ###
4

1 回答 1

1

好的,所以有一个两部分的修复。我没有递归搜索文件夹,而是使用了$SoftwareLinks = Get-StartApps解决我的第一个问题的内置文件夹,然后重新编写了插入字符串以匹配新格式。谢谢大家的反馈。

于 2018-05-03T13:00:40.537 回答