Powershell copy from linux samba to local windows server folder - powershell

I need to copy a file from a Linux samba server to various Windows Server 2008.
The shared folder has a specific login and is read-only.
I can access and copy the shared file using Windows Explorer without a problem.
But, when using PowerShell to copy the file, it always give an error as shown below.
I have tried using Copy-item, robocopy and bitstransfer but they all give an error.
$arq = "file.zip"
$downloadSource = "\\domain.or.ip\sharedfolder\$arq"
echo $downloadSource
Copy-Item -Path "$downloadSource" -Destination ".\$arqAgenteZabbix"
this method gives me the following error
Copy-Item : Access denied
CategoryInfo : PermissionDenied: (\domain.or.ip\sharedfolder\file.zip:String) [Copy-Item], UnauthorizedAc
cessException
FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
...
Copy-Item: path not found...
So, I tried adding a credential parameter
$credencial = New-PSSession -ComputerName "serverhostname" -Credential "serverhostname\sharedfolder"
Copy-Item -Path "$downloadSource" -Destination ".\$arqAgenteZabbix" -ToSession $credencial
But received this error after typing my password:
"New-PSSession : [pxl0mon00013] Fail to connect to remote server >serverhostname ...
WinRM cannot process the request... error 0x80090311 ...
CategoryInfo : OpenError (System.Manageme....RemoteRunspace:RemoteRunspace) [New-PSSession], PSRemotin
gTransportException
FullyQualifiedErrorId : AuthenticationFailed,PSSessionOpenFailed
Then, I decided to give BitsTransfer a shot.
Import-Module bitstransfer
$arq = "file.zip"
$downloadSource = "\\domain.or.ip\sharedfolder\$arq"
Start-BitsTransfer -DisplayName DownloadName `
-TransferType Download `
-Source $downloadSource `
-Destination .\$arq
And it also gave me an error:
Start-BitsTransfer : path not found
'\domain.or.ip\sharedfolder\file.zip' does not exist.
CategoryInfo : ObjectNotFound: (\domain.or.ip\sharedfolder\file.zip:String) [Start-BitsTransfer], ParentC
ontainsErrorRecordException
FullyQualifiedErrorId : PathNotFound,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand
How can I make this file copy, please?
EDIT - 20190403
I tried the following:
get-childitem \\domain.or.ip\sharedfolder\
which resulted in:
Get-ChildItem : Cannot find path '\\domain.or.ip\sharedfolder\' because it does not exist.
At line:1 char:3
+ ls <<<< \\domain.or.ip\sharedfolder\
+ CategoryInfo : ObjectNotFound: (\domain.or.ip\sharedfolder\:String) [Get-ChildItem], ItemNotFo
undException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
So, I opened Explorer and pasted \domain.or.ip\sharedfolder\ at the address bar. It asked me for username and password, then, the file was available.
After that, I returned to PowerShell and tried once again the same Get-ChildItem cmdlet. Then, I was able to list the shared folder contents as expected.
Directory: \\domain.or.ip\sharedfolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 01/04/2019 10:06 3896455 file.zip
Finally, I tried:
Copy-Item -Path \\domain.or.ip\sharedfolder\file.zip -Destination ".\file.zip"
And it was copied successfully.
Well, only after I entered my login information in Explorer that PowerShell was able to find the shared folder.
But I need it to copy without having to open explorer.

You can provide alternate credentials with Invoke-Command:
Invoke-Command -ScriptBlock {
$arq = "file.zip"
$downloadSource = "\\domain.or.ip\sharedfolder\$arq"
echo $downloadSource
Copy-Item -Path "$downloadSource" -Destination ".\$arqAgenteZabbix"
} -Credential $Cred

I finally managed to successfully copy the shared file in a relative simple way. I used "NET USE" command to open a session and copy the file, like shown below.
$arq = "file.zip"
$downloadSource = "\\domain.or.ip\sharedfolder"
net use $downloadSource /persistent:no /user:[user] [pass]
Copy-Item -Path "$downloadSource\$arq" -Destination ".\$arq"
net use $downloadSource /delete
Now, a new challenge... encrypt the clear-text password.

Related

PowerShell Copy-Item: The System detected a possible attempt to compromise security

I am trying to copy files from local computer to remote computer.
Here is my code
$localPath='D:\Apps\temp.txt'
$remoteComputerLocation='\\192.168.1.200\E$\tempfolder'
Copy-Item $localPath -Destination $remoteComputerLocation -Force
So when i try to run this i got this error
Copy-Item: The System detected a possible attempt to compromise security. Please ensure that you can contact the server that authenticated you. At line:3 char 1
Copy-Item $localPath -Destination $remoteComputerLocation -Force
+CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+FullyQualifiedErrorID : System.IO.IOException, Microsoft.Powershell.Commands.CopyItemCommand.
I am using this to copy files from local computer to remote computer. I have also used -Credential parameter.

PowerShell: Copy-Item fails despite the same process working with Windows Explorer

I have a script that needs to copy files on a regular basis (every hour). I can open the source and destination folder using windows explorer and copy the file without issue.
However, if I try the same thing in PowerShell I get an Access to the path is denied error. I've checked the permissions on the share and I have full access. Why does this fail through PowerShell?
Copy-Item command:
Copy-Item \\idmststtrm2\tns_admin$\tnsnames.ora -Destination \\bts13r2b\tnsnames -Force
Errors:
Copy-Item : Access to the path '\\bts13r2b\tnsnames\tnsnames.ora' is denied.
At line:1 char:1
+ Copy-Item \\idmststtrm2\tns_admin$\tnsnames.ora -Destination \\bts13r2b\tnsnames ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\\idmststtrm2\tns_admin$\tnsnames.ora:FileInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
Copy-Item : Access to the path '\\bts13r2b\tnsnames\tnsnames.ora' is denied.
At line:1 char:1
+ Copy-Item \\idmststtrm2\tns_admin$\tnsnames.ora -Destination \\bts13r2b\tnsnames ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand
Edits:
When I execute a Get-ChildItem against the destination path, I'm able to see the folder contents.
Results from get-item:
get-item \\idmststtrm2\tns_admin$\tnsnames.ora
Directory: \\idmststtrm2\tns_admin$
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/10/2017 8:49 AM 14143 tnsnames.ora
get-item \\bts13r2b\tnsnames\tnsnames.ora
Directory: \\bts13r2b\tnsnames
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 3/8/2017 9:51 AM 15991 tnsnames.ora
get-item \\bts13r2b\tnsnames
Directory:
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 3/21/2017 11:14 AM tnsnames
Tried using xcopy:
xcopy \\idmststtrm2\tns_admin$\tnsnames.ora \\bts13r2b\tnsnames\tnsnames.ora
Access is denied.
This problem is due to permissions writing to a network share. While the share permissions were set correctly, the NTFS permissions were missing. A system administrator will need to ensure both sets of permissions allow for the account to write to the folder. Once this was updated correctly the script was able to perform a copy to the network share.
I know this is old, but I had a hair-pulling experience trying to get a scheduled job (running as a GMSA) to work calling a .ps1 using Copy-Item and getting the same "Access to the path ... is denied" error. I checked and double-check permissions on the remote shares - both Share permissions and NTFS permissions. It ran successfully with my login, it ran successfully with Admin login.
Finally, just for grins, I changed the Share permissions from "All Users" to "Everyone" and it started working! Therefore, it appears that GMSA accounts are not part of "All Users". I would have never guessed!
Hopefully this saves someone 10 hours of fruitless labor...
You have the correct idea, but what if you try using the 'Administrative Share' to access the other system.
Created some standard variables: $Source and $Target. Now we use Get-ChildItem and the switch -Path to grab the file or directory we need. Then we use Copy-Item and the switch -Force to send the file to the other server. This method should work, but will describe another method.
I assume it would look something like this.
$Source = "\\idmststtrm2\c$\app\oracle\product\11.2.0\dbhome_1\network\admin\tns_admin$\tnsnames.ora"
$Target="\\bts13r2b\c$\app\oracle\product\11.2.0\dbhome_1\network\admin\tnsnames"
Get-ChildItem -Path $Source | Copy-Item -Destination $Target -Force
Another option is to make sure that you first have write access to both shared directories. Once that is verified, we run the following:
$Source="\\idmststtrm2\tns_admin$\tnsnames.ora"
$Target="\\bts13r2b\tnsnames"
Get-ChildItem -Path $Source | Copy-Item -Destination $Target -Force
#(Get-Acl $Source).Access #Verify $Source Access
#(Get-Acl $Target).Access #Verify $Target Access
Let us know if this works.
Try opening powershell as an administrator, some times that causes this issue

Error with PowerShell command for copying file to remote server with credential

PS C:\Windows\system32> Copy-Item -ToSession $s C:\Programs\temp\test.txt -Destination C:\Programs\temp\test.txt
Copy-Item : A parameter cannot be found that matches parameter name
'ToSession'. At line:1 char:11
Copy-Item -ToSession $s C:\Programs\temp\test.txt -Destination C:\Programs\temp\ ...
~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Copy-Item], ParameterBindingException
FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
As it is noted in the change list of PoSh V5 here and you tagged your question with V4 the simple answer is probably your version's Copy-Item cmdlet does not provide that parameter
Copy-Item now lets you copy files or folders from one Windows
PowerShell session to another, meaning that you can copy files to
sessions that are connected to remote computers, (including computers
that are running Nano Server, and thus have no other interface). To
copy files, specify PSSession IDs as the value of the new -FromSession
and -ToSession parameters, and add –Path and –Destination to specify
origin path and destination, respectively. For example, Copy-Item
-Path c:\myFile.txt -ToSession $s -Destination d:\destinationFolder.

Access is denied - confusion over COPY-ITEM

I am trying to copy some files from my server to a workstation. If I change $Foldername to C:\the process works fine. However if I leave the code the way it is, i.e. If I decide to copy files to C:\Program Files\Interrogator.
I get this error:
Copy-Item : Access to the path 'C:\Program Files\Interrogator\Setup Instructions.txt' is denied.
At C:\Users\coduy\Desktop\Copy2Test.ps1:20 char:10
+ Copy-Item <<<< -Path \\10.10.0.10\DeploymentShare\Applications\JDE-Interrogator\* -Destination $Foldername
+ CategoryInfo : PermissionDenied: (\\10.10.0.10\De...nstructions.txt:FileInfo) [Copy-Item], Unauthorized
AccessException
+ FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
I can see that the access is denied but why? I have not shared any of the folders and that should not make any difference if I decide to copy to C:\ or C:\Program Files
$net = New-Object -comobject Wscript.Network
$net.MapNetworkDrive("Z:","\\10.13.0.10\DeploymentShare\Applications\JDE Interrogator",0,"mydomain\coduy","P0kroy$")
$uncServer = "\\10.10.0.10\"
$uncFullPath = "\\10.13.0.10\DeploymentShare\Applications\JDE Interrogator"
$username = "coduy"
$password = "password"
$Foldername="C:\Program Files\Interrogator"
net use $uncServer $password /USER:$username
try
{
mkdir C:\'Program Files'\Interrogator
Copy-Item -Path \\10.10.0.10\DeploymentShare\Applications\JDE-Interrogator\* -Destination $Foldername
}
finally {
net use $uncServer /delete
}
Found out that other user has similar issue and this seems to be a reasonable answer:
Windows Vista and above default to not allowing non-administrative
users to write to the `%PROGRAMFILES% folder. This means that you're
not going to be allowed to copy the files there; you're also not going
to be able to save them after doing your find/replace operation.
You can write them to your user documents folder
(%USERPROFILE%\Documents) folder instead, if that will work for you.
share|edit answered Dec 23 '12 at 6:32
Ken White
74.5k770140

Can I use New-Symlink on UNC paths?

I am trying to create a symlink to a file on Windows using PowerShell. I am using the New-Symlink cmdlet from the PowerShell Community Extensions.
After struggling to figure out the syntax of New-Symlink I was able to get it to work correctly on my local drive. However, I am trying to get it to work on a file share and when I run the command there, I get this output
[PC-KHARPER] <~>$ New-Symlink '\\nyprodfs01\profiles$\kharper\testDir\testFile2' '\\nyprodfs01\profiles$\kharper\testFile'
New-Symlink : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:1
+ New-Symlink '\\nyprodfs01\profiles$\kharper\testDir\testFile2' '\\nyprodfs01\pro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Microsoft.Power...stDir\testFile2:UnresolvedPscxPathImpl) [New-Symlink], Unauthoriz
edAccessException
+ FullyQualifiedErrorId : CreateSymbolicLinkError,Pscx.Commands.IO.Ntfs.NewSymlinkCommand
Is this expected output for UNC paths? Or should it be working and I'm just doing something wrong?
If you use the symlink support that comes with PS5 (which came out well after this question was posted) then you can mount a symlink to a UNC share.
[host]: PS> New-Item -ItemType SymbolicLink -Path <localpath> -Value \\<uncpath>
There is a problem if you need to pass credentials as New-Item doesn't take creds. The workaround I use is to mount a PSDrive to the folder and then symlink the PSDrive.
[host]: PS> New-PSDrive Z FileSystem \\<uncpath> -Credential <cred>
[host]: PS> New-Item -ItemType SymbolicLink -Path <path> -Value Z:\