Reputation: 53
I think I have a basic PowerShell question that most of you guys know how to answer.
When I export a list from lets say my AD, this is a csv list with the following headers:
CsDNSHostName, WindowsVersion, Any (In that case any is something that may change due to User action)
So now I have my CSV which I have exported I then want to keep this csv up to date. In my environment the "ANY" header may change often and also there may come new computers to the list and Old ones disappear...
So now I need a way to keep my csv updated without having to create multiple csv files, also I don't want duplicate entries inside the csv so it.
Here is the code I think might be used, but I'm sure I'm wrong because my knowledge is to low to get this running properly, getting the Data is easy but making it Updatable, not...
#Runs once and afterwards I use the csv that was created as Base for all other operations
Get-ADComputer -Filter * | export-csv "c:\mycsv.csv"
$impc = import-csv "c:\mycsv.csv"
$impc | Select CsName,@{Name="WindowsVersion";Expression={
$data = invoke-command -computername $_.ComputerName -scriptblock {get-computerinfo}
$data.WindowsVersion
}},@{Name="ANY";Expression={
$data = invoke-command -computername $_.ComputerName -scriptblock {get-computerinfo}
$data.ANY
}} | Export-CSV "C:\mycsv.csv"
Upvotes: 0
Views: 206
Reputation:
To not multiple times run the Invoke-Command with Get-ComputerInfo
I'd try something like this:
$impc = Get-ADComputer -Filter * | ForEach {
$data = invoke-command -computername $_.ComputerName -scriptblock {get-computerinfo}
[PsCustomObject]@{
CsName = $_.CsName
WindowsVersion = $data.WindowsVersion
ANY = $data.ANY
}
}
$impc | Export-CSV "C:\mycsv.csv" -NoTypeInformation
Upvotes: 1