[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 求助,在powershell脚本中如何捕获命令行的输出

本帖最后由 g495326 于 2022-10-31 17:07 编辑

以下脚本使用WScript.Shell获取的快捷方式的源文件,我想改成使用调用外部命令行的方式来实现。
但当我把19行 $p = $shell.CreateShortcut($_).TargetPath 这一行代码替换成
  1. $OutputVariable = (cmd.exe /c "E:\Shortcut.exe" /F:$_ /A:Q | findstr TargetPath=) | Out-String
  2. $p = $OutputVariable.Substring(11)
复制代码
就提示找不到源文件,如果能解决,可以给与报酬

由于论坛无法上传附件,外部命令行程序Shortcut.exe见网址:https://wx.mail.qq.com/ftn/downl ... amp;fweb=1&cl=1


下面是整个脚本代码
  1. using namespace 'Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes'
  2. using namespace 'System.Windows.Forms'
  3. using namespace 'System.Collections.Specialized';cls
  4. [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
  5. Add-Type -Name 'WinApi' -MemberDefinition '[DllImport("user32.dll")]public static extern short GetKeyState(uint VK);'
  6. $shell = New-Object -ComObject 'WScript.Shell'
  7. $paths = New-Object 'StringCollection'
  8. $arr_err = New-Object 'System.Collections.ArrayList'
  9. if([WinApi]::GetKeyState(16) -band 0x8000){ write-host 'shift';$paths = [Clipboard]::GetFileDropList() }
  10. $args | foreach { [void]$paths.Add($_) }
  11. $paths = &{
  12. $paths | Select-Object -Unique | foreach {
  13. #目录
  14. if([System.IO.Directory]::Exists($_)){ return $_ }
  15. #文件
  16. if([System.IO.File]::Exists($_)){
  17. #lnk文件
  18. if($_.EndsWith('.lnk')){
  19. $p = $shell.CreateShortcut($_).TargetPath
  20. if([System.IO.File]::Exists($p)){ return $p }
  21. if([System.IO.Directory]::Exists($p)){ return $p }
  22.                 [void]$arr_err.Add(('{0} -> {1}' -f $_,$p))
  23. return $null
  24. }
  25. #其它文件
  26. return $_
  27. }
  28. } | Select-Object -Unique
  29. }
  30. if($paths.Count -gt 0){
  31. [Clipboard]::SetFileDropList($paths)
  32. 'ok'
  33. $paths
  34. '-----------'
  35. }
  36. if($arr_err.Count -gt 0){
  37. 'fail'
  38. $arr_err
  39. '-----------'
  40. pause
  41. }
复制代码

  1. $p='';
  2. $process=New-Object System.Diagnostics.Process;
  3. #$process.StartInfo.WindowStyle=[System.Diagnostics.ProcessWindowStyle]::Hidden;
  4. $process.StartInfo.UseShellExecute=$false;
  5. $process.StartInfo.RedirectStandardOutput=$true;
  6. $process.StartInfo.FileName='E:\Shortcut.exe';
  7. $process.StartInfo.Arguments='/F:"'+$_+'" /A:Q';
  8. [void]$process.Start();
  9. $result=$process.StandardOutput.ReadToEnd();
  10. $m=[regex]::match($result, 'TargetPath=([^\r\n]+)');
  11. if($m.Success){$p=$m.groups[1].value;}
  12. $process.WaitForExit();
  13. $process.Dispose();
复制代码
提供bat代写,为你省时省力省事,支付宝扫码头像支付
微信: unique2random

TOP

回复 2# zaqmlp


    多谢,解决了我的问题。已发支付宝

TOP

本帖最后由 5i365 于 2022-11-1 10:41 编辑

多分解一行应该可以:

  1. $a = cmd.exe /c "E:\Shortcut.exe" /F:$_ /A:Q
  2. $OutputVariable = $a | findstr TargetPath= | Out-String
  3. $OutputVariable.Substring(11)
复制代码

________________________________________________
下面测试能过:
  1. $p = "$HOME\Desktop\word.lnk"
  2. $a = cmd.exe /c "E:\Shortcut.exe" /F:$p /A:Q
  3. $OutputVariable = $a | findstr TargetPath= | Out-String
  4. $OutputVariable.Substring(11)
复制代码
本人所发所有贴子或代码, 诸大侠若认为有改进之处,请不吝赐教,感激不尽!

TOP

回复 4# 5i365


    谢谢你的回复,这样是报错的

TOP

返回列表