0

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 .

1 Answer 1

3
  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/

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for you answer i give you vote up . but can u give me image or something plz ? i cant understand it .
That is the procedure if you have written your c# code in visual studio and then debug using powershell..... Hope it helps!! :)
this is not work as i expect . but thank you . you teach me something new .
look i want to debug in string in powershell . my english not good . i can understand it with picture or video or something . if you can give me video or link.
informit.com/articles/article.aspx?p=2421573 Check this.... it has detailed explaination with pictures... Hope you understand well!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.