Copy-Item is not working - powershell

I have a script that is finding a few files and the copying them. This is part of a psake build script.
The command is:
Get-ChildItem -Path "$sourceFolder" -Include "*.ispac" -Recurse `
| Select-Object -ExpandProperty FullName `
| Copy-Item -Destination "$caseFolder\" -Force -Verbose
When I execute this, I get this for the message:
VERBOSE: Performing the operation "Copy File" on target
"Item: C:\Source\TestSssisOne.ispac
Destination: C:\Destination\TestSssisOne.ispac".
That sure looks like the files where copied. But they aren't. No errors. If I copy this command out to ISE and setup the variables, it copies no problem. I tried to just manually copy a single file, with explicit paths. Again, in script it does not copy, but in PS console or ISE it does.
I have no idea what could be the problem. I've used Copy-Item in psake scripts. In fact, I copied the above code to a later task and it works! In the task where it isn't working I'm calling msbuild to build a solution.
Any insight appreciated!

modify your code like this
Get-ChildItem -Path "$sourceFolder" -Include "*.ispac" -Recurse -File | foreach{Copy-Item $_.FullName -Destination (("$caseFolder\") + $_.Name) -Force -Verbose }

Related

powershell -exclude did not exclude all the files from list

I have this script:
$list= #("inetpub", "Program Files", "Program Files (x86)", "ProgramData", "Windows", "Users"); Get-Item "C:\*" -exclude $list | Remove-Item -Recurse -force
and it should delete everything that is root but nothing from the list and what is inside the folders from the list. When I run it, somehow the script deletes some of Program Files folders like notepad++. How can i fix it?
The script is deployed using GPO like this
-ExecutePolicy Bypass -command "$list= #("inetpub", "Program Files", "Program Files (x86)", "ProgramData", "Windows", "Users"); Get-Item "C:\*" -exclude $list | Remove-Item -Recurse -force"
Edit: If it's deployed as a script not as a command it is working. But i would still like to know how can i make it work as a command
Is that a startup script? This works from cmd at least:
powershell $list = 'inetpub','Program Files','Program Files (x86)','ProgramData','Windows','Users'; Get-Item C:\* -exclude $list ^| Remove-Item -Recurse -force -whatif

How to delete all temp files using powershell

Anyone knows how to delete all temp files using powershell.
Get-ChildItem $env:TEMP\TEMP | Remove-Item -confirm:$false -force -Recurse
I tired this code but it couldn't work.
Can you suggest me any better way to perform the same.
If you don't want to see any errors, you could use the -ErrorAction switch like this:
Remove-Item -Path $env:TEMP\* -Recurse -Force -ErrorAction SilentlyContinue
To empty TEMP folder and leave the folder in place, you should use this command:
Remove-Item $env:TEMP\* -Recurse
If you don't want to type so much, you can use also shorter version:
rm $env:TEMP\* -r
Just use this:
Remove-Item -Path $env:TEMP -Recurse -Force
Of course you will get access errors if any of the files you are deleting are actually being used by the system.
I'm running PS as LOCAdmin and run following command
PS C:\Windows\system32>$tempfolders = #(“C:\Windows\Temp\*”, “C:\Windows\Prefetch\*”, “C:\Documents and Settings\*\Local Settings\temp\*”, “C:\Users\*\Appdata\Local\Temp\*”)
PS C:\Windows\system32>Remove-Item $tempfolders -force -recurse
works for me :)

Compress-Archive Error: Cannot access the file because it is being used by another process

I would like to zip a path (with a service windows running inside).
When the service is stopped, it works perfectly, when the service is running, I have the exception:
The process cannot access the file because it is being used by another
process.
However, when I zip with 7-zip, I don't have any exception.
My command:
Compress-Archive [PATH] -CompressionLevel Optimal -DestinationPath("[DEST_PATH]") -Force
Do you have any idea to perform the task without this exception?
Copy-Item allows you to access files that are being used in another process.
This is the solution I ended up using in my code:
Copy-Item -Path "C:\Temp\somefolder" -Force -PassThru |
Get-ChildItem |
Compress-Archive -DestinationPath "C:\Temp\somefolder.zip"
The idea is that you pass through all the copied items through the pipeline instead of having to copy them to a specific destination first before compressing.
I like to zip up a folder's content rather than the folder itself, therefore I'm using Get-ChildItem before compressing in the last line.
Sub-folders are already included. No need to use -recurse in the first line to do this
A good method to access files being used by another process is by creating snapshots using Volume Shadow Copy Service.
To do so, one can simply use PowerShells WMI Cmdlets:
$Path = "C:/my/used/folder"
$directoryRoot = [System.IO.Directory]::GetDirectoryRoot($Path).ToString()
$shadow = (Get-WmiObject -List Win32_ShadowCopy).Create($directoryRoot, "ClientAccessible")
$shadowCopy = Get-WmiObject Win32_ShadowCopy | ? { $_.ID -eq $shadow.ShadowID }
$snapshotPath = $shadowCopy.DeviceObject + "\" + $Path.Replace($directoryRoot, "")
Now you can use the $snapshotPath as -Path for your Compress-Archive call.
This method can also be used to create backups with symlinks.
From there on you can use the linked folders to copy backed up files, or to compress them without those Access exceptions.
I created a similiar function and a small Cmdlet in this Gist: Backup.ps1
There was a similar requirement where only few extensions needs to be added to zip.
With this approach, we can copy the all files including locked ones to a temp location > Zip the files and then delete the logs
This is bit lengthy process but made my day!
$filedate = Get-Date -Format yyyyMddhhmmss
$zipfile = 'C:\Logs\logfiles'+ $filedate +'.zip'
New-Item -Path "c:\" -Name "Logs" -ItemType "directory" -ErrorAction SilentlyContinue
Robocopy "<Log Location>" "C:\CRLogs\" *.txt *.csv *.log /s
Get-ChildItem -Path "C:\Logs\" -Recurse | Compress-Archive -DestinationPath $zipfile -Force -ErrorAction Continue
Remove-Item -Path "C:\Logs\" -Exclude *.zip -Recurse -Force

Remove-Item : Cannot find path '\\footfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does not exist

I am trying to remove all files and folders in a specific network path location (the tfs build drop folder of one of my builds).
In my post-build script I tried the following lines: (with same results)
$Destination = '\\footfs32\Builds\TestBuildTest'
Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse
Remove-Item \\footfs32\Builds\TestBuildTest -Force -Recurse
Remove-Item "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
Remove-Item -LiteralPath "\\footfs32\Builds\TestBuildTest\*" -Force -Recurse
All included folders are being removed, but when it tries to remove the files in the dir, the following error pops ups:
Remove-Item : Cannot find path
'\abctfs32\Builds\TestBuildTest\foo_3.3.17009.3.zip ' because it does
not exist.'
Why is this error popping up on files only, while all folders are deleted correctly? This doesn't make any sense to me. And how do I fix it?
According to the error info.Very likely the file had been deleted in your build process.
Some events triggered during your build process, and that file had been deleted. When running the powershell script, those files couldn't be found. The Remove-item command Can not delete a file twice.
Please double check your build definition, build process and drop folder.
Please try the following command:
Remove-Item -Path "\\footfs32\Builds\TestBuildTest\*.*" -Force -Recurse

Powershell not copying files and not throwing exception

I am not understanding what is happening.
I am attempting to copy and paste dll's from one directory and another.
gci -path $FromPath -Include ("*.dll", "*.pdp") | ? {$_.Name -match "appMaskA|appMaskB|appMaskC"} | foreach{Copy-item $_.Fullname -destination $ToPath -force}
Now that command works for one function that I have it in, but not for this one...
Now, this command is moving dll's to a different server. Not certain why it isn't working.
And if it isn't working it should throw an exception. I did wrap that command in a try catch by the way? Should I be catching a specific exception?
What does your $ToPath look like? If your code is wrapped in try/catch add -ErrorAction Stop parameter to your copy statement as the default value is to continue so the catch block will never be executed.
gci -path $FromPath -Include ("*.dll", "*.pdp") | ? {$_.Name -match "appMaskA|appMaskB|appMaskC"} | foreach{Copy-item $_.Fullname -destination $ToPath -force -ErrorAction Stop}
Does this need to be Powershell or can you use XCOPY via a BASH/CLI script. Using XCOPY you can access C Drive by doing
SERVER.DOMAIN.LOCAL/c$/path/to/dll
Maybe this works for you:
gci -path $FromPath -Include *.dll,*.pdp | where {$_.Name -match "appMaskA|appMaskB|appMaskC"} | Copy-item -path $_ -destination $ToPath -force
For complicated and/or large copying jobs I would use the program robocopy. Robocopy is part of Windows. Execute this command to verify its location:
Get-Command robocopy|select path