2017-04-17 209 views
0

我想写一个部署脚本将嗅出一组文件夹(总是被更新)为.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进行封装。

+2

'{新建项目-Itemtype SymbolicLink -Path $项目\桌面\ -Name $ _。名称 - 目标$ _。全名}' – Swonkie

+0

由于内部使用$_.BaseName$_.FullName Swonkie,这是诀窍!干杯! – JanBan1221

回答

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

通知的ForEach-Object

+0

感谢您的解释!现在很好用! – JanBan1221