Run command in each sub directory using PowerShell - powershell

I want to run mvn clean compile in each directory in my Eclipse workspace containing a pom.xml file.
I was able to run svn update for each directory containing .svn like this:
PS C:\workspace> Get-ChildItem -recurse -force |
where {$_.name -eq ".svn"} |
foreach {svn update $_.parent.FullName}
However, this gives the full path to svn and runs from the current directory. The mvn clean compile needs to be run inside the directory, afaik. Tried doing the following, but then I just get an error saying something can't be null. Seems to be the first cd call it complains about. Do I just have some syntax wrong, or am I using foreach wrong?
PS C:\workspace> Get-ChildItem -recurse |
where {$_.name -eq "pom.xml"} |
foreach { cd $_.parent.name; mvn clean compile; cd ..; }

Seems like $_.parent.FullName property worked in the first case because I was filtering out only directories. In this case I filtered out files, so I got it working by using $_.DirectoryName instead.
PS C:\workspace> Get-ChildItem -recurse |
where {$_.name -eq "pom.xml"} |
foreach { cd $_.DirectoryName; mvn clean compile; cd ..; }

Related

Run npm command in exact folder with powershell

i want to run npm command in each folder
Get-ChildItem -Directory -Filter "desiredFolder" | ForEach-Object {&npm install}
Problem i am experiencing is that npm command runs from root folder where is script not from each folder from foreach. How can I fix it?
Use Push-Location/Pop-Location to drop in and out of the directory while installing:
Get-ChildItem -Directory -Filter "desiredFolder" | ForEach-Object { $_ |Push-Location; try{ &npm install }finally{ Pop-Location }}

Is there a correct form of using find?

I am trying to delete everything except init.py, using this command in powershell(windows 10 and python django version- 2.2.7):
find . -path "/blog/migrations/.py" -not -name "init.py" -delete
Tree of this project(attaching image)
**If i run the command i am getting this error:
FIND: Parameter format not correct
What to do? Thank you in advance. I am new to python django.**
The following script will delete all files that aren't named "init.py". Make sure to run this at the root of the folder you would like to delete items from.
Get-Childitem -File -Recurse | Where-Object{$_.Name -ne "init.py"} | Remove-Item -WhatIf
To test, leave the -WhatIf switch. If it displays the files you would like to remove, run again without the -WhatIf switch.

MSIExec via Powershell Install

Find the list of MSI Files from a directory and install on given PC remotely or locally . I want to be able to to run a script that will install 8 separate MSI files in a given directory 1 by 1. I found this script and think it work but i feel as if it is missing something right?
foreach($_msiFiles in
($_msiFiles = Get-ChildItem $_Source -Recurse | Where{$_.Extension -eq ".msi"} |
Where-Object {!($_.psiscontainter)} | Select-Object -ExpandProperty FullName))
{
msiexec /i $_msiFiles /passive
}
It would help you to understand what is going on here. I would write it something like this:
Declare source Directory:
$source = “\\path\to\source\folder”
Put each child .msi object into an array:
$msiFiles = Get-Childitem $source -File -recurse | Where-Object {$_.Extension -eq “.msi”}
Iterate the array to run each .msi:
Foreach ($msi in $msiFiles) {
Msiexec /I “$($msi.FullName)” /passive
}
This is of course just an explanation of what you are doing. It does not include any error handling, checking for return codes, or remote command syntax, etc. etc.

Getting the manifest version of a jar file using unzip in powershell

I am trying to get the implementation version of a jar file using the unzip command in a powershell script. I got few results but not all of them. If i run the same command from the powershell command it works but not from the script.
This is the code that I use
#{n='v';Expression={unzip -q -c $_.fullname META-INF/MANIFEST.MF |findstr Implementation-Version}}
For example a file called rt.jar , I am able to run it directly on the command prompt but it comes empty in the script using the above in the as part of the Get-ChildItem and Select-Object
From the command prompt:
PS C:\PC\Libaudit> unzip -q -c "C:\Program Files\Java\jre1.8.0_201\lib\rt.jar" META-INF/MANIFEST.MF |findstr Implementation-Version
Implementation-Version: 1.8.0_201
Section of the code
ForEach ($Loc in $LocStr){
Get-ChildItem -Path $Loc -Include $Extensions1 -Recurse -ErrorAction SilentlyContinue | Select-Object #{Name='f'; Expression={$_.name}},#{Name="p"; Expression={$_.fullname}},#{Name='h'; Expression={$System}},#{Name='a'; Expression={$values}},#{Name="s";Expression={Get-FileHash $_.fullname -Algorithm "SHA256"}},#{n='v';Expression={unzip -q -c $_.fullname META-INF/MANIFEST.MF |findstr Implementation-Version}},#{Name='Live'; Expression={$live}}
}

Powershell Script to navigate files in SVN directory

I need to navigate files from a SVN location instead of a windows location using PowerShell script, If I substitute windows path with SVN path it does not works, I have below working code for windows location:
function CheckPath($path)
{
Test-Path $path -PathType Container
}
function Navigate()
{
$directory = "C:\Test"
#Instead of above windows path I want to give SVN path as below:
#https://svn.test.local/svn/Files/
$qscomponent="SomeValue"
if (CheckPath -path $directory)
{
Write-Host "Executing scripts from $directory"
Get-ChildItem $directory -Filter LTR*.sql | Foreach-Object -Process { RunScript -comp $component -file $_.FullName }
}
else
{
Write-Host "Invalid component: the path does not exist!"
}
}
You can't execute anything that is on a remote SVN server. Any way of doing that will have to include downloading at least that file locally.
That said, to make this totally transparent, you could create script that depends on svn ls command.
One example is to change prompt function so that when you cd into some folder it will get the svn file list (ideally use proxy to set-location) but prompt is easier. Suppose you have .svn folder but no files (for this set depth to empty when doing checkout)
function prompt() {
$lastCmd = h | select -Last 1 -expand CommandLine
if ($lastCmd -match '^cd ') {
$x = svn info
if ($x -ne $null) {
svn ls | % {Write-Host $_}
}
}
"PS $pwd>"
}
This prompt function will list you all files on svn remote repository when you enter the folder. You could crete a simple powershell function that will download desired file and execute it (svn can checkout single files).
It will feel more Powershelly if you proxy cd or ls as you can then actually return array of items that you can pipe to for instance execution function, something like:
ls *.exe | execute
where ls is proxied and executed inside the svn repository local path and execute uses svn checkout for piped files, perhaps to TEMP and executes them.