列出 'target' 个 'links'

List 'target' of 'links'

我需要在快捷方式 link 中获取 'target' 到另一台服务器。 但是……当我使用 -Recurse 时,它遵循 links,而不是简单地获取快捷方式目标。

这是我在网上找到的代码,我根据自己的目的对其进行了编辑。但它进入 links 并试图在另一个服务器中找到快捷方式:

#Created By Scott

$varLogFile = "E:\Server\GetBriefsTargets.log"
$varCSVFile = "E:\Server\GetBriefsTargets.csv"

function Get-StartMenuShortcuts
{
    $Shortcuts = Get-ChildItem -Recurse "F:\Connect" -Include *.lnk
    #$Shortcuts = Get-ChildItem -Recurse "D:\Scripts\scott_testing" -Include *.lnk
    $Shell = New-Object -ComObject WScript.Shell
    foreach ($Shortcut in $Shortcuts)
    {
        $Properties = @{
            ShortcutName = $Shortcut.Name;
            ShortcutFull = $Shortcut.FullName;
            ShortcutPath = $shortcut.DirectoryName
            Target = $Shell.CreateShortcut($Shortcut).targetpath
        }
        New-Object PSObject -Property $Properties
    }

    [Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null
}

$Output = Get-StartMenuShortcuts
$Output
ECHO "$Output" >> $varLogFile
ECHO "$Output" >> $varCSVFile

有人可以就我可以更改的内容提供建议,以便它仍然可以在所有文件夹中找到所有快捷方式 links 吗?并停止进入那些 links?

即:

F:\Connect\CLIENTA\shortcut.lnk
F:\Connect\CLIENTB\shortcut.lnk
等等

有大约 100 个客户我必须得到他们的 link,我不想手动(每个月)。

这是我在尝试 运行 这个脚本时遇到的错误:

Get-ChildItem : The specified path, file name, or both are too long. The fully
qualified file name must be less than 260 characters, and the directory name must
be less than 248 characters.
At D:\Scripts\scott_testing\GetShortcutTarget_edit1.ps1:8 char:18
+     $Shortcuts = Get-ChildItem -Recurse "F:\Connect" -Include *.lnk
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ReadError: (F:\Connect\CLIENTA\briefs\FILENAME:String) [Get-ChildItem], PathTooLongException
    + FullyQualifiedErrorId : DirIOError,Microsoft.PowerShell.Commands.GetChildItemCommand

它正在 link 的 'inside' 并试图在另一台服务器上找到快捷方式。

我无法重现。目标应该都找到了。

$output 行在控制台中显示的结果应该显示所有内容。如果不是,请用实际输出更新问题,即所需输出。

我在这里看到的唯一错误是您尝试使用输出(文本)重定向来保存 CSV 对象。那只会 return 对象的字符串表示,这不是 CSV。在这种情况下,当我尝试它时它不会输出任何内容,因为 $output 是一个对象数组,当调用 ToString() 方法时 return 什么也没有。

替换:

ECHO "$Output" >> $varCSVFile

与:

$Output | Select ShortcutName, ShortcutFull, ShortcutPath, Target | Export-CSV -Path $varCSVFile -NoTypeInformation

示例输出:

"ShortcutName","ShortcutFull","ShortcutPath","Target"
"WinSystem.lnk","C:\Users\frode\Desktop\test\WinSystem.lnk","C:\Users\frode\Desktop\test","C:\Windows\System"
"Lol.lnk","C:\Users\frode\Desktop\Lol.lnk","C:\Users\frode\Desktop","C:\Windows\System32\notepad.exe"
"Windows.lnk","C:\Users\frode\Desktop\Windows.lnk","C:\Users\frode\Desktop","\localhost\c$\Windows"
"WinSystem32.lnk","C:\Users\frode\Desktop\WinSystem32.lnk","C:\Users\frode\Desktop","\127.0.0.1\c$\Windows\System32"

UPDATE 如果你想让它做一个递归搜索,你可以尝试这样的事情。请注意 "second-level" 快捷方式的目标(例如 \server\share\shortcutb.nk 可能将 c:\temp 作为目标,这意味着您将获得本地路径而不是远程计算机的 UNC(请参阅 MyEnd.lnk 在下面的示例输出中。

警告:这很容易导致无限循环(循环引用)或长时间 运行 搜索(因为递归搜索)。

 function Get-StartMenuShortcuts ($path) {
  $Shortcuts = Get-ChildItem -Path $path -Recurse -Include *.lnk
    #$Shortcuts = Get-ChildItem -Recurse "D:\Scripts\scott_testing" -Include *.lnk
    $Shell = New-Object -ComObject WScript.Shell
    foreach ($Shortcut in $Shortcuts)
    {
        $Properties = @{
            ShortcutName = $Shortcut.Name;
            ShortcutFull = $Shortcut.FullName;
            ShortcutPath = $shortcut.DirectoryName
            Target = $Shell.CreateShortcut($Shortcut).targetpath
        }

        Get-StartMenuShortcuts -path $Properties.Target

        New-Object PSObject -Property $Properties
    }

[Runtime.InteropServices.Marshal]::ReleaseComObject($Shell) | Out-Null

}

$output = Get-StartMenuShortcuts -path "F:\Connect"

示例输出:

"ShortcutName","ShortcutFull","ShortcutPath","Target"
"WinSystem.lnk","C:\Users\frode\desktop\test\WinSystem.lnk","C:\Users\frode\desktop\test","C:\Windows\System"
"MyEnd.lnk","\127.0.0.1\c$\EFI\MyEnd.lnk","\127.0.0.1\c$\EFI","C:\Users\frode\Desktop\TheEnd"
"EFI.lnk","C:\Users\frode\desktop\EFI.lnk","C:\Users\frode\desktop","\127.0.0.1\c$\EFI"
"Lol.lnk","C:\Users\frode\desktop\Lol.lnk","C:\Users\frode\desktop","C:\Windows\System32\notepad.exe"

如果你只想得到最深的层次,你应该为每个递归调用增加的对象添加一个"level"-计数器,然后只保留最高级的等等。它可能会变得复杂,具体取决于你需要这样就需要一个单独的详细问题。

我找到了一个可以在命令提示符下运行的命令,但似乎无法与 powershell 一起运行:

DIR /AL /S F:\Connect

运行花了一些时间...但它做到了。

此外,我发现一个程序可以在大约 2 秒内完成: http://sumtips.com/2012/10/find-symbolic-links-ntfs-junction-points-windows.html

我试图找到 'symbolic links' ...我正在陈述的目标。