0

I have an array containing folders I need to run a command on application for each of the folder, so I am using foreach command. However within the argument list, I need to specify path to each folder separately, so let's say I have 4 folders (Folder1, Folder2, Folder3, Folder4) and within foreach script block I need to Start-Process with -Argumentlist containing several application commands and a name of a folder. Same action for each folder respectively.

The problem I am having is running ArgumentList for each folder with different folder from the same array. I've been trying to be clear but let me know if this is still not understandable enough.

What I've been trying to figure out, these are pretty much my notes:

$folders=Get-ChildItem C:\AppFolder
$argum="-appcommand /path/$folders"

foreach ($folder in $folders) {
    Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList $argum
}

I can't write separate line for each folder because these will vary on each computer and I need a completely automated solution.

I know it's confusing so I'll try to rewrite this step by step what I want to achieve. I need to automate this:

For every folder stored in $folders=Folder1,Folder2,Folder3,Folder4

For Folder1: Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList '-appcommand /path/Folder1' (Note: -appcommand /path/ is a constant)

For Folder2: Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList '-appcommand /path/Folder2'

For Folder3: Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList '-appcommand /path/Folder3'

For Folder 4: Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList '-appcommand /path/Folder4'

EDIT: I have managed to work this around using new combined variables and then run them using Invoke-Expression. Below is what I did in a nutshell.

$items=Get-ChildItem C:\AppFolder -Directory | select -ExpandProperty Name
$argum="Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList '-appcommand ""/Path/"

foreach ($item in $items) {

    $Tasks=$argum+$item+"""'"
    Invoke-Expression $Tasks
}

I know it's a duct tape solution but in the end it works. Thank you good people for your input!

2 Answers 2

0

I'm not sure if I got your problem right.

But try like that:

$folders=Get-ChildItem C:\AppFolder    
foreach ($folder in $folders) {
    Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList "-appcommand $($Folder.FullName)"
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but that didn't work. I think I already tried that too. Let me be more specific. I need to run a command for each folder like this: Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentLis "-appcommand /path/Folder1" then Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentLis "-appcommand /path/Folder2" and so on. Hope that clears the situation.
@shepmarine I'm confused.. What then do you mean with /path/Folder1, /path/Folder2 etc? P.S. use $folders = Get-ChildItem C:\AppFolder -Directory if you want to return only folders.
@Theo I've edited the post, hope it's more understandable now.
Then Join-Path is your friend
try "-appcommand /path/$($Folder.BaseName)" instead of "-appcommand $($Folder.FullName)"
0

EDIT: I have managed to work this around using new combined variables and then run them using Invoke-Expression. Below is what I did in a nutshell.

$items=Get-ChildItem C:\AppFolder -Directory | select -ExpandProperty Name
$argum="Start-Process -FilePath 'C:\Program Files\App1\run.exe' -ArgumentList '-appcommand ""/Path/"

foreach ($item in $items) {

    $Tasks=$argum+$item+"""'"
    Invoke-Expression $Tasks
}

I know it's a duct tape solution but in the end it works. Thank you good people for your input!

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.