Run npm command in exact folder with powershell - 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 }}

Related

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 Get Latest from Nuget Feed

I am needing to create a powershell script that will pull down the latest packages from our nuget feed. This script will run during a TeamCity build to help with a caching issue that we are having with the nuget packages. I have not worked with powershell before, so any help would be greatly appreciated. This is what I have so far:
$packagesConfigFiles = Get-ChildItem $path -Recurse | Where-Object {$_.Name -eq "packages.config"}
foreach($packagesConfig in $packagesConfigFiles){
.\nuget.exe i $packagesConfig.FullName -o Source\Packages
}
Just in case someone needs it I wanted to post what I used for my solution.
Set-ExecutionPolicy Unrestricted
$packagesConfigFiles = Get-ChildItem $path -Recurse | Where-Object {$_.Name -eq "packages.config"}
if (Test-Path packages){
Write-Host "Delete packages folder."
Remove-Item packages -recurse
}
foreach($packagesConfig in $packagesConfigFiles){
.nuget\NuGet.exe i $packagesConfig.FullName -o packages
}
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null

Run command in each sub directory using 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 ..; }