2017-06-22 36 views
0

如果您的谷歌爲PowerShell Imp,您當前會在列表的頂部找到VBScript-to-Windows PowerShell Conversion Guide。然而,答案是很大的技術幫助:轉換VBScript的Imp運算符

進出口經營

定義:對兩個表達式的邏輯含義。

我們對Imp運算符有什麼瞭解?有兩件事:1)它在Visual Basic .NET中不受支持,2)我們從來沒有聽說過任何人使用它。因此,我們要說的是,是否有Windows PowerShell等價物並不重要。

事實上,我在VBScript中多次使用了Imp運算符(也寫爲:A→B),如果它存在的話,可能會在PowerShell中使用它。

例1

我想複製不隱藏,除非它被設置爲存檔(它也需要被複制)的任何文件。
的命令爲這將是這樣的:

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

實施例2

我有一個小命令Log-EntryLog-Debug別名。類似於本地Write-Host/Write-Debug函數;當使用Log-Debug別名時,不提供信息但只在提供公共-Debug時記錄。 的命令爲這個本來是這樣的:

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

如何建立用最少的代碼邏輯蘊涵算?

回答

0

如果你把定義安靜字面上看,你可能會來像一個邏輯蘊涵聲明:

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

,因爲你需要執行相同功能的兩次可能得到安靜的過度。 所以,最好是有一個單一如果條件作爲替換爲VBScript的Imp運營商這讓我想出了:

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

不過有點進一步的調查使我甚至對VBScript的小鬼一個更簡單的更換運營商:


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"} Else {"False"}返回False

按位

如果你找的是按位Imp操作(這或許應該被稱爲 - bImp,如果它存在),那麼這將是:

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