locked
Store the results within the job to a textfile(paasing array arguments) RRS feed

  • Question

  • I want the values of $comp to be stored be stored in a variable or a textfile. Please let me know how this can be done. 

    foreach($dc in $test)
        {
          #parallel execution
          Start-Job -ArgumentList (,$dc) -ScriptBlock {param($dc); 
          #sequential execution within each parallel block
                      foreach($comp in ($dc))
                      {
                        write-host " Rebooting $comp... " -ForegroundColor Yellow
                        Restart-Computer -ComputerName $comp -Wait -For WinRM -Timeout 900 -Force 
                     if(!$?)
                       {
                           #Unable to reboot a server scenerio 
                           if(Test-Connection $comp)
                           {
                            $notrebooted += $comp
                            Write-output "unable to reboot $comp" >> result.txt

                           }

    }}}

                           
    • Edited by Annu12 Monday, January 9, 2017 1:38 PM
    Monday, January 9, 2017 11:22 AM

Answers

  • hi Martijn, foreach -parallel ($computer in $computername), as per my requirement $computer should be an array and $computername should be an array of arrays($computer). can you please let me know how do i accomplish this in my current script. Thanks in advance.

    You need to learn how PowerShell works.  You current script is wrong and does not do what you say it does.  The posted workflow does what you ask.  If you do not know how to run a script or understand how it works you will need to spend some time learning this.

    The output is being sent to the file as you needed. You cannot easily write to file from multiple threads or processes. Look at your file.

    Here is a version that outputs more information.

    workflow Reboot-Computers {
    	param (
    		[string[]]$computername
    	)	
    	foreach -parallel ($computer in $computername) {
    		if (Test-Connection $computer -count 1) {
    			Restart-Computer -PSComputerName $computer -Wait -For WinRM -Timeout 900 -Force
    			"succeeded to connect to $computer "
    		} else {
    			"failed to connect to $computer "
    		}
    	}
    }
    
    Reboot-Computers -ComputerName Comp1,Comp2,Comp3
    # output to file:
    Reboot-Computers  -ComputerName Comp1,Comp2,Comp3 | Out-File results.txt


    \_(ツ)_/



    • Edited by jrv Monday, January 9, 2017 1:51 PM
    • Marked as answer by Annu12 Tuesday, January 10, 2017 12:31 PM
    Monday, January 9, 2017 1:49 PM

All replies

  • The code does exactly what you asked.

    Perhaps you could ask the author to explain to you how the code works.  You could also do one of the ma y PowerShell tutorials to help you learn how it works.

    The code does not have an parallel blocks.  It is not a workflow. It just runs once for each items in "$test". 


    \_(ツ)_/

    Monday, January 9, 2017 12:02 PM
  • You could use workflow to accomplished parallel and logging.

    The problem with working remote is that you will log on the remote system. You outfile runs on that machine. What you need is a control thread to control the remote sessions and catch the logs.

    workflow reboot-thispc { param( [string[]]$computername ) foreach -parallel ($computer in $computername) { if (Test-Connection $computer -count 1) { Restart-Computer -PSComputerName $computer -Wait -For WinRM -Timeout 900 -Force }else { Write-Output " failed to connect to $computer " } } } $noconnection = reboot-thispc -ComputerName "13th-dc02","test" $noconnection | Add-Content -Path "d:\temp\error.txt"



    MCTS exchange 2013 | MCTS-MCITP exchange 2010 | MCTS-MCITP Exchange: 2007 | MCSA Messaging: 2003 | MCP windows 2000


    Monday, January 9, 2017 12:02 PM
  • I have written the code myself and let me explain you what the code does exactly.

    $test is an array of arrays($dc). This code starts jobs on all the $dc's parallelly and within each dc, servers will be  rebooted and if the server reboot fails, that server has to be stored on to some variable or a text file. But capturing the server names is not happening(rest of the code is working except the highlighted(bold) portion of it

      if(Test-Connection $comp)
                           {
                            $notrebooted += $comp
                            Write-output "unable to reboot $comp" >> result.txt

                           }

    and am not sure where the issue is exactly. Is there a way that these outputs can be captured as i am expecting. please suggest

    Monday, January 9, 2017 1:29 PM
  • hi Martijn, foreach -parallel ($computer in $computername), as per my requirement $computer should be an array and $computername should be an array of arrays($computer). can you please let me know how do i accomplish this in my current script. Thanks in advance.
    Monday, January 9, 2017 1:33 PM
  • Hi Annu,

    jobs are in parallel automatically. They start and run and at some point later you should collect them. No action needed thus.

    Here's how you can write to file:

    $comp | Out-File attempted.txt -Append
    if (Test-Connection $comp)
    {
    	"unable to reboot $comp" | Out-File failed.txt -Append
    }
    else
    {
    	$comp | Out-File success.txt -Append
    }

    But files aren't really all that great. How about using objects instead?

    New-Object PSOBject -Property @{
    	ComputerName = $comp
    	Success = (-not (Test-Connection $comp -Quiet))
    }

    Then when the jobs are done (check with Get-Job) you can collect the objects with Receive-Job, before removing the Jobs with Remove-Job.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Monday, January 9, 2017 1:43 PM
  • hi Martijn, foreach -parallel ($computer in $computername), as per my requirement $computer should be an array and $computername should be an array of arrays($computer). can you please let me know how do i accomplish this in my current script. Thanks in advance.

    You need to learn how PowerShell works.  You current script is wrong and does not do what you say it does.  The posted workflow does what you ask.  If you do not know how to run a script or understand how it works you will need to spend some time learning this.

    The output is being sent to the file as you needed. You cannot easily write to file from multiple threads or processes. Look at your file.

    Here is a version that outputs more information.

    workflow Reboot-Computers {
    	param (
    		[string[]]$computername
    	)	
    	foreach -parallel ($computer in $computername) {
    		if (Test-Connection $computer -count 1) {
    			Restart-Computer -PSComputerName $computer -Wait -For WinRM -Timeout 900 -Force
    			"succeeded to connect to $computer "
    		} else {
    			"failed to connect to $computer "
    		}
    	}
    }
    
    Reboot-Computers -ComputerName Comp1,Comp2,Comp3
    # output to file:
    Reboot-Computers  -ComputerName Comp1,Comp2,Comp3 | Out-File results.txt


    \_(ツ)_/



    • Edited by jrv Monday, January 9, 2017 1:51 PM
    • Marked as answer by Annu12 Tuesday, January 10, 2017 12:31 PM
    Monday, January 9, 2017 1:49 PM
  • I have written the code myself and let me explain you what the code does exactly.

    $test is an array of arrays($dc). This code starts jobs on all the $dc's parallelly and within each dc, servers will be  rebooted and if the server reboot fails, that server has to be stored on to some variable or a text file.

    What  does that mean?  The workflow restarts all computers in the array passed in.

    \_(ツ)_/

    Monday, January 9, 2017 1:53 PM
  • Excellent!! Thank you so much.. I tried using workflow and it worked..:) 
    Tuesday, January 10, 2017 12:32 PM
  • Excellent!! Thank you so much.. I tried using workflow and it worked..:) 

    Workflow in PowerShell is a very powerful mechanism for running multiple tasks in parallel.  This is one of this simplest examples. It can be used to do a tremendous amount of work in a very short time. It is way more efficient then jobs and much easier to manage then runspaces.

    \_(ツ)_/

    Tuesday, January 10, 2017 12:35 PM
  • Thank you for looking and providing your suggestion.. :)
    Tuesday, January 10, 2017 1:49 PM