1'st off,你需要使用一些其他的東西,然後-eq。 EQ將爲您提供一個 對象而不是一組對象。在這種情況下。
2'nd off,您需要告訴out-file它需要將內容添加到文件中,而不是重寫它。 在這裏,您使用屬性-append
-append 表示該cmdlet將輸出到現有文件的末尾,而不是替換文件的內容。
如果你願意,你也可以使用-NoClobber,不需要。
-NoClobber 指示該cmdlet不會覆蓋現有文件。默認情況下,如果指定路徑中存在文件,Out-File會覆蓋文件而不發出警告。如果使用Append和NoClobber,則輸出將附加到現有文件。
它看起來像out-file C:\Users\adm-arifsohm\Desktop\test-folder\computers.txt -Append
3'nd關閉,如果您鏈接到,不位於磁盤上的路徑,它無法找到路徑,它會給你這個錯誤Out-File : Could not find a part of the path 'C:\Users\adm-arifsohm\Desktop\test-folder\computers.txt'.
代碼將是什麼樣子如下:
Get-ADComputer -Filter { Name -like 'GBSU1*' } | select Name | Out-File -FilePath "C:\Users\adm-arifsohm\Desktop\test-folder\computers.txt" -Append
這將採取任何電腦開始GBSU1並將其放入文件中。所有這些都可以在名稱'* GBSU1'前面移動,所有結束該GBSU1的計算機都將放入該文件。
最後,我會寫的代碼,因爲這:
$FilePath = 'C:\Users\adm-arifsohm\Desktop\test-folder'
$Filename = 'computers.txt'
if (-not (Test-Path $FilePath)) {
New-Item -ItemType Directory -Path $FilePath -Force
} else {}
Get-ADComputer -Filter { Name -like 'mth24*2' } -Properties name | Out-File -FilePath $($FilePath + "\" + $Filename) -Append -Force
下一次你提出一個問題時,請寫下你希望得到的輸出,這會使它更容易一點你的問題。 –