一系列文件夹的符号链接

Symbolic Links for series of folders

我正在尝试编写一个部署脚本,该脚本将为 .exe 文件嗅探一组文件夹(始终在更新),并为机器上所有用户的目标目录中的每个文件夹创建一个快捷方式(a供应商提供价格指南,每个指南都有自己的源文件夹和文件,为了方便最终用户,我们的帮助台为每个价格指南创建了一个快捷方式。

流程目前是手动的,我希望将其自动化。源文件总是在更新,所以我不想硬编码任何名称。

我可以 运行 生成我希望为其创建快捷方式的所有 .exe 文件:

Get-ChildItem -Path C:\dirSupportFiles -Include "*.exe" -Recurse |
    ForEach-Object { Write-Verbose "List of Shortcut Files: $_" -Verbose }

结果:

VERBOSE: List of Shortcut Files: C:\dirSupportFiles\ESRDPC151\ESMGR151.EXE
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\ESRDPC151\FujitsuNetCOBOL.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\ESRDPC160\ESMGR160.EXE
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\ESRDPC170\ESMGR170.EXE
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\HHAPRC152\HHDRV152.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\HOSPC16B\HOSP_PC_FY16_V162.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\INPPC17B\INP_PC_FY17.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\INPPRC154\INDRV154.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\INPPRC161\INDRV161.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\IPFPRC150\IPF.EXE
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\IPFPRC160\IPF_PC_FY16.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\IRFPRC150\IRF.EXE
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\IRFPRC160\IRF_PC_FY16.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\LTCHPC15D\LTCH_PC_FY15.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\LTCHPC16B\LTCH_PC_FY16.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\SNFPC16E\SNF_PC_FY16.exe
VERBOSE: List of Shortcut Files: C:\dirSupportFiles\SNFPC17B\SNF_PC_FY17.exe

因此,为了将其改编成脚本来编写快捷方式,我尝试使用 New-Item -ItemType SymbolicLink cmdlet 来执行此操作,但我在使其按我希望的方式工作时遇到了问题:

##variable defined for copying data into user appdata folders
$Destination = "C:\users\"

##variable defined for copying data into user appdata folders
$Items = Get-ChildItem -Path $Destination -Exclude public,ADMIN*,defaultuser0

Get-ChildItem -Path C:\dirSupportFiles -Include "*.exe" -Recurse |
    ForEach-Object {
        New-Item -Itemtype SymbolicLink -Path $Item\Desktop\ -Name "NAME OF OBJECT" -Target $_
    }

关于NAME OF OBJECT:我希望将快捷方式名称写成与文件名相同,但我无法让它工作。当我 运行 命令时,它只会写入一个快捷方式,因为每次它尝试写入下一个时,脚本都会因 ResourceExists 异常而出错。

有没有人对此有任何意见,或者我是否应该考虑其他方法?我对其他方法持开放态度,但最终使用 PS App Deploy Toolkit 包装它。

ForEach-Object 进程块中,$_ 魔法变量不仅指名称,而且还包含对 FileInfo 对象的引用,这意味着您可以使用它来访问相应文件的多个属性:

$Destination = "C:\users"

foreach($Item in Get-ChildItem -Path $Destination -Exclude public,ADMIN*,defaultuser0){

    Get-ChildItem -Path C:\dirSupportFiles -Include "*.exe" -Recurse |ForEach-Object {
        New-Item -Itemtype SymbolicLink -Path $Item\Desktop\ -Name $_.BaseName -Target $_.FullName
    }
}

注意 ForEach-Object 块中 $_.BaseName$_.FullName 的使用