以下腳本使用.Shell獲取的快捷方式的源文件,我想改成使用調用外部命令行的方式來實現。
但當我把19行 $p = $shell.($_). 這一行代碼替換成
復制代碼
- $OutputVariable = (cmd.exe /c "E:\Shortcut.exe" /F:$_ /A:Q | findstr TargetPath=) | Out-String
- $p = $OutputVariable.Substring(11)
就提示找不到源文件復制找不到源文件,如果能解決復制找不到源文件,可以給與報酬
由于論壇無法上傳附件,外部命令行程序.exe見網址: ... amp;fweb=1&cl=1
下面是整個腳本代碼
復制代碼
- using namespace 'Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes'
- using namespace 'System.Windows.Forms'
- using namespace 'System.Collections.Specialized';cls
- [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
- Add-Type -Name 'WinApi' -MemberDefinition '[DllImport("user32.dll")]public static extern short GetKeyState(uint VK);'
- $shell = New-Object -ComObject 'WScript.Shell'
- $paths = New-Object 'StringCollection'
- $arr_err = New-Object 'System.Collections.ArrayList'
- if([WinApi]::GetKeyState(16) -band 0x8000){ write-host 'shift';$paths = [Clipboard]::GetFileDropList() }
- $args | foreach { [void]$paths.Add($_) }
- $paths = &{
- $paths | Select-Object -Unique | foreach {
- #目錄
- if([System.IO.Directory]::Exists($_)){ return $_ }
- #文件
- if([System.IO.File]::Exists($_)){
- #lnk文件
- if($_.EndsWith('.lnk')){
- $p = $shell.CreateShortcut($_).TargetPath
- if([System.IO.File]::Exists($p)){ return $p }
- if([System.IO.Directory]::Exists($p)){ return $p }
- ? ?? ?? ?? ?? ? [void]$arr_err.Add(('{0} -> {1}' -f $_,$p))
- return $null
- }
- #其它文件
- return $_
- }
- } | Select-Object -Unique
- }
- if($paths.Count -gt 0){
- [Clipboard]::SetFileDropList($paths)
- 'ok'
- $paths
- '-----------'
- }
- if($arr_err.Count -gt 0){
- 'fail'
- $arr_err
- '-----------'
- pause
- }