如何在PowerShell中禁止溢出检查? [英] How to suppress overflow-checking in PowerShell?

查看:131
本文介绍了如何在PowerShell中禁止溢出检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell似乎在算术运算和转换之后执行边界检查.例如,以下操作失败:

PowerShell seems to perform bounds-checking after arithmetic operations and conversions. For instance, the following operations fail:

[byte]$a = 255
$a++

$a = [byte]256

有什么方法可以强制执行溢出或类型转换,而无需借助模数或C#和Add-Type进行手动计算?

Is there any way to enforce overflows or the typecast without resorting to a manual calculation via modulo or C# and Add-Type?

推荐答案

在PowerShell中想要的行为是可以实现的,尽管有点麻烦.也许有更好的方法.

The behavior you want in PowerShell is achievable, though, it's a bit of a hack; and maybe there's a better way.

但是,如果您只是想要加密功能,那么值得一提,BCL中已经内置了很多功能,并且可以从PowerShell完全访问它(MD5,SHA,RSA,X509,大量其他功能)也是如此).

If you just want cryptographic functionality though, it's worth calling out, that there's a TON of that already built-in to the BCL, and it's fully accessible from PowerShell (MD5, SHA, RSA, X509, a ton of other stuff too).

但是,如果您对在PowerShell中执行未经检查的算术一无所知,这是一种黑客程序,应该可以为您提供所需的东西(基本上,我们正在嵌入C#代码,并使用unchecked关键字):

But if you're dead set on performing unchecked arithmetic in PowerShell, this is a hack that should give you what you want (basically we're embedding C# code, and using the unchecked keyword):

$members = @' 
public static UInt32 ToUInt32(int value)
{
    unchecked
    {
        return (UInt32)value;
    }
}


public static byte ToByte(int value)
{
    unchecked
    {
        return (byte)value;
    }
}
'@;

$type = Add-Type -Name "Convert" -Namespace "Helpers" `
        -MemberDefinition $members -PassThru -ErrorAction SilentlyContinue;

用法:

PS C:\Some\Folder> [Helpers.Convert]::ToUInt32(-1);
4294967295

PS C:\Some\Folder> [Helpers.Convert]::ToByte(-1);
255

PS C:\Some\Folder> [Helpers.Convert]::ToByte(256);
0

注意事项:

  • 我们正在呼叫[Helpers.Convert]::To...,而不是 [System.Convert]::To....
  • 如果需要其他方法,则需要在代码顶部的$members块中插入实际的C#方法.
  • 如果您在同一PowerShell会话中连续为同一NamespaceName组合连续运行Add-Type,它将失败;否则,将失败.然后您将拥有先前注册的类型. -我添加了-ErrorAction SilentlyContinue来忽略此特定情况. -这样,您可以将此代码转储到.ps1脚本中,并在使用它们的方法之前每次调用它,它们将始终存在. -但是,如果要修改C#代码并重新测试,则每次更改后都将要更改类型名称;有点痛苦.
  • We're calling [Helpers.Convert]::To... and not [System.Convert]::To....
  • If you need other methods, the actual C# methods will need to be inserted into the $members block at the top of the code.
  • If you run Add-Type multiple times in a row for the same Namespace and Name combination, in the same PowerShell session, it will fail; and you will be left with the previously registered type. -- I've added -ErrorAction SilentlyContinue to ignore this specific scenario. -- That way, you can dump this code into a .ps1 script, and call it every single time, before you use them methods, and they will always be present. -- But if you are modifying the C# code and retesting, you will want to change the type name after every change you make; it's kind of painful.

这篇关于如何在PowerShell中禁止溢出检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆