Trying to script GPResult with powershell foreach loop

I have several windows 2003 boxes that I'm trying to get the output of gpresult in a text file format.  Here is what I have so far:

$Computers = Import-Csv ".\GPResultComputers.csv"

foreach ($Computer in $Computers) {
    GPResult /R /S $Computer.ComputerName > "GPResult $Computer.txt"
}

The script starts, but hangs on the first computer object in the csv file.  I've gotten this script to run if I take out all of the powershell and just run this with a single computer name in the actual code, running within a .ps1 powershell script file.

Any help in fixing the hang in the script is appreciated!




May 20th, 2015 7:42pm

This will work:

$Computers=Import-Csv \GPResultComputers.csv
foreach($Computer in $Computers){
    $name=$computer.Computername
    $report=GPResult /R /S $name
    $report | Out-File $name.txt
}


Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 8:04pm

Thanks for the reply, jrv!  Unfortunately that seems to have introduced a couple of other problems. 

$Computers=Import-Csv \GPResultComputers.csv

The above seems to think that my csv file is in the root of my C:.  That's not where my ps1 file is that contains this code.  You can imagine also that I'd have to run powershell with elevated privileges to overcome this if I wanted to start storing my script-related files at the root of C:

Also, the script outputs the below error:

Out-File : Cannot bind argument to parameter 'FilePath' because it is null.
At line:4 char:24
+     $report | Out-File $name.txt
+                        ~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Out-File], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.OutFileComm
   and

I tried modifying my original code to fit with what you originally provided, but I run into the original problem where it still hangs.

$Computers = Import-Csv "$PathToScript\GPResultComputers.csv"

foreach ($Computer in $Computers) {
    $Report = GPResult /R /S $Computer.ComputerName
    $Report | Out-File ".\GPResult for $Computer.txt"
}

May 20th, 2015 8:28pm

So put in the correct path.  I am not writing this for you I am just showing you how to avoid your errors.

You have to fix errors in the order they appear.

Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 8:31pm

This works buu I leave it up to you to lwarn enough about WIndows and PwoerSHell toi understand why.  If you change it incorrectly again then I cannot help.

$Computers=Import-Csv .\GPResultComputers.csv
foreach($Computer in $Computers){
    $name
=$computer.Computername
    $report
=GPResult /R /S $name
    $report
| Out-File $name.txt
}

Try to use that without reverting it back to your original mistakes.

May 20th, 2015 8:33pm

I'm not sure what you think I'm doing wrong.  The code that you provide has the same exact results that I provide.  There is something else that we're both missing.

Free Windows Admin Tool Kit Click here and download it now
May 20th, 2015 11:01pm

I run it it works fine.

Try it like this:

foreach($Computer in $Computers){
     $name=$computer.Computername
     $report=GPResult  /R /S $name
     $report | Out-File "$name.txt"
}
May 20th, 2015 11:30pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics