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

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

Related

Get XML files from multiple folders and zip them into one archive in 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.

Powershell - Unable to create directory in Program Files and specify install path

I am trying to write a simple script but I am running into trouble when I try to create a directory in "C:\Program Files".
New-Item -Path "C:\Program Files\JHA" -ItemType Directory
When I do this, no directory is created. No error is thrown. I have admin privileges on my computer. Any advice?
Also, in this script I would like to run an installer and specify this path I created above as the directory it installs to. Is that possible? I read one post where the person was using IMAGEDIR= but someone said Powershell doesn't allow you to specify the installation folder.
Thanks for any help.

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.

How to properly set path in Powershell and 7zip?

I have a Powershell script to create a self-extracting archive via 7zip. But it's receiving this error:
cannot find specified SFX module
The Powershell code is:
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe
sz a -t7z -sfx -ppassword $fullpath $filetostore
Both variables are valid. I've tried -sfx and -sfx7z.sfx, same error. The 7z.sfx file is indeed in the correct folder with 7zip. I can also verify the alias is working, as the 7zip copyright appears when running the code (so 7zip commandline is being initiated). This command works outside Powershell.
I'm also tried Set-Location into the 7zip folder, but same error. What am I missing?
It seems you should add the 7-zip folder to your PATH environment variable to make things easier :
#find the 7-zip folder path
$7zPath = (Get-ChildItem "C:\Program Files","C:\Program Files (x86)" -Include "7-zip" -Recurse -ErrorAction SilentlyContinue).FullName
#add it to PATH environment variable
$env:Path += ";$7zPath;"
Then you can run 7z -sfx with no errors about the SFX module.
While Sodawillow has an answer that will work for the active session, a more permanent answer would be to add 7zip to the path for the Environment you are working in:
[Environment]::SetEnvironmentVariable("Path",$env:Path+";C:\Program Files\7-zip", [EnvironmentVariableTarget]::User)
The above one-liner should add 7zip to the active user account's path. Change 'User' to 'Machine' for the whole computer, or 'Process' for the currently running window. If you set 'User' or 'Machine', you will need to open a new powershell instance to see the change reflected.

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.