windows - Sådan ændres filtrering i Powershell-script for at oprette dynamisk startmenu med .url og .website?

Indlæg af Hanne Mølgaard Plasc

Problem



Opdatering: Jeg forsøger nu at bruge en kommando Get-StartApps i stedet for recursivt at gå gennem filerne med et filter. Nu har jeg brug for hjælp til at skrive om logikken bag indsatssektionerne nævnt i nr. 3 nedenfor. Eventuelle forslag ville være fantastisk!


Jeg har fundet og har arbejdet med en række scripts, der kan bruges til at oprette en startmenu XML. Den version jeg har afgjort på, er nedenfor. Desværre ser jeg ikke ud til at få det til at håndtere '.URL', '.websted', '.shortcut' -udvidelser.


Jeg har prøvet følgende (en ad gangen og sammen):



  1. On line 90 fjernelse af filteret '* .lnk'

  2. Tilføjelse -filter ' .website', -Filter ' .shortcut' ... til linje 90

  3. Oprettelse af ifelse-udsagn for hver af disse udvidelser modelleret efter linjerne 107-116


      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) 
        }
    }
    



Intet af dette ser ud til at virke, når man forsøger at hente links, der er placeret i startmenumappen. Har nogen stødt på dette? Har du råd til, hvordan du løser dette? Hele scriptet nedenfor:


        # Where to save start menu xml
#$StartMenuFile = "$ENV:programfilesDESKTOPENGINEERINGstartmenustartmenu.xml"
$StartMenuFile = "$ENV:programfilesDESKTOPENGINEERINGstartmenustartmenu.xml"
#$OldStartMenuFile = "$ENV:programfilesDESKTOPENGINEERINGstartmenustartmenu.xml"
$OldStartMenuFile = "$programfilesDESKTOPENGINEERINGstartmenustartmenu.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:PROGRAMDATAMicrosoftWindowsStart 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 ###

Bedste reference


Ok, så var der en todelt fix. I stedet for rekursivt at søge i mapperne brugte jeg den indbyggede i $SoftwareLinks = Get-StartApps, som løste mit første problem og derefter skrev om insert-strengen for at matche det nye format. Mange tak for alle dine tilbagemeldinger.