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

[问题求助] 怎样修改powershell ise的F5执行方式?

[复制链接]
发表于 2026-2-23 11:09:15 | 显示全部楼层 |阅读模式
https://www.reddit.com/r/PowerSh ... _hidden/?tl=zh-hant
我在上面贴子发现了下面的代码, 原来powershell ISE是可以自定义的,
现在我有一个功能需求, 就是在按下F5后, 先执行一下cls把输出窗口中的已前的输出先清理一下, 怎样修改呢?
  1. Add-Type -TypeDefinition @"
  2. using System;
  3. using System.Windows.Threading;
  4. using System.Reflection;

  5. namespace ISECustomization
  6. {
  7.     public class EditorCustomization
  8.     {
  9.         private MethodInfo methodToInvoke;
  10.         private object editorOptions;
  11.         private object[] optionsToSet;

  12.         public static void SetEditorOptions(object editor, object[] optionsToSet)
  13.         {
  14.             BindingFlags nonPublicFlags = BindingFlags.NonPublic | BindingFlags.Instance;
  15.             Type editorType = editor.GetType();

  16.             object editorOperations = editorType.GetProperty("EditorOperations", nonPublicFlags).GetMethod.Invoke(editor,null);
  17.             object editorOptions = editorOperations.GetType().GetProperty("Options").GetValue(editorOperations);

  18.             object editorViewHost = editorType.GetProperty("EditorViewHost",nonPublicFlags).GetMethod.Invoke(editor,null);
  19.             object dispatcher = editorViewHost.GetType().GetProperty("Dispatcher").GetValue(editorViewHost);

  20.             var setterInstance = new EditorCustomization()
  21.             {
  22.                 methodToInvoke = editorOptions.GetType().GetMethod("SetOptionValue", new Type[] {typeof(string), typeof(object) }),
  23.                 editorOptions = editorOptions,
  24.                 optionsToSet = optionsToSet
  25.             };
  26.             DispatcherFrame frame = new DispatcherFrame();
  27.             (dispatcher as Dispatcher).BeginInvoke(DispatcherPriority.Normal, new DispatcherOperationCallback(setterInstance.ExitFrames),frame);
  28.             Dispatcher.PushFrame(frame);
  29.         }

  30.         private object ExitFrames(object f)
  31.         {
  32.             DispatcherFrame df = ((DispatcherFrame)f);
  33.             foreach (object[] args in optionsToSet)
  34.             {
  35.                 methodToInvoke.Invoke(editorOptions, args);
  36.             }
  37.             df.Continue = false;
  38.             return null;
  39.         }
  40.     }
  41. }
  42. "@ -ReferencedAssemblies windowsbase

  43. $Settings=@(
  44.     ,@('Adornments/HighlightCurrentLine/Enable',$true)
  45.     ,@('TextViewHost/ChangeTracking',$true)
  46.     ,@('TextView/WordWrapStyle',[Microsoft.VisualStudio.Text.Editor.WordWrapStyles]7)
  47.     ,@('TextView/UseVisibleWhitespace',$true)
  48.     ,@('TextView/UseVirtualSpace',$true)
  49. )

  50. [ISECustomization.EditorCustomization]::SetEditorOptions($psISE.CurrentFile.Editor,$Settings)
复制代码
发表于 2026-2-23 21:42:20 | 显示全部楼层
第一行写cls不就行了
 楼主| 发表于 2026-2-24 06:49:45 | 显示全部楼层
回复 2# went


    我就是不想每次都添加cls, 想实现默认就追加了cls
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2026-3-16 19:14 , Processed in 0.026131 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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