workflow in a powershell script

Sometime ago i had asked how to run multiple commands at one time and was given advice that Jobs or Workflow would be the best approach.  

Having done something similar with Jobs i thought this would be easy however the jobs are not working as i need info written out to text files and the jobs will not do it.  I thought I would attempt to use workflow but nothing is done.  It runs and noting happens.

This is merely a small test and i cant get the data.  Below is the relevant portion of my code.  It runs with no errors but produces no text files.

Thanks

$workfile1 = Get-Content $env:TEMP\worklog1.txt
$workfile2 = Get-Content $env:TEMP\worklog2.txt





function action1 {
foreach ($line1 in $workfile1) {
$mypath1 = "\\es01a5901\STU_HOME\$line1"
Get-ChildItem $mypath1 | Out-File $env:TEMP\scrap1.txt -Append
}
}



function action2 {
foreach ($line2 in $workfile2) {
$mypath2 = "\\es02a5902\STU_HOME\$line2"
Get-ChildItem $mypath2 | Out-File $env:TEMP\scrap2.txt -Append
}
}



workflow actions {

parallel {
action1
action2


}
}

July 6th, 2015 1:38pm

did you try to replace : Get-ChildItem $mypath2 | Out-File $env:TEMP\scrap2.txt -Append

with something like that : Get-ChildItem $mypath2 | Out-File c:\scrap2.txt -Append

??

i dont work with workflow, but maybe the $env is not correctly interpretated

using jobs :

$paths = "c:\windows;c:\test\file1.txt","c:\bootcamp;c:\test\file2.txt"

foreach ($path in $paths){
    start-job -scriptblock {$split = $args[0].split(";");get-childitem -path $split[0] | out-file $split[1]} -argumentlist $path
}

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 3:02pm

Thanks good call but no luck.

I don't care if i do it in workflow or jobs i just cant seem to make either way work.

I am all up and down the web and no mater what i am trying cant make this work.  

Everyone says Jobs is the way to go but i don't see it.

Thank you however

July 6th, 2015 3:09pm

Hi

Just a shot, your functions are outside of the workflow defined. I would try this...

workflow actions {

$workfile1 = Get-Content $env:TEMP\worklog1.txt
$workfile2 = Get-Content $env:TEMP\worklog2.txt


function action1 {
foreach ($line1 in $workfile1) {
$mypath1 = "\\es01a5901\STU_HOME\$line1"
Get-ChildItem $mypath1 | Out-File $env:TEMP\scrap1.txt -Append
}


}

function action2 {
foreach ($line2 in $workfile2) {
$mypath2 = "\\es02a5902\STU_HOME\$line2"
Get-ChildItem $mypath2 | Out-File $env:TEMP\scrap2.txt -Append
}


}

parallel {

action1
action2


}
}


#Run you action workflow
actions

Cheers,

Stefan

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 3:16pm

it doesnt work ? or it doesnt do exactly what u want ?
July 6th, 2015 3:24pm

something new happened so it is progress but no results and it only took seconds where as it should have took much longer.

I was able to do screen shots it looks like it comes up with a dialog box for  just a split second and says

get-content running 1.53 line 58 char 58

get-content running 1.43 line 59 char 59

but nothing more

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 3:28pm

it does not do anything.  click run and it quits.  No results

July 6th, 2015 3:30pm

click ? how do you run the script ? by double clicking it or what ?

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 3:35pm

I use PowerShell ISE.  I can try it with out.

July 6th, 2015 3:36pm

That was a good call and i ran it from within PowerShell via the command prompt with no change.    .\scriptname.ps1    

It did the same as in ISE

 dialog box for  just a split second says

get-content running 1.53 line 58 char 58

get-content running 1.43 line 59 char 59

but nothing more

 
Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 3:42pm

whats the content of the files ?

maybe leave only 1 or 2 lines for the time being

July 6th, 2015 3:45pm

the 2 files each only have a list of usernames.

I removed all but 2 in each of the files still same results as before.

I can run it fine using the old conventional way of one at a time.

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 3:49pm

$paths = "c:\windows;c:\test\file1.txt;c:\test\users1.txt","c:\bootcamp;c:\test\file2.txt;c:\test\users2.txt"

foreach ($path in $paths){
    start-job -scriptblock {
        $split = $args[0].split(";")
        get-content $split[2] | get-childitem -path "$split[0]\$($_)" | out-file $split[1]
        } -argumentlist $path
}

so, it may not be the best way to do it ^^ it's pretty ugly but try it,

to check the job state : get-job
when job is completed : check the "Hasmoredata" of the get-job cmdlet

if true, it means something happened, good or bad ^

^to view the "data" : receive-job -id jobId

post the result

July 6th, 2015 3:58pm

PS C:\Users\9201401145> receive-job -id 90
receive-job -id 92
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : localhost
 
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : localhost
Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 4:03pm

can i just do this as jobs.  Create a couple of jobs and run them?  I cant figure out how to make that work.  

I am game for either way.

July 6th, 2015 4:05pm

it's exactly what i trying to show you !

these are jobs ! created Inside a loop

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 4:06pm

$paths = "c:\windows;c:\test\file1.txt;c:\test\users1.txt","c:\bootcamp;c:\test\file2.txt;c:\test\users2.txt"

foreach ($path in $paths){
    start-job -scriptblock {
        $split = $args[0].split(";")
        "$($split[0]) ++ $($split[1]) ++ $($split[2]))"
        } -argumentlist $path
}

show me result !

did you create the test directory ?

July 6th, 2015 4:09pm

so this is the code i hope i applied it correctly.

$paths = "$env:TEMP\worklog1.txt;$env:TEMP\worklog2.txt"

foreach ($path in $paths){
    start-job -scriptblock {
        $split = $args[0].split(";")
        get-content $split[2] | get-childitem -path "$split[0]\$($_)" | out-file $split[1]
        } -argumentlist $path
}


get-job | wait-job
get-job | receive-job 

and this is the error message 

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
98     Job98           BackgroundJob   Running       True            localhost            ...                      
98     Job98           BackgroundJob   Completed     True            localhost            ...                      
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : localhost

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 4:10pm

i am trying here?

-- did you create the test directory ?

no

July 6th, 2015 4:16pm

I am trying.

Obviously i am not proficient with jobs on a level anywhere as close as you.

I guess don't understand the first line of code.

I am trying to keep this simple.

I understand 

start-job scriptblock {
foreach ($line1 in $workfile1) {
$mypath1 = "\\es01a5901\STU_HOME\$line1"
Get-ChildItem $mypath1 | Out-File c:\temp\scrap1.txt -Append
}
}



start-job scriptblock {
foreach ($line2 in $workfile2) {
$mypath2 = "\\es02a5902\STU_HOME\$line2"
Get-ChildItem $mypath2 |  Out-File c:\temp\scrap2.txt -Append
}
}

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 4:22pm

$paths = "$env:TEMP\worklog1.txt;$env:TEMP\worklog2.txt"
foreach ($path in $paths){
    start-job -scriptblock {
        Get-ChildItem "\\es01a5901\STU_HOME\$($args[0])" | Out-File $env:TEMP\scrap1.txt -Append
        } -argumentlist $path
}

July 6th, 2015 4:26pm

$paths = "$env:TEMP\worklog1.txt","$env:TEMP\worklog2.txt"
the $paths variable
Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 4:27pm

Ok this is the code

$paths = "$env:TEMP\worklog1.txt","$env:TEMP\worklog2.txt"


foreach ($path in $paths){
    start-job -scriptblock {
        $split = $args[0].split(";")
        get-content $split[2] | get-childitem -path "$split[0]\$($_)" | out-file $split[1]
        } -argumentlist $path
}


get-job | wait-job
get-job | receive-job 
get-job|  Remove-Job -Force

and this the results

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
122    Job122          BackgroundJob   Running       True            localhost            ...                      
124    Job124          BackgroundJob   Running       True            localhost            ...                      
122    Job122          BackgroundJob   Completed     True            localhost            ...                      
124    Job124          BackgroundJob   Completed     True            localhost            ...                      
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : localhost
 
Cannot bind argument to parameter 'Path' because it is null.
    + CategoryInfo          : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentCommand
    + PSComputerName        : localhost

July 6th, 2015 4:41pm

$paths = "$env:TEMP\worklog1.txt","$env:TEMP\worklog2.txt"
foreach ($path in $paths){
    start-job -scriptblock {
        Get-ChildItem "\\es01a5901\STU_HOME\$($args[0])" | Out-File $env:TEMP\scrap1.txt -Append
        } -argumentlist $path
}
try this

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 4:43pm

Thanks so very much for your patients and time.  I would have never got this without you.  

Granted your way i was unable to make work.  I just ran your above suggestion but received the errors

Cannot find path '\\es01a5901\STU_HOME\C:\Users\920140~1\AppData\Local\Temp\worklog1.txt' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (\\es01a5901\STU...mp\worklog1.txt:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost
 
Cannot find path '\\es01a5901\STU_HOME\C:\Users\920140~1\AppData\Local\Temp\worklog2.txt' because it does not exist.
    + CategoryInfo          : ObjectNotFound: (\\es01a5901\STU...mp\worklog2.txt:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
    + PSComputerName        : localhost

It is fine however because of your guidance i realized what i was doing wrong.  I needed to put the 

$workfile1 = Get-Content $env:TEMP\worklog1.txt

inside of the function. Once i did it started to work perfectly.

Again thanks so much for your kind help and i always mark every comment helpful as you are freely giving me your time so i thank you.  Below is the code i made work after looking at yours. so thanks

I know it is not as advanced as the direction i failed to take but it works.  

$job1 = start-job -scriptblock {
$workfile1 = Get-Content $env:TEMP\worklog1.txt

foreach ($line1 in $workfile1) {
$mypath1 = "\\es01a5901\STU_HOME\$line1"
Get-ChildItem $mypath1 | Out-File c:\temp\scrap1.txt -Append
}
}



$job2 = start-job -scriptblock {
$workfile2 = Get-Content $env:TEMP\worklog2.txt
foreach ($line2 in $workfile2) {
$mypath2 = "\\es01a5901\STU_HOME\$line2"
Get-ChildItem $mypath2 |  Out-File c:\temp\scrap2.txt -Append
}
}


$job1
$job2



get-job | wait-job
get-job | receive-job 


get-job|  Remove-Job -Force

July 6th, 2015 4:56pm

no problemo :)

at least mark on off reply as an answer :)

glad i could help


 and btw you should really look at my last answer ! did you try it ?
  • Edited by TonQ 9 hours 50 minutes ago
Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 5:16pm

no problemo :)

at least mark on off reply as an answer :)

glad i could help


 and btw you should really look at my last answer ! did you try it ?
  • Edited by TonQ Monday, July 06, 2015 9:19 PM
July 6th, 2015 9:11pm

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

Other recent topics Other recent topics