c# - 尝试在 Powershell v1 中嵌入 C# 代码

标签 c# .net powershell

我是第一次发帖提问。我对 csharp 和 powershell 都是新手。如果我的英语不好,请多多包涵。 我一直在尝试将我的 csharp 代码转换为 powershell,但没有成功。我试图研究有关 powershell 的文档并尝试每一个,不幸的是,对我来说没有任何用处。我的 csharp 代码在 Visual Studio 2010 中运行,但我无法在 Powershell 中嵌入和运行它。 下面的代码简单地捕获任务管理器的 PID,并将其写入带有时间和日期戳记的记事本。 任何帮助将不胜感激。

我的 csharp 代码是:

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);                               
            }
        }
    }
}

在我的 Powershell 代码中,我只是把

$source = @"

在这段代码的开头和

Add-Type -TypeDefinition $source
[GET_TASK.Program]::Main()

在代码的末尾,但它不起作用。我正在使用 Powershell 版本 1。请帮助转换我的 C# 代码。

最佳答案

试试这个:

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

这是 powershell 的等效代码。

您在添加类型中使用的代码(仅限 powershell V2 或 V3,对于 V1,您必须创建一个 DLL 并使用 [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()

关于c# - 尝试在 Powershell v1 中嵌入 C# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13355198/

相关文章:

.net - 使用 C++/CLI 的 SQL Server

azure - 如何使用 PowerShell 将 nextmarker 参数用于 Azure 列表 blob Rest api

windows - 从 Powershell 类返回自定义 PSObject 数组

Powershell 列出 Azure 存储上的所有表

c# - 代码编辑器 Visual Studio 2013 中缺少颜色

c# - FileStream.Read - 不使用循环读取流

c# - 如何将 String 转换为 Int?

c# - WPF 中的消息框功能

c# - 检查用户是域用户还是本地用户

.net - 从后台线程更新BindingList <>?