Ausführen eines Befehls in vielen Unterordnern in der Windows-PowerShell

Ausführen eines Befehls in vielen Unterordnern in der Windows-PowerShell

Ich befinde mich in einem Ordner mit 24 Unterordnern. Die Unterordner heißen Angle1, Angle2, ..., Angle24. In jedem dieser Unterordner möchte ich diesen Befehl ausführen:

Get-ChildItem PKA.dump -r | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt

Gibt es eine Möglichkeit, dies mit Windows PowerShell zu tun?

Beachten Sie, dass ich keinen Befehl in Unterordnern ausführen möchte.

Das hier ist nicht ganz das, was ich will:

Get-ChildItem/*/ PKA.dump -Recurse | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt

Antwort1

Sie könnten dies verwenden:

Erstellen Sie ein PowerShell-Objekt im übergeordneten Element (das heißt, es script.ps1enthält Get-ChildItem PKA.dump -r | Sort-Object $_ | ForEach-Object { Get-Content $_ | Select -Index 19 } > output.txt) und aktivieren Sie die nicht signierte Ausführung mit: Set-ExecutionPolicy Unrestrictedvon einer PowerShell-Administrator-Instanz aus.

Kopieren Sie dies und fügen Sie es in eine Batchdatei ein:

copy script.ps1 Angle1
cd Angle1
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle2
cd Angle2
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle3
cd Angle3
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle4
cd Angle4
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle5
cd Angle5
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle6
cd Angle6
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle7
cd Angle7
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle8
cd Angle8
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle9
cd Angle9
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle10
cd Angle10
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle11
cd Angle11
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle12
cd Angle12
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle13
cd Angle13
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle14
cd Angle14
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle15
cd Angle15
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle16
cd Angle16
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle17
cd Angle17
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle18
cd Angle18
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle19
cd Angle19
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle20
cd Angle20
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle21
cd Angle21
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle22
cd Angle22
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle23
cd Angle23
powershell .\script.ps1
del script.ps1
cd ..
copy script.ps1 Angle24
cd Angle24
powershell .\script.ps1
del script.ps1
cd ..
exit

Speichern Sie das Skript und führen Sie es als Administrator aus.

Zum Beispiel: script.batRechtsklick,Run as Administrator

verwandte Informationen