1

I am struggling with something that should be obviously simple but seems not so straightforward to solve. I have a dynamic sequence of powershell commands to run. It is a part of some templating I wrote for myself to make myself easier in my workflow.

Here is a simple example of a generated grouped commands:

$commands = {
  Write-Output "Navigating to tools->deploy";
  Set-Location "C:\tools\deploy\";
  Write-Output "OK. Run dd.ps1 [server name] to deploy.";
}

It is possible to run this $commands by using Invoke-Command. So eventually I have this function that handles it:

function invokeNow() {
  param([string]$c)
  Invoke-Command $c;
} 

This works fine. It executes the commands in the object in the window. But with the time, I have added more functionality to it. For each new instruction, I have to open powershell each time for that... That is because sometimes, when a script is running I cannot issue another function. So, my goal now is to run the above command group in a new window so that I can use one same shell window to invoke multiple tasks (that runs in their own windows). I thought that it would be simple, so I altered the function to

function invokeNow() {
  param([string]$c)
  Invoke-Expression "cmd /c start powershell -Command {$c} -NoProfile -NoExit";
} 

That leads to this result:

#< CLIXML
#< CLIXML
PS C:\tools\deploy\>

The navigation did work. However, my output information is lost. Some commands have added instructions like deploy S1 foo or deploy FS -src "data". Those are hard to memorize sometimes. For that, I have two small questions: (1) Where is my Write-Output data? And (2) what is that weird CLIXML thing (seems "special" powershell XML according to google)? When I issue exit to close that window, I see a bunch of XML stuff before the window closes.

Main question

Is it possible to open a new powershell window, then execute the commands in my $commands object correctly (thus including the Write-Output data)?

I already have tried to add Invoke-Command to the above like

Invoke-Expression "cmd /c start powershell -Command {Invoke-Command -ScriptBlock $c} -NoProfile -NoExit";

but the new window spits a lot XML stuff...

ps madness


version info

PS C:\> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.16299.251
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.251
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
  • I guess my first question would be why? Why do you think you need to store the commands as script blocks then execute them in a separate console. Why not use functions and jobs? – EBGreen Apr 16 '18 at 12:10
  • A group is generated by calling multiple functions: eg Write-Output "Navigating to tools->deploy"; Set-Location "C:\tools\deploy\"; comes from one function that navigates you to a directory. By this, I do not have to write both lines multiple times. I would want to trace my directory navigation. I could add function names too, but that is more harder to resolve I believe. – KarelG Apr 16 '18 at 14:35
  • I don't know what you mean by harder to resolve. Regardless, I would suggest simply writing the script to do what you want to do then run the script in the other console. Not sure why you need another console either to be honest with you. – EBGreen Apr 16 '18 at 14:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.