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

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.

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

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 executing .exe file without the folder path

I am fairly new to powershell and I am trying to create a script that executes a .exe file. I can execute them on my machine no problem because the folder path is hard coded. The problem is that if I shift this script to another computer, the .exe it calls might be located in a different folder structure. Example
My computer:
D:\Folder1\subfolder\RunMe.exe
Client computer might be
D:\RunMe\subfolder\RunMe.exe
I just need it to execute the RunMe.exe no matter where it is. Is there a way to do this in powershell?
# 1. Get the location of RunMe.exe
$RunMe = Get-ChildItem -Path d:\* -Include RunMe.exe -Recurse;
# 2. Invoke RunMe.exe
Start-Process -FilePath $RunMe[0].FullName -Wait -NoNewWindow;

PowerShell: Run command from script's directory

I have a PowerShell script that does some stuff using the script’s current directory. So when inside that directory, running .\script.ps1 works correctly.
Now I want to call that script from a different directory without changing the referencing directory of the script. So I want to call ..\..\dir\script.ps1 and still want that script to behave as it was called from inside its directory.
How do I do that, or how do I modify a script so it can run from any directory?
Do you mean you want the script's own path so you can reference a file next to the script? Try this:
$scriptpath = $MyInvocation.MyCommand.Path
$dir = Split-Path $scriptpath
Write-host "My directory is $dir"
You can get a lot of info from $MyInvocation and its properties.
If you want to reference a file in the current working directory, you can use Resolve-Path or Get-ChildItem:
$filepath = Resolve-Path "somefile.txt"
EDIT (based on comment from OP):
# temporarily change to the correct folder
Push-Location $dir
# do stuff, call ant, etc
# now back to previous directory
Pop-Location
There's probably other ways of achieving something similar using Invoke-Command as well.
There are answers with big number of votes, but when I read your question, I thought you wanted to know the directory where the script is, not that where the script is running. You can get the information with powershell's auto variables
$PSScriptRoot # the directory where the script exists, not the
# target directory the script is running in
$PSCommandPath # the full path of the script
For example, I have a $profile script that finds a Visual Studio solution file and starts it. I wanted to store the full path, once a solution file is started. But I wanted to save the file where the original script exists. So I used $PsScriptRoot.
If you're calling native apps, you need to worry about [Environment]::CurrentDirectory not about PowerShell's $PWD current directory. For various reasons, PowerShell does not set the process' current working directory when you Set-Location or Push-Location, so you need to make sure you do so if you're running applications (or cmdlets) that expect it to be set.
In a script, you can do this:
$CWD = [Environment]::CurrentDirectory
Push-Location $MyInvocation.MyCommand.Path
[Environment]::CurrentDirectory = $PWD
## Your script code calling a native executable
Pop-Location
# Consider whether you really want to set it back:
# What if another runspace has set it in-between calls?
[Environment]::CurrentDirectory = $CWD
There's no foolproof alternative to this. Many of us put a line in our prompt function to set [Environment]::CurrentDirectory ... but that doesn't help you when you're changing the location within a script.
Two notes about the reason why this is not set by PowerShell automatically:
PowerShell can be multi-threaded. You can have multiple Runspaces (see RunspacePool, and the PSThreadJob module) running simultaneously withinin a single process. Each runspace has it's own $PWD present working directory, but there's only one process, and only one Environment.
Even when you're single-threaded, $PWD isn't always a legal CurrentDirectory (you might CD into the registry provider for instance).
If you want to put it into your prompt (which would only run in the main runspace, single-threaded), you need to use:
[Environment]::CurrentDirectory = Get-Location -PSProvider FileSystem
This would work fine.
Push-Location $PSScriptRoot
Write-Host CurrentDirectory $CurDir
I often used the following code to import a module which sit under the same directory as the running script. It will first get the directory from which powershell is running
$currentPath=Split-Path ((Get-Variable
MyInvocation -Scope
0).Value).MyCommand.Path
import-module "$currentPath\sqlps.ps1"
I made a one-liner out of #JohnL's solution:
$MyInvocation.MyCommand.Path | Split-Path | Push-Location
Well I was looking for solution for this for a while, without any scripts just from CLI. This is how I do it xD:
Navigate to folder from which you want to run script (important thing is that you have tab completions)
..\..\dir
Now surround location with double quotes, and inside them add cd, so we could invoke another instance of powershell.
"cd ..\..\dir"
Add another command to run script separated by ;, with is a command separator in powershell
"cd ..\..\dir\; script.ps1"
Finally Run it with another instance of powershell
start powershell "cd..\..\dir\; script.ps1"
This will open new powershell window, go to ..\..\dir, run script.ps1 and close window.
Note that ";" just separates commands, like you typed them one by one, if first fails second will run and next after, and next after... If you wanna keep new powershell window open you add -noexit in passed command . Note that I first navigate to desired folder so I could use tab completions (you couldn't in double quotes).
start powershell "-noexit cd..\..\dir\; script.ps1"
Use double quotes "" so you could pass directories with spaces in names e.g.,
start powershell "-noexit cd '..\..\my dir'; script.ps1"