Powershell Script "$_.FullName" not working - Sending files via "Copy-VMFile" - powershell

I'm trying to use a script I found on the internet to copy all files in a directory but I can't get it to work. Can anybody help debug? I'm guessing the script was used to transfer windows/windows but I need windows --> Linux.
https://www.powershellmagazine.com/2013/12/17/pstip-copying-folders-using-copy-vmfile-cmdlet-in-windows-server-2012-r2-hyper-v/
Get-ChildItem C:\tmp -Recurse -File | % { Copy-VMFile -Name "OpenProject8.3" -SourcePath $_.FullName -DestinationPath "/tmp/" -FileSource Host }
The issue seems to be related to the sourcepath, but im not 100%.
Copy-VMFile : Failed to initiate copying files to the guest.
Failed to copy the source file 'C:\tmp\svn-repositories-20200212010002.tar.gz' to the destination '/tmp/' in the guest.
At line:1 char:43
+ ... -File | % { Copy-VMFile -Name "OpenProject8.3" -SourcePath $_.FullNam ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-VMFile], VirtualizationException
+ FullyQualifiedErrorId : OperationFailed,Microsoft.HyperV.PowerShell.Commands.CopyVMFile

Well.... I feel stupid! The issue was that earlier testing the x-fer had succeeded and then when the file exists, Copy-VMFile won't overwrite the file (even though it uses root account...) and gives a non-descriptive error! The code above works fine as is.

Related

Can't publish script to PowerShell gallery, getting error

Publish-Script -Path "path-to-script.ps1" -NuGetApiKey 123456789
after doing that, I get this error in PowerShell 7.3:
Write-Error: Failed to generate the compressed file for script 'C:\Program Files\dotnet\dotnet.exe failed to pack: error '.
and I get this error in PowerShell 5.1:
Publish-PSArtifactUtility : Failed to generate the compressed file for script 'C:\Program Files\dotnet\dotnet.exe
failed to pack: error
'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:11338 char:17
+ ... Publish-PSArtifactUtility #PublishPSArtifactUtility_Param ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : FailedToCreateCompressedScript,Publish-PSArtifactUtility
my script has no dependency.
this problem has been going on for the past 2 weeks.
I even gave my script with API key to a friend from another country and they receive the same error too.
how can I fix this? I've published previous versions of this script before at least 6 times.
I've tried resetting my API key and running PowerShell as admin, didn't fix it.
Update:
I installed .NET 7 runtimes x64 and used this command from this answer on PowerShell 5.1:
# find the file having wrong .NET version
$path = Get-ChildItem (Get-Module PowerShellGet -ListAvailable).ModuleBase -Recurse -File |
Select-String -Pattern netcoreapp2.0 | ForEach-Object Path
# unload the module
Remove-Module PowerShellGet -Verbose -Force -EA 0
# update the file
$path | ForEach-Object {
(Get-Content -LiteralPath $_ -Raw).Replace('netcoreapp2.0', 'net7') |
Set-Content $_
}
Import-Module PowerShellGet -Force -Verbose
# now try to publish
but still getting error:
Publish-PSArtifactUtility : Failed to generate the compressed file for script 'C:\Program Files\dotnet\dotnet.exe
failed to pack: error
'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.2.5\PSModule.psm1:11338 char:17
+ ... Publish-PSArtifactUtility #PublishPSArtifactUtility_Param ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : FailedToCreateCompressedScript,Publish-PSArtifactUtility
enter image description here
I fixed the problem by installing .NET Core 2.0 SDK
https://dotnet.microsoft.com/en-us/download/dotnet/thank-you/sdk-2.1.202-windows-x64-installer

Powershell Remove-Item Cmdlet error

I'm using powershell to remove a directory with the command:
Remove-Item $pathAsString -Recurse -Force
However, it gives me the following error:
Remove-Item : Cannot remove the item at 'C:\Path' because it is in use.
At line:1 char:1
+ Remove-Item "C:\Path" -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Remove-Item], PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand
I thought that was odd, because the directory shouldn't be in use. I can go into file explorer and delete the directory manually. I'm not really sure why this behavior is occurring. I'm fairly new to powershell, so don't understand exactly how it behaves.
My scripts interaction with the program includes:
Creating the folder
Downloading an MSI to the folder
Setting a variable to represent the MSI stored in the folder. Like so:
$MSIVariable = Get-ChildItem $Path | Where {&_.Name -eq "MSIName.msi"}
I'm assuming that something to do with the folder is within a stream of some sort, but don't know how I'd fix this issue.
EDIT:
Here is the code I use involving the folder:
Creating the Folder:
if(!(Test-Path $MSILocation))
{
New-Item $MSILocation -ItemType directory
}
Downloading the MSI:
$webClient = (New-Object System.Net.WebClient)
$downloadLocation = Join-Path $MSILocation $MSIName
$webClient.DownloadFile($downloadURL, $downloadLocation)

Copy-Item PowerShell silently continues when source is a network share that does not exist

I'm seeing a strange behavior with the "Copy-Item" cmdlet. It looks like the cmdlet errors out if the source location is a local location that does not exist, but completely ignores that if the source is a network location that does not exist.
For e.g.
Copy-Item \\server\share\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop
This command doesn't show any errors even if the source share is invalid. However,
Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop
throws an error that looks like:
Copy-Item : Cannot find path 'C:\thisdoesnotexist' because it does not exist.
At line:1 char:1
+ Copy-Item c:\thisdoesnotexist\* -Destination c:\temp -ErrorAction Stop;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\thisdoesnotexist:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
What's going on here? I checked TechNet but couldn't see anything there that talked about this. My question is:
Is this expected?
Is there a way to make the cmdlet fail when source network share is invalid as well?
Thanks for any pointers!

How correct delete folder in IIS server

I wrote functional in powershell which delete virtual folder in IIS, delete physical path, unzip archive to folder, and create virtual path in IIS with old name. The problem in step 2. Script try delete files and folder.
...
get-childitem ($pathToIIs + "*") -recurse | remove-item -Force -recurse
...
Powershell throws error:
Cannot remove item C:\inetpub\test\css: The directory is not empty. + CategoryInfo : WriteError: (css:DirectoryInfo) [Remove-Item], I
OException
+ FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell
.Commands.RemoveItemCommand
+ PSComputerName : test.cloudapp.net
If understand correct, to stop and start IIS it's bad idea. So how fix this error?
You MUST stop IIS to be able to remove files locked by IIS:
...
iisreset /stop
get-childitem ($pathToIIs + "*") -recurse | remove-item -Force -recurse
iisreset /start
...
Stopping IIS will obviously and temporarily prevent your server from serving web pages.

Copy-Item with over write

In one of my script, it tries to copy an exe to C:\Windows\Temp folder. For example:
Copy-Item repo\filename.exe -Destination \\$machine_name\C$\Windows\Temp
Error Message:
Copy-Item : The network path was not found.
At D:\CMPortal\Scripts\ClientRepair\RepairCCMClient.ps1:122 char:33
+ if(Copy-Item <<<< cmsetup.exe -Destination \\$install_cmexec\C$\Windows\Temp)
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
When I run the same command manually, it works. Dont know whats wrong.
Is there any to overwrite the file if the copying file exists in the destination?
Thanks in advance.
Are you using the quotes correctly?
Copy-Item 'repo\filename.exe' -Destination "\\$machine_name\C`$\Windows\Temp" -Force
Does $install_cmexec contains a server name?
Also, make sure the user who runs the script has the appropriate permissions on the target path.