找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 13910|回复: 4

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

[复制链接]
发表于 2022-10-31 16:42:09 | 显示全部楼层 |阅读模式
本帖最后由 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. }

复制代码
发表于 2022-10-31 17:37:15 | 显示全部楼层
  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();
复制代码
 楼主| 发表于 2022-11-1 09:15:19 | 显示全部楼层
回复 2# zaqmlp


    多谢,解决了我的问题。已发支付宝
发表于 2022-11-1 10:40:08 | 显示全部楼层
本帖最后由 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)
复制代码
 楼主| 发表于 2022-11-1 15:34:32 | 显示全部楼层
回复 4# 5i365


    谢谢你的回复,这样是报错的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 01:37 , Processed in 0.014923 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表