从Powershell脚本调用.NET静态类方法

时间:2018-01-17 21:00:58

标签: c# .net powershell

尝试使用Powershell脚本中的.NET DLL。 DLL的目的是记录。我能够无错误地执行命令,但不显示任何日志。该类包含可能无提示失败的try/catch块。

以下是我的Powershell代码:

Add-Type -Path "C:\path\to\dll.dll"
[NS.MsgLog]::GetLogPath("C:\path\to\log.log")
[NS.MsgLog]::LogMessage([NS.MsgLevel]::Info, "line1")

GetPath()设置本地路径变量 LogMessage()执行日志记录工作

这是基本的.NET代码:

namespace NS
{
    public enum MsgLevel
    {
        Info,

        ...more code here

    public static class MsgLog
    {

      public static void LogMessage(MsgLevel level, string Message, params object[] Args)
      {
          try
          {
              // Open file.
              // Ensure directory path exist.
              if (!File.Exists(MsgLog.LogPath))
              {
                  Directory.CreateDirectory(Path.GetDirectoryName(MsgLog.LogPath));
              }

              ...more code follows

      public static void GetLogPath(string Path)
      {
          LogPath = Path;
      }

      ...more code follows

我可以确认DLL存在没有错误,如此

PS Z:\> add-type -Path "C:\path\to\dll.dll"
PS Z:\> [NS.MsgLog]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    MsgLog                                   System.Object


PS Z:\> [NS.MsgLevel]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     MsgLevel                                 System.Enum

LogMessage()在其他任何地方都可以正常工作,所以我不认为函数中存在任何错误 - 但是我调用函数的方式可能存在错误。

更新

此处验证功能是否存在:

PS Z:\> [NS.MsgLog] | Get-Member -Static

TypeName: NS.MsgLog

Name            MemberType Definition
----            ---------- ----------
Equals          Method     static bool Equals(System.Object objA, System.Object objB)
GetLogPath      Method     static void GetLogPath(string Path)
LogBOJ          Method     static void LogBOJ(), static void LogBOJ(string AppName)
LogEOJ          Method     static void LogEOJ(), static void LogEOJ(string AppName)
LogMessage      Method     static void LogMessage(NS.MsgLevel level, string Message, Params System.Object[] Args)
ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object objB)

1 个答案:

答案 0 :(得分:-1)

我尝试了你的代码,它对我有用。我可以使用您提供的精确Powershell命令获取日志。我认为import os import sys from django.core.wsgi import get_wsgi_application from djunk.utils import getenv os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'multitenant.settings.settings') # If the environment is configured to enable remote debugging, attach to the configured remote debug server. # If REMOTE_DEBUG_ENABLED set to True, REMOTE_DEBUG_HOST and REMOTE_DEBUG_PORT are required. if getenv('REMOTE_DEBUG_ENABLED', False): print('Debugging Enabled') # We keep this import inside the REMOTE_DEBUG_ENABLED check because simply doing the import slows down the process, # even if we don't call settrace(). sys.path.insert(0, '/debug/pycharm-debug-py3k.egg') import pydevd # Attach to a Remote Debugger session running in PyCharm or PyDev on the configured host and port. # NOTE: If no remote debug server is running, this call will crash and the exception handler will also crash. # Be aware of this! pydevd.settrace( host=getenv('REMOTE_DEBUG_HOST'), port=getenv('REMOTE_DEBUG_PORT'), suspend=False ) application = get_wsgi_application() 出了问题。您可以移除LogMessage块并再次拍摄。它可能会抛出异常。