使用powershell编写新的powershell窗口脚本吗? [英] Script a new powershell window with powershell?

查看:69
本文介绍了使用powershell编写新的powershell窗口脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动启动一个Powershell并从另一个Powershell运行一些命令(由于功能的优势,我想从另一个Powershell启动它).我想这样做是因为我正在使用powershell测试用C#编码的* .dll,并且由于无法在powershell中卸载dll而必须不断启动和退出powershell,因此无法用其重新加载库课.

I would like to automatically launch a powershell and run a few commands from another powershell (I want to start it from another powershell because of the advantages of functions).
I would like to do so because I'm testing a *.dll coded in C# with powershell and I have to constantly launch and quit powershell because it's not possible to unload a dll in powershell and therefore it's not possible to reload the library with its classes.

是否有一种方法可以自动执行Powershell,就像使用Office进行对象自动化一样?

Is there a way to automate powershell just like the com-object automation with office?

推荐答案

仅使用脚本或脚本块怎么办?

What about just using a script or script blocks?

powershell {
   Add-Type foo.dll
   [Foo]::DoSomething();
}

应该工作.

如果您需要交互性并且需要随意尝试多个命令,则可以使用

If you need interactivity and need to try multiple commands at will, you could use

powershell -noexit { Add-Type foo.dll }

在这种情况下,至少更改提示颜色可能很有用,这样您就知道您是在测试子外壳程序中还是在父子外壳程序中:

In that case it can be useful to at least change the prompt color so you know whether you're in the test sub-shell or in the parent one:

function Test-DLL {
  powershell -noexit {
    function prompt {
      Write-Host -n -fore yellow "Test $PWD>"
      ' '
    }

    Add-Type foo.dll
  }
}

我也倾向于通过以下方式定义一个小函数:

I tend to define a small function in the following way too:

"function $([char]4) { exit }" | Invoke-Expression

这使我可以使用 Ctrl + D Enter (类似于Unix shell的一半)关闭PowerShell.

which allows me to close PowerShell with Ctrl+D, Enter (half an analog to Unix shells).

这篇关于使用powershell编写新的powershell窗口脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆