Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell - Convert FileTime to HexString

Tags:

After searching the interwebs, I've managed to create a C# class to get a FileTimeUTC Hex String.

public class HexHelper
{
    public static string GetUTCFileTimeAsHexString()
    {
        string sHEX = "";

        long ftLong = DateTime.Now.ToFileTimeUtc();
        int ftHigh = (int)(ftLong >> 32);
        int ftLow = (int)ftLong;
        sHEX = ftHigh.ToString("X") + ":" + ftLow.ToString("X");

        return sHEX;
    }
}

For PowerShell I've tried using this same code:

$HH = @"
public class HexHelper
{
    public static string GetUTCFileTimeAsHexString()
    {
        string sHEX = "";

        long ftLong = DateTime.Now.ToFileTimeUtc();
        int ftHigh = (int)(ftLong >> 32);
        int ftLow = (int)ftLong;
        sHEX = ftHigh.ToString("X") + ":" + ftLow.ToString("X");

        return sHEX;
    }
}
"@;

Add-Type -TypeDefinition $HH;
$HexString = [HexHelper]::GetUTCFileTimeAsHexString();
$HexString;

The problem is I get a few error messages:

The name 'DateTime' does not exist in the current context

+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand

Add-Type : Cannot add type. Compilation errors occurred.

+ CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

I don't know how to get this C# code valid for PowerShell and would like a working solution. I don't know why PowerShell can't recognize the DateTime class in my C# snippet.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!