Trying to embed C# code in Powershell v1

.net, c#, powershell

Solution

Try this:

get-process | % { "Process ID = $($_.id) - $(get-date -Format "MMM ddd d HH:mm yyyy")" } |
out-file -path "C:/powershell/GET_TASK" -append

It's the powershell equivalent code.

Your code for using in add-type (only powershell V2 or V3, for V1 you have to create a DLL and use `[Reflection.Assembly]::LoadFile("c:\myddl.dll")` )

$t = @"
using System;
using System.Diagnostics;
using System.IO;

 public  class Program
    {
        public static void Main()
        {    
            var processList = Process.GetProcessesByName("taskmgr");    
            foreach (var process in processList)
            {    
                string myPath = "C:/"; //change data path as needed    
                FileStream logfile;
                //log program process
                logfile = new FileStream(myPath + "/logfile.txt", FileMode.Append, FileAccess.Write);    
                TextWriter oldLog = Console.Out;    
                StreamWriter writelog;
                writelog = new StreamWriter(logfile);
                    DateTime time = DateTime.Now;
                string format = "MMM ddd d HH:mm yyyy";    
                Console.SetOut(writelog);
                    // print out the process id
                Console.WriteLine("Process Id=" + process.Id + " - " + time.ToString(format));    
                Console.SetOut(oldLog);    
                writelog.Close();    
                //System.Environment.Exit(0); removed, otherwise closes the console  
          }
        }
    }
"@

Add-Type -TypeDefinition $t
[program]::main()

Problem

It is my first time to post a question. I am both new to csharp and powershell. Please bear with me if my english is not good at all. I've been trying to convert my csharp code into powershell, but i'm not successful. I tried to research documents about powershell and try each one of them, unfortunately, nothing work for me. My csharp code is working in Visual Studio 2010 but I could not able to embed and work it in Powershell. The code below simply catch PID of a taskmanager and write it to notepad with time and date stamp. Any help will be greatly appreciated. My csharp code is: ``` using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Windows.Forms; using System.IO; namespace GET_TASK { class Program { static void Main(string[] args) { var processList = Process.GetProcessesByName("taskmgr"); foreach (var process in processList) { string myPath = "C:/powershell/GET_TASK"; //change data path as needed FileStream logfile; //log program process logfile = new FileStream(myPath + "/logfile.txt", FileMode.Append, FileAccess.Write); TextWriter oldLog = Console.Out; StreamWriter writelog; writelog = new StreamWriter(logfile); DateTime time = DateTime.Now; string format = "MMM ddd d HH:mm yyyy"; Console.SetOut(writelog); // print out the process id Console.WriteLine("Process Id=" + process.Id + " - " + time.ToString(format)); Console.SetOut(oldLog); writelog.Close(); System.Environment.Exit(0); } } } } ``` In my Powershell code, I just put ``` $source = @" ``` at the begging of this code and ``` Add-Type -TypeDefinition $source [GET_TASK.Program]::Main() ``` at the end of the code but it don't work. I'm using Powershell version 1. Please help convert my C# code.

Original source

Related problems