[英]CSV output from PowerShell (test-connection) appearing blank when more than one line in source CSV
[英]powershell better way to test if both directories exists or one only on all pc's from domain and output to csv
你能幫我糾正這個腳本(從互聯網上收集)或更好地改變它應該如何工作的邏輯(目前不工作)。 目標是獲取僅存在一個文件夾(oracle11)而不是兩者(11 + 12)的PC,並將其導出到csv。 Oracle 是一個真正的痛苦.... 提前感謝您的建議。
Import-Module ActiveDirectory
$computers = Get-ADComputer -Filter * -Properties * | Select -Property Name
$output = @()
#$computers = get-adcomputer -filter * | Select-Object -Expand Name | foreach-object {
Foreach ($Computer in $computers){
if ( (test-path "\\$Computer\C$\oracle\product\11.2.0\" ) -and !( test-path "\\$Computer\C$\oracle\product\12.2.0" )) {
$output += $Computer
}
}
$output | Export-Csv -NoTypeInformation -Path c:\temp\test.csv
問題是您在循環中構造的路徑字符串與您期望的不一樣。
When you pipe the output from Get-ADComputer
ADComputer to Select-Object -Property Name
, it creates a new object with a single property Name
for each input object.
然后,當您將這些對象之一隱式轉換為字符串時,結果值將是"@{Name=Computer01}"
,而不僅僅是"Computer01"
。
您可以通過調用Write-Host
而不是Test-Path
自己觀察這一點:
Get-ADComputer -Filter * |Select-Object -Property Name |ForEach-Object {
Write-Host "\\$_\C$"
}
要僅從每個 ADComputer 中提取Name
屬性的值,請使用ForEach-Object -MemberName
而不是Select-Object -Property
:
$computerNames = Get-ADComputer -Filter * -Properties * | ForEach-Object -MemberName
$output = @()
foreach($ComputerName in $computerNames){
if ( (Test-Path "\\$ComputerName\C$\oracle\product\11.2.0\" ) -and !( Test-Path "\\$ComputerName\C$\oracle\product\12.2.0" )) {
$output += $ComputerName
}
}
$output | Export-Csv -NoTypeInformation -Path c:\temp\test.csv
請注意,不需要將-Properties *
傳遞給Get-ADComputer
ADComputer,object 名稱始終是 Get-AD* cmdlet 發回的默認屬性集的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.