Author Posts

April 1, 2014 at 10:14 am

I am relatively new to powershell and this might be a bit of running before walking but working through this will help crystallize a lot of concepts. There are two scenarios I am trying to address. I will try to walk through them and how I would approach it and any tips/suggestions would be appreciated.

Scenario 1 – I would like to go out to a bunch of machines and get a list of files and their attributes in the system and system32 folder. Specifically DLLs. Then I would like to create a table that list the file name in the first column and the various hosts across the top. I would like to then fill in the version numbers found.

I assume i could create a file with the list of host names. I could then do a dir (Get-Childitem) in those specific directories. I would then pipe that output somewhere. This is where I am getting lost as to how to format it the way I want or if that is possible.

Here is the second scneario

Below is a set of PS code that goes out and output the update to a log file (that I assume is local to each machine) I also understand that this will process sequentially.

What I would like to do is run these simultaneously (or at least as many as the default PS configuration allows) and pipe the information back to the client where I am running Powershell from to be output or sorted through by machine name, policy name etc. Is that possible?

Thanks,
David

icm -cm accounting-pc { gpupdate /force/wait:120) > accounting-pc.log
icm -cm adtest { gpupdate /force/wait:120) > adtest.log
icm -cm issexpo { gpupdate /force/wait:120) > issexpo.log
icm -cm issline1 { gpupdate /force/wait:120) > issline1.log
icm -cm issline2 { gpupdate /force/wait:120) > issline2.log
icm -cm issline3 { gpupdate /force/wait:120) > issline3.log
icm -cm issoffice { gpupdate /force/wait:120) > issoffice.log
icm -cm isspos1 { gpupdate /force/wait:120) > isspos1.log

April 1, 2014 at 6:51 pm

Hi David,

Scenario 1
Formatting is possible, but think of it separately from the actual data. Part of the reason for that is we sometimes want to process the data after it's collected or even saved to disk. Once you format it you don't try to sort/filter/calculate/process the data. It sounds like you want to use what is called a Hashtable. Since I'm not very familiar with them I don't want to give incorrect info.

Scenario 2
The gpupdate command you listed just tells me that the GPs have been applied, so I'm not sure what info you are looking to collect specifically so this is a bit general.

You don't need to store the info in a file on the remote computer. The command can be run on the remote computer with the results coming back to the computer you started it on. Think of it like this:

$Scriptblock = {gpupdate /force /wait:120}
$ServerList = Get-Content c:\Servers.txt
$LogFolder = "\\Server1\Files"

# Start the job on each server
Foreach ($Server in $ServerList) {
  Invoke-Command -scriptblock $ScriptBlock -computername $Server -asjob $("Info-"+$server)
  }

# Wait for the information to be collected
$Totaljobs = (Get-Job Info*).count

do {
    Sleep -Seconds 5
    $FinishedJobs = (Get-Job Info* | where {$_.state -ne "Running"}).count
    Write-Host "  $FinishedJobs completed so far..." -ForegroundColor DarkGray
    }while ($FinishedJobs -lt $Totaljobs)

Write-Host
Write-Host "All jobs are done" -ForegroundColor Green

#get-job Info* | ft Id, Name, State, HasMoreData -AutoSize
Write-Host

# Save the results
$ResultsFile = "Server_Audit-$((Get-Date -Format yyyyMMdd)).csv"
Receive-Job Info* | Select-Object -Property * -ExcludeProperty RunspaceID, PSShowComputerName | Export-Csv -NoTypeInformation -Path "$LogFolder\$ResultsFile"
Write-Host "You will find the results at $("$LogFolder\$ResultsFile")" -ForegroundColor Green
Remove-Job Info*

April 1, 2014 at 9:09 pm

Thank you for extensive info and post.

I will play around with this. It will be a good opportunity for me to hone my skill