第一次在這里發帖并使用 PowerShell 完成初學者(以及一般的編碼),如果我遺漏了一些非常明顯的東西,我深表歉意。
我有一個包含三列的文本檔案(以“:”分隔)。第一個包含我想創建的快捷方式的路徑。第二個包含目標路徑。第三列包含應應用于快捷方式的相應 ICO 檔案的路徑。
我一直在嘗試各種方法來表達代碼,但我的基礎知識有點太弱,無法真正有效地解決問題,所以我主要是在黑暗中蹣跚而行。這是我認為可以解決問題的方法:
$Contents = Get-Content "D:\Projects\Learning To Code\PowerShell\Commands and Scripts\Shortcuts List.txt"
ForEach ($Line in $Contents) {
$ShortcutFile = "`"" "D:\Projects\PowerShell\Test Environment\" $Contents[0].Split(":")[1] ".lnk`""
$TargetFile = "`"" "D:\Projects\PowerShell\Test Environment\" $Contents[1].Split(":")[1] "`""
$IconFile = "`"" "D:\Miscellaneous\Other\Backend\Icons\" $Contents[2].Split(":")[1] ".ico`""
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.IconLocation = $IconFile
$Shortcut.Save()
}
“Shortcuts Lists.txt”的內容如下:
Random Path 1\Name A – Title A:Random Path 2\Name A – Title A:Random Path 3\Name A – Title A
Random Path 1\Name B – Title B:Random Path 2\Name B – Title B:Random Path 3\Name B – Title B
Random Path 1\Name C – Title C:Random Path 2\Name C – Title C:Random Path 3\Name C – Title C
Random Path 1\Name D – Title D:Random Path 2\Name D – Title D:Random Path 3\Name D – Title D
Random Path 1\Name E – Title E:Random Path 2\Name E – Title E:Random Path 3\Name E – Title E
Random Path 1\Name F – Title F:Random Path 2\Name F – Title F:Random Path 3\Name F – Title F
Random Path 1\Name G – Title G:Random Path 2\Name G – Title G:Random Path 3\Name G – Title G
Random Path 1\Name H – Title H:Random Path 2\Name H – Title H:Random Path 3\Name H – Title H
Random Path 1\Name I – Title I:Random Path 2\Name I – Title I:Random Path 3\Name I – Title I
Random Path 1\Name J – Title J:Random Path 2\Name J – Title J:Random Path 3\Name J – Title J
Random Path 1\Name K – Title K:Random Path 2\Name K – Title K:Random Path 3\Name K – Title K
Random Path 1\Name L – Title L:Random Path 2\Name L – Title L:Random Path 3\Name L – Title L
Random Path 1\Name M – Title M:Random Path 2\Name M – Title M:Random Path 3\Name M – Title M
Random Path 1\Name N – Title N:Random Path 2\Name N – Title N:Random Path 3\Name N – Title N
Random Path 1\Name O – Title O:Random Path 2\Name O – Title O:Random Path 3\Name O – Title O
Random Path 1\Name P – Title P:Random Path 2\Name P – Title P:Random Path 3\Name P – Title P
Random Path 1\Name Q – Title Q:Random Path 2\Name Q – Title Q:Random Path 3\Name Q – Title Q
A bit more debugging has suggested that the root of the problem might lie with the Em Dash that's present in the shortcut path and target path for each line. Using the Write-Output $ShortcutFile command shows that the Em Dash gets replaced by `a€“
Renaming all the files to replace the Em Dash isn't really an option so I'm hoping there's an alternative solution.
UPDATE: I've managed to circumvent the issue with Em Dash by replacing it with a unique string in the text file and adding a "-Replace" argument to the main code, but still no dice on the output. The Write-Output $ShortcutFile command shows that the code isn't going through line by line but just repeating the first line over and over. And I'm still getting the error telling me that the $Shortcut must end with ".lnk" or ".url"
uj5u.com熱心網友回復:
您可以將那些 EM 破折號替換為
$inputFile = "D:\Projects\Learning To Code\PowerShell\Commands and Scripts\Shortcuts List.txt"
(Get-Content -Path $inputFile -Raw) -replace '\p{Pd}','-' |
Set-Content -Path $inputFile -Force
正則運算式的\p{Pd}意思是“任何型別的連字符或破折號”
那么它應該是可以使用匯入-CSV上
# define the COM object only once
$WScriptShell = New-Object -ComObject WScript.Shell
# read and parse the CSV. Supply headers and define the delimiter used
$data = Import-Csv -Path $inputFile -Header 'Shortcut','Target','Icon' -Delimiter ':'
# loop through the data
foreach ($item in $data) {
$ShortcutFile = Join-Path -Path "D:\Projects\PowerShell\Test Environment" -ChildPath ('{0}.lnk' -f $item.Shortcut)
$TargetFile = Join-Path -Path "D:\Projects\PowerShell\Test Environment" -ChildPath $item.Target
$IconFile = Join-Path -Path "D:\Miscellaneous\Other\Backend\Icons" -ChildPath ('{0}.ico' -f $item.Icon)
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.IconLocation = $IconFile
$Shortcut.Save()
}
# finally remove the used COM objects from memory
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Shortcut)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($WScriptShell)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
uj5u.com熱心網友回復:
這是您希望資料格式化的方式嗎?這是資料的第一行,如果這是正確的,那么它也可以回圈用于其他行,并且可以使用其余代碼創建快捷方式。
$line = "Random Path 1\Name A – Title A:Random Path 2\Name A – Title A:Random Path 3\Name A – Title A"
$array = $line.split(":")
$array
$shortcutFile = "D:\Projects\PowerShell\Test Environment\" $array[0] ".lnk"
$targetFile = "D:\Projects\PowerShell\Test Environment\" $array[1]
$iconFile = "D:\Miscellaneous\Other\Backend\Icons\" $array[2] ".ico"
Write-Output $shortcutFile
Write-Output $targetFile
Write-Output $iconFile
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/358652.html
標籤:powershell
