简体   繁体   中英

Script a new powershell window with 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.

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();
}

should work.

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

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

也许不是最优雅的解决方案,但应该这样做(我不是PS专业人士):

cmd /c start powershell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM