本文介绍了转换 VBScript 的 Imp 运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在 google 上搜索 PowerShell Imp,您目前会找到 VBScript 到 Windows PowerShell 转换指南位于列表顶部.然而,答案是很多技术帮助:

If you google for PowerShell Imp you currently will find the VBScript-to-Windows PowerShell Conversion Guide on top of the list. Yet the answer is much of technical help:

定义:对两个表达式执行逻辑蕴涵.

Definition: Performs a logical implication on two expressions.

我们对 Imp 运算符了解多少?两件事:1) Visual Basic .NET 不支持它,2) 我们从未听说有人实际使用它.因此,我们要说是否有等效的 Windows PowerShell 并不重要.

事实上,我在 VBScript 中多次使用了 Imp 操作符(也写成:A→B),如果它在 PowerShell 中使用,我可能会使用它存在.

In fact, I used the Imp operator (also written as: A→B) a number of times in VBScript and likely would have used it in PowerShell if it existed.

我想复制任何未隐藏的文件,除非将其设置为存档(也需要复制).
一个命令应该是这样的:

I want to copy any file that is not hidden except if it is set to be archived (it also needs to be copied).
A command for this would have been something like:

If (($File.Attributes -match "Hidden") -imp ($File.Attributes -match "Archive")) {Copy $File}

示例 2

我有一个带有 Log-Debug 别名的 Log-Entry cmdlet.类似于原生的 Write-Host/Write-Debug 函数;当使用Log-Debug别名时,信息不会被泄露,只有在提供公共-Debug时才会被记录.一个命令应该是这样的:

Example 2

I have a Log-Entry cmdlet with a Log-Debug alias. Similar to the native Write-Host/Write-Debug functions; when the Log-Debug alias is used, the information not be revealed but only be recorded when the common -Debug is supplied.A command for this would have been something like:

If (($Noun -eq "Debug") -imp $MyInvocation.BoundParameters.Debug.IsPresent) {Add-content $LogFile $$Entry}

如何用最少的代码构建逻辑蕴涵运算符?

How can I build a logical implication operator with a minimum of code?

推荐答案

如果你从字面上理解这个定义,你可能会得出类似逻辑含义的陈述:

If you take the definition quiet literally, you will probably come to a statement like for a logical implication:

If ($Expression1) {If ($Expression2) {DoThis}} Else {DoThis}

这可能会变得过于安静,因为您需要执行两次相同的功能.因此,最好有一个 If 条件来代替 VBScript 的 Imp 运算符,这让我想出了:

Which might get quiet excessive as you need to execute twice the same function.So, it is preferable to have a single If condition as replacement for the VBScript's Imp operator which made me come up with:

If (($True, [Bool]$Expression2)[[Bool]$Expression1]) {...}

但是进一步的调查让我找到了一个更简单的替代 VBScript 的 Imp 操作符:

But a little further investigation led me even to a much simpler replacement for the VBScript's Imp operator:

If (!$Expression1 -or $Expression2) {...}

检查

0..3 | ForEach {
    $Expression1, $Expression2 = [Int]($_ / 2), [Int]($_ % 2)
    New-Object PSObject -Property @{
        Expression1 = [Bool]$Expression1
        Expression2 = [Bool]$Expression2
        Implication = !$Expression1 -or $Expression2
    }
} | Format-Table -AutoSize

真值表

Expression1 Expression2 Implication
----------- ----------- -----------
      False       False        True
      False        True        True
       True       False       False
       True        True        True

注意:在此解决方案中,$Null 表达式被视为 $False.这与 VBScript Imp 实现不同,但与包含 $Null 表达式的其他 PowerShell 运算符一致.例如VBScript 语句:If 1 And vbNull Then msgbox "True" Else msgbox "False",返回True 其中PowerShell语句If (1 -And $Null){"True"} 否则 {"False"},返回 False.

Note: In this solution $Null expressions are considered $False. This differs from the VBScript Imp implementation but is consistent with other PowerShell operators that contain $Null expressions. e.g. The VBScript statement: If 1 And vbNull Then msgbox "True" Else msgbox "False", returns True where the PowerShell statement If (1 -And $Null) {"True"} Else {"False"}, returns False.

如果您正在寻找按位 Imp 运算符(它可能应该被称为 -bImp,如果它存在),那么它将是:

If you looking for a bitwise Imp operator (which should probably be called -bImp, if it existed), then it would be:

$Implication = -bNot Expression1 -bOr Expression2    # e.g.: -bNot 3 -bOr 5 = -3 (-bAnd 0xF = 13)

这篇关于转换 VBScript 的 Imp 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 18:31