SharpeRAD / Cake.Powershell

Powershell addin for Cake
http://cakebuild.net
MIT License
83 stars 36 forks source link

How to work with session? #69

Closed denis-peshkov closed 5 years ago

denis-peshkov commented 5 years ago

Hi!

I have task to do next powershell script: Copy-Item .\tslint.json -Destination C:\WebSites\tml2\ -ToSession $session

I did:

#addin "Cake.WebDeploy"
#addin "Cake.Powershell"
#addin "Cake.Incubator" // "Cake.Incubator&version=x.x.x"
#tool "nuget:?package=NUnit.ConsoleRunner"
#addin "nuget:?package=Cake.Powershell&version=0.4.4"

Task("Main")
  .Description("Clean local files")
  .Does(() => {
    var session1 = StartPowershellScript("New-PSSession", args =>
    {
      args.Append("ComputerName", "server-portal");
    });
    Information(session1);

    StartPowershellScript("Copy-Item", args =>
    {
      args
        .AppendQuoted(zipName)
        .Append("Destination", @"C:\WebSites\tml2\")
        .Append("ToSession", session1);
    });
  })
  .OnError(exception => {
    Information("-[Main]----------> " + exception);
  });

RunTarget("Main");

But it fails with next error:

Executing: New-PSSession -ComputerName esd-opportal
[PSSession]WinRM1
System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]
Executing: Copy-Item "publish.zip" -Destination C:\WebSites\OP-LessCompilerService\tml2\ ToSession
An error occurred when executing task 'Main'.
-[Main]----------> System.AggregateException: Failed to Execute Powershell Script: System.Management.Automation.ParameterBindingException: A positional parameter cannot be found that accepts argument 'ToSession'.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc() ---> System.Management.Automation.ParameterBindingException: A positional parameter cannot be found that accepts argument 'ToSession'.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
   --- End of inner exception stack trace ---
   at Cake.Powershell.PowershellRunner.Invoke(String script, PowershellSettings settings)
   at Submission#0.StartPowershellScript(String script, Action`1 arguments)
   at Submission#0.<<Initialize>>b__0_0()
   at Cake.Core.CakeTaskBuilderExtensions.<>c__DisplayClass20_0.<Does>b__0(ICakeContext x)
   at Cake.Core.CakeTask.<Execute>d__43.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Cake.Core.DefaultExecutionStrategy.<ExecuteAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Cake.Core.CakeEngine.<ExecuteTaskAsync>d__31.MoveNext()
---> (Inner Exception #0) System.Management.Automation.ParameterBindingException: A positional parameter cannot be found that accepts argument 'ToSession'.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()<---
denis-peshkov commented 5 years ago

As I understood, problem with the parameter "ToSession", and I have no ideas how to set it like an object that I got after New-PSSession

SharpeRAD commented 5 years ago

Each powershell script is executed within its own pipeline so anything created in a powershell script will be cleaned up when that script finishes executing. Also the library itself comes with built in remote support so there’s no need to use session. Your actually trying to create a remote session by connecting to the local machine, rather than connecting directly to the remote machine using the libraries built in aliases.

denis-peshkov commented 5 years ago

Could you help me, how to write a script that will:

Please!

SharpeRAD commented 5 years ago

I would use an online file storage service like S3 as an intermediary. Upload the zip to the S3 bucket then execute the remote script, which in turn downloads the zip and processes the files.

I even have a Cake addon for it 😄

denis-peshkov commented 5 years ago

Cool addon, but it isn't possible to use AWS for me, I need deploy to server in the same network (, Any ideas?