Unable to pass path variable while copying file - powershell

i am trying to copy one file from local machine to a server path. when i hardcode the paths it does copy the file with the same syntax however when passing it through variable it through an error
Cannot bind argument to parameter 'Path' because it is null.
+ CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand
+ PSComputerName : localhost
here is the code:
$SourcePath ="C:\Temp\Test.txt"
$DestinationPath ="\\ServerPath\Apps"
write-host $SourcePath
write-host $DestinationPath
Copy-Item $SourcePath $DestinationPath
Any thought why i am getting this.

If you really wrote $DestinationPath ="\ServerPath\Apps" then this should be correct:
$DestinationPath ="\\ServerPath\Apps"
Serverpath always start with a double back slash, and depending on escape sequences (different language like C#) you have to double the back slash, so it would be
DestinationPath ="\\\\ServerPath\\Apps"

Related

Powershell cmdlet: move file to Team Drive

I'm having a hard time moving a file from a local directory and into Team Drive.
I have a feeling I may be forced to step away from PS and find another route, which I really don't want to but here goes.
This command does not work:
Move-Item -Path 'C:\Program Files (x86)\FieldSmart View\Logs\BgSync.log' -Destination 'G:\Team Drives\LGE Prints\Logs\$env:computername.txt'
This command does work:
Move-Item -Path 'C:\Program Files (x86)\FieldSmart View\Logs\BgSync.log' -Destination C:\Users\ITAdmin\Desktop\Test\$env:computername.txt
The only difference is the destination.
When trying to move a file into Team Drive this is the error that is returned:
Move-Item : The given path's format is not supported.
At line:1 char:1
Move-Item -Path 'C:\Program Files (x86)\FieldSmart View\Logs\BgSync.l ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : NotSpecified: (:) [Move-Item], NotSupportedException
FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.MoveItemCommand
What can I do?
My guess is that you are using Single Quotes not Double Quotes when you want a variable replacement in your string. Your 2nd example has no quotes and the command line will turn that "text" into a string with replacement.
Run this:
"single"
'G:\Team Drives\LGE Prints\Logs\$env:computername.txt'
"double"
"G:\Team Drives\LGE Prints\Logs\$env:computername.txt"
This link looks good, or any other search for variable replacement.
https://kevinmarquette.github.io/2017-01-13-powershell-variable-substitution-in-strings/

powershell to copy folder(with sub-folders) from one server to another with credentials

I am using below code :
Copy-Item "\\10.X.X.Z\E:\abc\TASKS\AUTO_DAILY_TOLL_NOTICE" -Destination "\\10.X.X.Y\E\AshimTest\myscript" -Recurse
shows up error:
Copy-Item : Cannot find path
'\\10.X.X.Z\E:\CAS\TASKS\AUTO_DAILY_TOLL_NOTICE' because it does not
exist.
At line:1 char:1
+ Copy-Item "\\10.X.X.Y\E:\CAS\TASKS\AUTO_DAILY_TOLL_NOTICE" -Destination
"\\10 ...
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound:
(\\10.0.0.Z\E...ILY_TOLL_NOTICE:String) [Copy-Item],
ItemNotFoundException
+ FullyQualifiedErrorId :
PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
I am missing something here.
Both the servers have credentials, is it because of that ??
I am actually copying from 10.X.X.Z server to 10.X.X.Y server where the powershell script is running.
With network drives you usually use e$ rather than e:
If you try the following, what is the result?
Copy-Item "\\10.0.0.49\E$\abc\TASKS\AUTO_DAILY_TOLL_NOTICE" -Destination "\\10.10.0.50\E$\AshimTest\myscript" -Recurse

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)

How to pipe a directory name into Remove-Item?

I'm not very savvy with powershell, so any help is appreciated.
I have a drive that holds users profiles. I need to remove a specific file from each user's profile. It's structured something like this. (Names changed to protect the innocent.)
E:\Profiles
UserID
Documents
OtherFolders
DirectoryToDeleteFrom
FileToDelete.txt
UserID2
...
...
I could use the following command to delete all of the files I need to get to, but it's unbearably slow as it's recursing through all of the other folders under UserID.
#this one would work perfectly, but is terribly slow
Get-ChildItem -Path E:\Profiles -Include FileToDelete.txt -Recurse
I know exactly where each file resides, so I thought that I could do this.
(Get-ChildItem -Path 'E:\Profiles' -Exclude *.lnk)|ForEach-Object{Remove-item -path 'E:\Profiles\' + $_.Name + '\Path\To\File\FileToDelete.txt'}
But I get the following error message:
Remove-Item : A positional parameter cannot be found that accepts
argument '+'. At line:1 char:70
+ (Get-ChildItem -Path 'E:\Profiles' -Exclude *.lnk)|ForEach-Object{Remove-item ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-Item], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
I thought it was because I was passing Remove-Item a bad file path, but the following command generates the correct paths.
(Get-ChildItem -Path 'E:\Profiles' -Exclude *.lnk)|ForEach-Object{'E:\Profiles\' + $_.Name + '\Path\To\File\FileToDelete.txt'}
So what am I doing wrong here?
If you construct a string on the fly to be used inline, by another command, then put brackets around the path:
..{Remove-item -path ('E:\Profiles\' + $_.Name + '\Path\To\File\FileToDelete.txt')}

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.