Link to home
Start Free TrialLog in
Avatar of Zack
ZackFlag for Australia

asked on

Running PowerShell Through Series of Pre-Defined Directories

Hi EE,

If I had the following code

$batchfiles = (gci -path .\downloads -filter "*batch*.csv").fullname
foreach ($file in $batchfiles){
  $f = gc $file -ReadCount -1
  write-host "$($($f.tochararray().where({$_ -eq ""'""}) | measure).count) apostrophes in $file"
  out-file -FilePath $file -InputObject $f.Replace("'", '''')
}
}

Open in new window


How could I add a list of series directories for this code module to execute against? Ideally, I would like to read the directories from .txt file e.g Directories.txt with each new line being treated a new directory.

Any assistance is welcome.

Thank you.
ASKER CERTIFIED SOLUTION
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zack

ASKER

Hi Jose,

No output at all.

[Cmdletbinding()]
param(
    [Parameter(Mandatory=$false,Position=0)]$PathToTextFile="C:\Temp\Backup\Directories.txt"
)

#$file in your text
$OutputFile = "C:\Temp\Output.txt"


$script={
    [cmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,Position=0)]$Directory,
        [Parameter(Mandatory=$true,Position=1)]$OutPutFile
    )

    $batchfiles = (gci -path $Directory -filter "*batch*.csv").fullname
    foreach ($file in $batchfiles){
        $f = gc $file -ReadCount -1
        Write-Host "$($($f.tochararray().where({$_ -eq ""'""}) | measure).count) apostrophes in $file"
        Out-File -FilePath $OutPutFile -InputObject $f.Replace("'", '''')
    }
}


Get-Job | Remove-Job -Confirm:$false -Force

foreach($directory in $(gc $PathToTextFile -ea Stop)){
    Invoke-Command -ScriptBlock $script -ArgumentList $directory,$OutputFile -AsJob
}

Get-Job | Wait-Job

Get-Job | Receive-Job

Open in new window


Running as admin ./fileparser.ps1

Thank you
Well I think that at that point the $script var is not giving any output.

find out the Output file you defined in line #7
Avatar of Zack

ASKER

Hi Jose,

It doesn't exist.Output.txt that is.

Thank you.
Avatar of Zack

ASKER

Hi Jose,

Hold up mate just looking at the event logs and I am getting a security issue one sec.

Thank you.
Avatar of Zack

ASKER

Hi Jose,

Our system admin has left for the day otherwise I'd have a 'talk' he said security was disabled grrrr on my Dev box.

Do you mind waiting until Monday or shall just award points now?

Thank you for understanding
I don't mind waiting haha I'm not in a rush :) I won't go anywhere :P

You can use another way:

Instead of:
Get-Job | Remove-Job -Confirm:$false -Force

foreach($directory in $(gc $PathToTextFile -ea Stop)){
    Invoke-Command -ScriptBlock $script -ArgumentList $directory,$OutputFile -AsJob
}

Get-Job | Wait-Job

Get-Job | Receive-Job

Open in new window


You can use in sequentially instead:
foreach($directory in $(gc $PathToTextFile -ea Stop)){
    Invoke-Command -ScriptBlock $script -ArgumentList $directory,$OutputFile 
}

Open in new window