Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am new to Powershell, and using PS v2. I can see the massive potential it has, but I just can't get the following code to work fully. I am trying to end up with a csv file that contains the wild carded required dlls in the GAC_MSIL or sub-directory, get the dll version, internal filename and path, and the server IP address.

The code is below, and it is in single line format because it appears easier to remote onto one of the servers in the server farm and run the single line from that console, ue to security log-ins etc. The code has produced a set of results, but only for the last server, it probably does the first server, then overwrites it but I am not sure about that.

I have done a lot of reading about using arrays, and custom objects, and had a go at doing that, but my scripting skills in PS are not yet up to it.

$out = "Ouput_dll_ver_results.csv";
foreach ($server in '11.222.33.123', '11.222.33.124') 
{
    $VersionInfo = 
    (
       Get-ChildItem \\$server\C$\windows\assembly\GAC_MSIL -recurse -Include abc*.dll,def*.dll,ghi*.dll,jkl*.dll 
           | Where-Object{$_.FullName -notmatch "\\windows\\assembly\\temp\\"}
    )
}; 
$VersionInfo | %{Get-Command $_.FullName} | select -expand File* |Export-Csv $out

Can you please advise if/how the above code can be corrected, and if not, what alternatives do I have to get the information I need?

share|improve this question
    
It may be easier to do it as a single line on the server but multiple lines will help others help you here. –  Scott Chamberlain Mar 31 '12 at 17:57
    
Assuming the edited version of your code above is accurate, you are, in fact, over-writing your results in the csv. Remove the |export-csv portion of your code and let us know whether the output to screen is correct. Then we'll look for a way to fix it. –  uSlackr Mar 31 '12 at 18:21

1 Answer 1

Well, each time you are iterating in the loop, you are OVERWRITING the $VersionInfo variable . Of course, all that you get at the end is the latest entry. Try to replace the

$VersionInfo =

with

$VersionInfo +=

and see if it helps. This should APPEND instead of overwriting.

share|improve this answer
    
Scott - I appreciate what you're saying about a one liner. Thanks for the advice. –  Graham Mar 31 '12 at 20:27
    
uSlackr - I tried your diagnostic tip - output to screen and that was the same - it gave results for the second server only. So I tried Evengard's tip of using += That has worked, I now get both server result sets, however for some reason I get server 124 then server 123, then server 124 again. That is not too much of an issue as I can chop out the second lot for server 124 in Excel. Thanks very much for your help guys - much appreciated. –  Graham Mar 31 '12 at 20:34
    
I have noticed though that the script now runs very slowly - about 8 minutes to bring back 3 lots of 60 results = 180 in total. Is the query structure I am using sub-optimal? Any suggestions for an improved method would be greatly appreciated. –  Graham Mar 31 '12 at 20:35
    
Guys - apologies - it just dawned on me that I may not have deleted the first output file, causing the repeat on server 124. So I have just run to test again ennsuring the previous output file is deleted first, and it does produce on one set of results for each server. So once again - thanks for your help. –  Graham Apr 1 '12 at 7:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.