2013-08-19 32 views
0

我試圖讓所有這一切都在一條線上,但我嘗試的一切都不起作用。我嘗試過`b,但是這不會使你退到上面的行。我試着將條件與第一個添加內容聯繫起來,但是這也不起作用。在一條線上的條件添加內容

Add-Content $txtpath "p: $strPhone x $strXTEN | " 
if ($strMobile -ne $null) { Add-Content $txtpath "`m: strMobile | " } 
Add-Content $txtpath "$strFax" 
+2

不要寫一行文本到文件,則完成構建該行之前。 –

回答

1

您不能使用多個外接電話的內容因爲每個人都會附加一個換行符。很久以前,我在Add-Content上記錄了一個建議NoNewline參數。你可以爲它投票here

您可以通過添加內容或設置內容使用StringBuilder爲此,然後輸出它的內容:

$sb = new system.text.stringbuilder 
[void]$sb.Append("p: $strPhone x $strXTEN | ") 
if ($strMobile -ne $null) { [void]$sb.Append("`m: strMobile | ") } 
[void]$sb.Append($strFax) 
Add-Content $txtPath $sb.ToString() 
+0

隨着一些調整工作,謝謝! –

1

一種方式做我認爲你想要做的是使用-f格式運算符,就像這樣:

$text = "p: $strPhone x $strXTEN {0}" -f $(if ($strMobile) {"| m: $strMobile"}) 
Add-Content $txtpath $text 
+0

如何在同一行上的-f之後添加$ strFax? –