locked
powershell script to a file RRS feed

  • Question

  • Hi,

    I have created a script to collect list of users part of remote servers admin group and I can see the output in command prompt.

    But how can I save this output to a file ?

    Any help is much appreciated.

    ForEach ($Computer in Get-Content C:\server.txt)
    {
           echo "$Computer"
           invoke-command {net localgroup administrators} -ComputerName $Computer                         



    • Edited by Mcteer Friday, August 18, 2017 8:28 AM
    Friday, August 18, 2017 6:57 AM

Answers

  • ForEach ($Computer in Get-Content C:\server.txt)
    {
           echo "$Computer"
          $admins = invoke-command {net localgroup administrators} -ComputerName $Computer      

    $results = $computer + "  " + $admins | out-file -append c:\restults.txt                   




    • Edited by simbrook2 Friday, August 18, 2017 7:33 AM
    • Marked as answer by Mcteer Friday, August 18, 2017 8:23 AM
    Friday, August 18, 2017 7:30 AM

All replies

  • invoke-command {net localgroup administrators} -ComputerName $Computer | out-file c:\restults.txt
    Friday, August 18, 2017 7:13 AM
  • Here data is overwriting and also the server name is not appearing in the data. I want server name against each data, so that I know in which all servers admin group who all are members.
    • Edited by Mcteer Friday, August 18, 2017 7:23 AM
    Friday, August 18, 2017 7:18 AM
  • ForEach ($Computer in Get-Content C:\server.txt)
    {
           echo "$Computer"
          $admins = invoke-command {net localgroup administrators} -ComputerName $Computer      

    $results = $computer + "  " + $admins | out-file -append c:\restults.txt                   




    • Edited by simbrook2 Friday, August 18, 2017 7:33 AM
    • Marked as answer by Mcteer Friday, August 18, 2017 8:23 AM
    Friday, August 18, 2017 7:30 AM
  •  ForEach ($ComputerName in Get-Content C:\server.txt)
    {
        invoke-command {net localgroup administrators} -ComputerName $ComputerName | 
            Select-Object -Property *,{Name='ComputerName';Expression={$ComputerName}} | 
                out-file c:\LocalAdministratorList.txt -Append                        
    } 


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Friday, August 18, 2017 7:35 AM
  • Room for one more:

    $sb = {
    	net localgroup administrators |
    Where{$_ -notmatch '--'} | Select-Object @{n='Name';e={$_}}, { Name='ComputerName'; Expression={ $env:ComputerName } } } invoke-command $sb -ComputerName (Get-Content servers.txt) | out-file c:\LocalAdministratorList.txt

    Change the e =  to format the output.  Change the where to filter unwanted lines.


    \_(ツ)_/



    • Edited by jrv Friday, August 18, 2017 2:54 PM
    Friday, August 18, 2017 2:49 PM
  • Or just do this:

    $sb = {
    	$env:COMPUTERNAME
    	net localgroup administrators
    }
    invoke-command $sb -ComputerName (Get-Content servers.txt) |
    	out-file c:\LocalAdministratorList.txt

    Or get the local management module from the gallery or from OneGet.

    find-module localmachine


    \_(ツ)_/


    • Edited by jrv Friday, August 18, 2017 2:57 PM
    Friday, August 18, 2017 2:55 PM