Get XML files from multiple folders and zip them into one archive in powershell - powershell

I am very new to windows / powershell scripting and I am trying atm to get a windows runner on github actions up and running for a java project. For this I need to simulate the following command I have for my linux runner:
find . -path '*/target/surefire-reports/*.xml' | zip -q test-reports.zip -#
So, check multiple target/surefire-reports/ directories of my project, get all XML files and zip them into one archive. This archive will then be uploaded in a later step as a CI artifact.
I read through multiple threads here and on other sites, but couldn't come up with something. I think I know I have to combine Get-ChildItem and Compress-Archive, but couldn't find a way to use these two together with multiple paths as of now.
My "solution" atm looks like this:
run: |
mvn clean install -nsu -B -f project/tests/pom.xml
# TODO
# $xmlReports = Get-ChildItem -Path .\*\target\surefire-reports -Directory |Get-ChildItem -File -Filter *.xml
# $xmlReports |ForEach-Object
TEST_RESULT=${PIPESTATUS[0]}
exit $TEST_RESULT
But as you can see it is unfinished and wouldn't work for sure. The runner runs on windows-latest on Github Actions, so this would be Windows Server 2022 and powershell >v5 at the time of writing this.
I am a bit stuck at the moment and hope someone here could help me out. Thanks in advance.

Related

Why do I get an error when trying to deploy a folder of script files with PSDeploy?

I am currently using PSDeploy to deploy my PowerShell script modules to a local PowerShell gallery and it is working great for that. It also works for deploying single files such as my profile.ps1.
Now I am wondering if I can use PSDeploy to deploy other non-PowerShell script files such as my AutoHotkey scripts. For the AHK scripts, the deploy task would only be copying a folder, including script files, from my dev location to a location where I run the scripts.
Unfortunately I keep getting an error while attempting to deploy a folder of script files with PSDeploy.
I created a simple test project on Github to help toubleshoot the error. Anyone can clone this project locally and run a test like I am if they want to.
My test PSDeploy script looks like this:
Deploy FolderTest {
By FileSystem {
FromSource '.\deployFolderFrom'
To '.\deployFolderTo'
}
}
This is how I am running my tests.
# I am running my test in 'G:\_temp'
cd G:\_temp
# Clone the test project to your test folder or in my case 'G:\_temp'
git clone https://github.com/jesmith01/PSDeployTest.git
# Change directory to the PSDeployTest folder.
cd .\PSDeployTest\
# Run this command from PSDeployTest folder.
Invoke-PSDeploy -Verbose
This is my results with verbose setting turned on, error at the end:
My PowerShell version is 7.2.1 as of running this test:
My OS version is Windows 11:
I even tested out this example code from RamblingCookieMonster exactly, without changing anything, and I still get an error:
# Source folders and files
mkdir C:\PSDeployFrom
mkdir C:\PSDeployFrom\MyModule
mkdir C:\PSDeployFrom\SomeScripts
mkdir C:\PSDeployFrom\Deployments
New-Item -ItemType File -Path C:\PSDeployFrom\MyModule\MyModule.psm1
New-Item -ItemType File -Path C:\PSDeployFrom\MyModule\MyModule.psd1
New-Item -ItemType File -Path C:\PSDeployFrom\SomeScripts\Script1.ps1
New-Item -ItemType File -Path C:\PSDeployFrom\SomeScripts\Script2.ps1
# Target folder, with a file that mirror should overwrite
mkdir C:\PSDeployTo
mkdir C:\PSDeployTo\MyModule
New-Item -ItemType File -Path C:\PSDeployTo\MyModule\Remnant.ps1
# Sample PSDeploy.ps1 file
Set-Content C:\PSDeployFrom\Deployments\my.psdeploy.ps1 -Value #'
Deploy ExampleDeployment {
By FileSystem Scripts {
FromSource 'SomeScripts'
To 'C:\PSDeployTo'
Tagged Dev
DependingOn ExampleDeployment-Modules
}
By FileSystem Modules {
FromSource MyModule
To C:\PSDeployTo
Tagged Prod, Module
WithOptions #{
Mirror = $true
}
}
}
'#
Set-Location -Path 'C:\PSDeployFrom'
After running the above sample code:
# Run this command from 'C:\PSDeployFrom' folder.
Invoke-PSDeploy -Verbose
This is my results with verbose setting turned on, getting the same error at the end:
I reviewed a bunch of *.psdeploy.ps1 scripts from others on GitHub, read the documentation, tested out examples from RamblingCookieMonster, tried using absolute paths, relative paths and I even took a peek at the PSDeploy source code where I am getting the error. I still cannot figure out where this error is coming from. It appears what I am doing is right.
Q: Is PSDeploy only intended for deploying PowerShell script modules or can it also be used to deploy non-PowerShell folders and files?
Q: Is it possible my PSDeploy installation is faulty or maybe it is not compatible with PowerShell 7.2.1?
Any help is appreciated. Thanks.
UPDATE: Thanks to #MathiasR.Jessen comment, he gave me the idea to try the same tests with Windows PowerShell 5.1 and they all worked! For now I can just use Windows PowerShell for deployment until I figure out what is broken with PowerShell 7.2.1.
Works with Windows PowerShell 5.1

Powershell Script for automatic execution of an .exe file, located in different directories, on the same base directory

I am trying to execute an .exe executable file (let' say it is called myfile.exe) under the argument (argument.fst) . Both files have the same name for each execution, but are located in different subfolders in the same parent directory.
My objective is to create a for-loop, in which, I will pinpoint the paths to both files (14 groups in total, so 14 loops) and then Windows Powershell will execute those. My goal is to automate my simulations, ran by the .exe files+arguments, thus saving time.
Is my thought possible to be implemented on Windows Powershell?
Thank you very much,
Ioannis Voultsos.
If you want to automate the process, you may store your command,args in csv file (i.e. commands.csv):
command;arguments
myapp.exe;c:/
myapp.exe;h:/
then load it and execute using &:
$csv=(import-csv commands.csv -delimiter ';')
$csv|foreach{ &$_.command $.arguments }
Beware of executing commands from strings, coming from untrusted sources though.
Try out this sample code on the parent folder
Get-ChildItem | Where-Object {($_.psiscontainer)} | ForEach-Object { cd $_.FullName; & ".\SampleApp.exe args0 args1"; cd.. }
it will go into each directory and execute .exe in each folder with arguments.

Jenkins job to Delete TeamCity's work directory

I am writing PowerShell scripts to delete TeamCity work directory on daily basis, the script is working well in ideal condition(when none of the folder in work directory is in use). But when any build is executing in TeamCity and during that time if I execute the script, it shows failure.
#Cleanup work directory of TeamCity's build agent
$Path = "C:\BuildAgent\work"
$exclude = #("*.old", "*directory.map")
Get-ChildItem $Path -Exclude $exclude | Remove-Item -Recurse
Can anyone suggest how can I skip this particular condition without showing any failure so that all the files are deleted except the one which is in use.
There is an undocumented API call that allows you to clean sources on a particular agent.
Loop through all agents and make a POST request to:
http://teamcity/ajax.html?resetSources=<agentID>
Assuming you are using a new version of TeamCity, you will need to be aware of CSRF protection.

Chef doesn't call the second line of the PS script

I'm using Chef Kitchen to do a POC. We have a PowerShell script which goes to a network path, sorts the build based on date [Install shield installer], copies to the VM in a folder.
This script works correctly when I run inside the VM manually using Power Shell. When I run using Chef Kitchen I don't get any error, the recipe is run but only the first line.I can see the folder called Build created in the VM.
I did an experiment and added a second line to create another folder, it worked correctly. So the issue is the logic I used to copy and paste the build doesn't seem to work with Chef.
The first line creates a new folder, the second and third sets the source and destination path. The fourth line copies the build from the network path [Sort by date to get the latest] to the VM. And the last one is to rename.
And also is there an efficient way to copy a file from a network path [Sorting by date] to a VM using Chef?
powershell_script 'CopyBuild' do
code <<-EOH
New-Item -ItemType directory -Path C:\Build
$source = gci \\BuildPath\Build | ? { $_.PSIsContainer } | sort CreationTime -Desc | select -f 1
$destination = "C:\Build"
Copy-Item "\\BuildPath\Build\$source\*" $destination
Get-ChildItem $destination -r -i "*app1*.exe" | Rename-Item -NewName {"Test.exe"}
EOH
end
I also tried this, calling the PS script:
cookbook_file 'getbuild.ps1' do
mode '0755'
end
This looks like a windows remoting problem, your powershell needs the right configuration to be allowed to access network shares.
To verify if this is the case: replace the UNC path and just copy a local directory - if this works you can dig further to setup the requirements for windows remoting, see the following links for further help:
Safely running windows automation operations that fail inside winrm or powershell remoting (see chapter No access to network resources)
Knife issue 655
enabling and using credssp
To sum these links up: you can schedule a local task in your PS which does the copy to achieve this, or setup credssp.

Powershell Copy-item command not working in script although it does work when run from a command line

I am on a Windows 7 machine trying to execute a PowerShell script to copy a template directory to another directory. The command I am executing looks like:
Copy-Item -path "$projectsFolder$SourceFolder" -destination "$Test" -recurse -verbose;
The parameters are as follows:
path: C:\Users\username\Documents\Visual Studio 2010\Projects\TemplateSolution\Source
Destination: C:\Users\username\Documents\Visual Studio 2010\Projects\test\source\main
When I run this command at a PowerShell prompt, the files are copied correctly. If I try and execute the command in the script with verbose enabled, it appears to copy the files and directories, but only the top level directory is created in the file system. I am not sure why this would happen and I would appreciate any guidance or troubleshooting steps to perform.
Make sure you put quotes around the directory names if they have spaces in them. Also, you may need the -Force parameter to create destination directories if they do not exist.