簡體   English   中英

PowerShell VB.NET cmdlet。 更改-DEBUG開關

[英]PowerShell VB.NET cmdlet. Change -DEBUG switch

試圖阻止WriteDebug中的(顯然是默認的)查詢模式,這是行不通的(請參見前面的文章)。 由於我尚不知道如何檢測命令行上是否設置了-DEBUG,因此我嘗試這樣做:

(<Cmdlet(VerbsDiagnostic.Test, "MyCmdlet", SupportsShouldProcess:=False)> _

Protected Overrides Sub BeginProcessing()          
  setting = SessionState.PSVariable                                    
  dbPref = setting.Get("DebugPreference").Value
  vbPref = setting.Get("VerbosePreference").Value
  WriteObject("VBpref: " & vbPref.ToString)
  WriteObject("DBpref: " & dbPref.ToString)
  setting.Set("VerbosePreference", ActionPreference.Continue)
  setting.Set("DebugPreference", ActionPreference.Continue)
  dbPref = setting.Get("DebugPreference").Value
  vbPref = setting.Get("VerbosePreference").Value
  WriteObject("VBpref: " & vbPref.ToString)
  WriteObject("DBpref: " & dbPref.ToString)
  WriteDebug("TEST")

輸出如下:

PS> Test-MyCmdlet -d
VBpref: SilentlyContinue
DBpref: SilentlyContinue
VBpref: Continue
DBpref: Continue
DEBUG: TEST

Confirm
Continue with this operation?
[Y] Yes  [A] Yes to All  [H] Halt Command  [S] Suspend  [?] Help
(default is "Y"):

這是不帶-D開關的輸出(無查詢):

PS> Test-MyCmdlet
VBpref: SilentlyContinue
DBpref: SilentlyContinue
VBpref: Continue
DBpref: Continue
DEBUG: TEST

那么,如果上述重置有效,為什么我仍處於查詢模式? 我該如何更改?

public PSVariableIntrinsics PSVariable { get; }

資料來源: MSDN

我不是開發人員,但是我想這意味着您對settings所做的任何更改都不會應用於用戶會話,而只會應用於存儲在變量中的本地copyin中。

至於檢測調試設置,您可以嘗試類似的方法,該方法來自早期的SO-answer

bool debug = MyInvocation.BoundParameters.ContainsKey("Debug") &&
             ((SwitchParameter)MyInvocation.BoundParameters["Debug"]).ToBool();

$ DebugPreference的默認值不是Inquire,而是SilentContinue,您可以在此處閱讀。 您自己的輸出證明了這一點。

這里說明了正確檢測WriteDebug為什么知道何時寫入的原因。

WriteDebug仍然提出問題的原因是因為您使用-Debug調用了cmdlet。 我不明白為什么設置$ DebugPreference會改變這一點。 我還看不到在執行cmdlet時更改-Debug參數的方法。

我懷疑那真的是您想要的,如果是,我不明白為什么。 如果您解釋您為什么想要,也許我們可以為您提供更好的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM