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!