2017-04-17 205 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