一系列文件夹的符号链接

时间:2017-04-17 13:26:44

标签: powershell symlink

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

流程目前是手动的,我希望自动化它。源文件总是在更新,所以我宁愿不对任何名称进行硬编码。

我可以运行以下命令来生成我希望为其创建快捷方式的所有.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将其包装起来。

1 个答案:

答案 0 :(得分:1)

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

请注意在$_.BaseName块中使用$_.FullNameForEach-Object