我有下面的代碼,基本上得到的服務器名,並將其添加到一個UNC一個CSV。我遇到的問題是,如果我多次運行它,它會不斷添加標題並添加第二臺服務器。添加到CSV
首先來看這是文件的外觀:
ServerName Server1
第二輪,這是發生了什麼:
ServerName Server1 ServerName Server2
我怎樣才能避免這種情況,使服務器只是獲取,而不是附加的頭的每次寫入?
這是我的代碼的外觀:
$servername = $env:computername
# convert string to an object, otherwise if you output to a CSV it will save the
# strings length instead of the servername
# This entry is for testing
#$servername = "test1"
$obj_list = $servername | Select-Object @{Name='ServerName';Expression={$_}}
# converts to an object and sets no header for the column
#$obj_list | ConvertTo-Csv -NoTypeInformation | % {$_.Replace('"','')} | select -Skip 1 | Add-Content \\test\d$\citrixservers\test1.csv
$obj_list | ConvertTo-Csv -NoTypeInformation | % {$_.Replace('"','')} | Add-Content \\test\d$\citrixservers\servers.csv
在第二次運行中,您可以嘗試如下所示:'$ obj_list | Select-Object -ExpandProperty ServerName |添加內容$文件-Encoding UTF8' – DarkLite1