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}}
}
Related
I have this Windows command line in a batch file:
for %%f in (*fla) do fla2comp.exe -d "%%f"
How does the command line look in PowerShell syntax?
fla2comp.exe is in the directory of the batch file.
To resolve all files in the current folder matching the wildcard name *fla:
Get-ChildItem -File -Filter *fla
To loop through each file, pipe the output to the ForEach-Object cmdlet:
Get-ChildItem -File -Filter *fla |ForEach-Object { <# $_ will contain a reference to each file here #>}
Then pass the FullName property (it'll contain the rooted file path) of each file object to fla2comp.exe:
Get-ChildItem -File -Filter *fla |ForEach-Object {
fla2comp.exe -d $_.FullName
}
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 }}
I would like to scan windows folder to find files that are created in a specific data range and export it to a csv file. Although I have opened a powershell as administrator, I still see some "access denied" messages.
PS C:\Windows> Get-ChildItem . -recurse | Where-Object { $_.CreationTime -ge "07/01/2021" -and $_.CreationTime -le "07/31/2021" } | Export-Csv 'e:\scans.csv'
Get-ChildItem : Access to the path 'C:\Windows\CSC' is denied.
...
How can I fix that?
A known trick is to install the sysInternals suite to use psexec.exe and to run your script in a powershell as System:
psexec.exe -i -s powershell.exe
Then at least C:\windows\CSC is available.
I want to find all the files having .zip extension in a folder (MyFiles) recursively and run the following command for each file in Windows PowerShell.
PS C:\solr-7.3.0> java -Dc=myCore1 -Dauto=yes -Ddata=files
-Drecursive=yes -jar example/exampledocs/post.jar "File fullpath goes here"
Could you help me to achieve this?
Use the Get-ChildItem cmdlet to find the relevant Zip files and then pipe the results to the ForEach-Object cmdlet to loop over the files. The $_ or $psitem variable is the current object passed through the pipeline. Then the FullName property on that object will contain the full path to each Zip file.
Get-ChildItem -Path C:\Example\Path -Filter '*.zip' -Recurse |
ForEach-Object {
& java -Dc=myCore1 -Dauto=yes -Ddata=files -Drecursive=yes -jar example/exampledocs/post.jar $_.Fullname
}
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 ..; }