我的 psobject 中有一個字串,它只適用于 10 個設備,但不適用于第 11 個設備。如果它是第 11 個變數,我如何構造一個字串而不包含一個變數?
這適用于我的 foreach 中的前 10 個:
$INFO_Tab = New-Object psobject -Property $([ordered]@{
DEVICE = "$($currentDevice)"
Platform = "Name"
Device_Error_Description = "ErrorCodeList files \Retail\Tools\ServiceEndToEnd\ErrorCodeList "
OCP_Display = "sdkDesc in $BaseName $($cppFile)" #$BaseName doesn't apply for $currentDevice="Scanner"
...
})
$xlsx = $INFO_Tab | Export-Excel -Path $outFilePathExcel -WorksheetName "Source" -Autosize -AutoFilter -FreezeTopRow -BoldTopRow -PassThru
我可以為 OCP_Display 線做這樣的事情嗎?我找不到一個例子。
OCP_Display = "sdkDesc in" if($currentDevice="Scanner"):"":$BaseName $($cppFile)"
這適用于 PowerShell 5.1 和 VSCode。
uj5u.com熱心網友回復:
if將陳述句包裝在子運算式中$():
"sdkDesc in $(if($currentDevice -ne 'Scanner'){"$BaseName "})$($cppFile)"
uj5u.com熱心網友回復:
看起來您甚至不需要$()在()字串之外。除此之外,它有助于了解 powershell 基礎知識。
$currentdevice = 'Unscanner'
$basename = 'Basename'
$cppfile = 'Cppfile'
"Prefix" $(if($currentDevice -eq "Scanner"){""}else{$BaseName}) $cppFile
PrefixBasenameCppfile
$currentdevice = 'scanner'
"Prefix" $(if($currentDevice -eq "Scanner"){""}else{$BaseName}) $cppFile
PrefixCppfile
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/473925.html
