user7160191
user7160191

Reputation:

How i can Debug C# code in Powershell with Powershell's IDE

How i can debug source in powershell line by line ?

i use c# code in powershell like this example :

$Assem = (
    “Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” ,
    “Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
    )

$Source = @”
using Microsoft.SharePoint.Publishing.Administration;
using System;

namespace StefanG.Tools
{
    public static class CDRemoteTimeout 
    {
        public static void Get()
        {
            ContentDeploymentConfiguration cdconfig = ContentDeploymentConfiguration.GetInstance();
            Console.WriteLine(“Remote Timeout: “+cdconfig.RemoteTimeout);
        } 

        public static void Set(int seconds)
        {
            ContentDeploymentConfiguration cdconfig = ContentDeploymentConfiguration.GetInstance();
            cdconfig.RemoteTimeout = seconds;
            cdconfig.Update();
        }
    }
}
“@

Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp 

[StefanG.Tools.CDRemoteTimeout]::Get()

[StefanG.Tools.CDRemoteTimeout]::Set(600)

and when i start debugging powershell i cant see anything that happen in source . i use powershell_ise . and if someone can please give me good powershell IDE to debug it easy . thanks for any help.

the $source is an string because of it the debugger just debug add-type at the first line of import source .

Upvotes: 0

Views: 3096

Answers (1)

Ajay kumar Sunnapu
Ajay kumar Sunnapu

Reputation: 31

  1. Set a Breakpoint in the method you would like to debug.
  2. Build the project.
  3. Start a new PowerShell window.
  4. In Visual Studio, select “Attach to Process” from the Debug menu.
  5. Select the PowerShell.exe process and click Attach. The debugger has now started as is attached to the PowerShell window you have opened.
  6. In the PowerShell window, import your module and run your PowerShell Cmdlet.
  7. When your code hits the breakpoint it will let you step through it in the Visual Studio debugger like any other Visual Studio solution.

Refer http://www.johnchapman.net/technology/coding/powershell-debug-custom-csharp-powershell-cmdlet/

                   (OR)

You can simply install powershell editor for debugging in powershell window http://dalexandrov.github.io/PowershellEditor/

Upvotes: 3

Related Questions