0

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"
2
  • 1
    Just add -Force to your Export-Csv command (also -NoTypeInformation). Commented Jun 5, 2018 at 21:57
  • I dont think that my "solution" works properly... I think I need to use this itpro.outsidesys.com/2017/10/21/… in combination with foreach and compare the rows (using the computername as uniqe identifier) Commented Jun 5, 2018 at 22:22

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Will this change the current Lines and add new ones(In case a new PC gets added), while waiting for some anwsers here , I found out about this itpro.outsidesys.com/2017/10/21/… Cant I use this in combination with foreach and then compare the Cloumns of every Line and say (If column "any" != $_.any(from get adcomputer) then $any = $_.any, else add new column timestamplastcheck This way I could check each row (Needing an Uniqe identifier) and replace if something is different. If it is not I get the last checked date. Thats a concept that may work?
The script will build the $impc object from scratch by iterating over Get-AdComputer output getting computerinfo once and stuff the data in properties you choose. If you export to a different name there is no problme simply trying. To sync with any prior data different steps are required.
Get-ComputerInfo | select WindowsVersion no longer works for 21h1 - it shows 2009 (same as 20h2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.