3

I have a simple workflow with a parallel loop but i run into an error when writing out the result. Because the write-output is parallel. I get the error:

The process cannot access the file because it is being used by another process.

Here is my script:

workflow Get-ServerServices 
{
    $srvlst = Get-Content C:\TEMP\srvlst.txt

    foreach -parallel ($srv in $srvlst)
    {
         Get-Service -PSComputerName $srv | Out-File c:\temp\test.txt -Append

    }

}

any idea?

4 Answers 4

5

I would suggest writing out to temporary files. You could do something like the below code:

workflow Get-ServerServices 
{
    #Get the temp path of the context user
    $TempPath = Join-Path -Path $([System.IO.Path]::GetTempPath()) -ChildPath "ServerServices"
    New-Item -Path $TempPath -ItemType Directory # and create a new sub directory
    $srvlst = Get-Content C:\TEMP\srvlst.txt

    foreach -parallel ($srv in $srvlst)
    {
        $TempFileName = [System.Guid]::NewGuid().Guid + ".txt" #GUID.txt will ensure randomness
        $FullTempFilePath = Join-Path -Path $TempPath -ChildPath $TempFileName
        Get-Service -PSComputerName $srv | Out-File -Path $FullTempFilePath -Force #Write out to the random file
    }

    $TempFiles = Get-ChildItem -Path $TempPath
    foreach ($TempFile in $TempFiles) {
        Get-Content -Path $TempFile.FullName | Out-File C:\temp.txt -Append #concatenate all the files
    }

    Remove-Item -Path $TempPath -Force -Recurse #clean up
}

Basically you are getting the temp dir, appending a new directory, adding a bunch of GUID named text files with your output, concatenating them all into one, and then removing all of them

Sign up to request clarification or add additional context in comments.

Comments

2

You can do it in a simpler form by using a buffer and outputting buffer contents at the end:

workflow Get-ServerServices 
{
    $srvlst = Get-Content C:\TEMP\srvlst.txt
    $out = @()

    foreach -parallel ($srv in $srvlst)
    {
         $WORKFLOW:out += (Get-Service -ComputerName $srv)

    }

    Out-File c:\temp\test.txt -InputObject $out

}

see technet "Variables in Parallel and Sequence Statements" for more info

Comments

0

Create the test.txt file name based on a variable - then you will have no conflicts.

At the end of the process gather up all the individual files and create one file with all the results - then remove the individual files as part of the cleanup'

Comments

0

As of Powershell 6, Workflow with Foreach-object is no longer used, also the parameter pass has been changed, follow an example below, see if it helps.

# Default directory variable
$DefaultDir = "C:\temp\sample"

# Log file.
$LogFile = "$($DefaultDir)\Log\Log_$([DateTime]::Now.ToString("yyyyMMdd-HHmmss")).txt"

# Directory where all computers are listed.
$AllComputerFolder = "$($DefaultDir)\ListToCheck\pcs.txt"

# Scan the file that lists all machines to be scanned.
Get-Content $AllComputerFolder | ForEach-Object -Parallel { 
    
    # It will only scan for machines that are connected to the network.
    $TurnedOn = Test-Connection -BufferSize 8 -Count 1 -ComputerName $($_ -replace "\\", "") -Quiet

    # Will scan only for machines that are connected
    IF($TurnedOn -eq $True){

        # Run your script, notice the new way of passing parameters within Foreach-object: $using:DefaultDir
        $Return = ."$using:DefaultDir\Script\script.ps1" $_        
        
        # Writes in file the return of the processing of each machine.
        Add-Content -Path "$using:LogFile" -Value $Return 
    }ELSE{
        # Writes the return of machines with error to file
        Add-Content -Path "$using:LogFile" -Value "$($_) : $([DateTime]::Now.ToString("yyyyMMdd-HHmmss")) : Machine turned-off or out of the network"  
    }  

# Number of simultaneous threads
} -ThrottleLimit 20

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.