Tôi mới sử dụng PowerShell và cần trợ giúp. Kịch bản đầu tiên của tôi cho công việc là tự động hóa người dùng mới và được gọi trong môi trường AD.Powershell - kết hợp các mảng
Kết xuất CSV sẽ được thực hiện một lần mỗi ngày từ hệ thống Peoplesoft của chúng tôi. Tôi sử dụng Import-CSV
và tạo 3 mảng (mới, thuật ngữ và xử lý).
Sự cố tôi gặp phải khi kết hợp 3 mảng khi tôi lặp lại tất cả người dùng và thử đưa nó trở lại tệp. Mã ngắt tại số $New += $Term
. Tôi tin rằng điều này là do thực tế rằng chỉ có 1 bản ghi của từng loại người dùng (mới, thời hạn và xử lý) trong tệp thử nghiệm của tôi (tôi biết, thêm nhiều người dùng hơn & hellip; không thể. Đây có thể là kết quả thực tế cho bất kỳ Ngày đặc biệt). Dưới đây là mã mẫu của tôi:
#Get Credentials from user
$c = Get-Credential
#Get Date for $Term array population
$e = Get-Date -format M/d/yyyy
#Set file location and variable for said file
$File = "c:\users\nmaddux\desktop\adduserstuff\test.csv"
#Import record sets for New and Term users
$New = @()
$Term = @()
$Procd = @()
$New = Import-Csv $File | Where-Object {
$_.TermDate -eq "" -and $_.LastName -ne "" -and $_.Processdate -eq ""
}
$Term = Import-Csv $File | Where-Object {
$_.TermDate -ne "" -and $_.Processdate -eq "" -and $_.TermDate -le $e
}
$Procd = Import-Csv $File | Where-Object { $_.Processdate -ne "" }
#Process both new and term users provided there are records to process for each
If ($New -ne $NULL -and $Term -ne $NULL) {
# Some code to process users
}
$new += $term
$new += $Procd
$new | Export-Csv $file -NoTypeInformation -ErrorAction SilentlyContinue
Vì vậy, mã sẽ xuất nhưng chỉ một phần kết quả.
lỗi - Yêu cầu phương thức không thành công vì [System.Management.Automation.PSObject] không chứa phương thức có tên 'op_Addition'.
'$ New + = $ Term' Và dòng đó là ở đâu ...? – aquinas
Đã thêm mã và báo cáo lỗi ... lời xin lỗi của tôi. – user2077056