Can I use New-Symlink on UNC paths? - powershell

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:\

Related

Powershell copy from linux samba to local windows server folder

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.

How To Transfer File Between 2 PSDrives

I created two PSDrives on my client computer PowerShell session to two different Remote Servers.
New-PSDrive -Name DllFrom -PSProvider FileSystem -Root "\\WPDHSFMSLxx\adap\Database\Install\KareAssistTest\HIDn"
New-PSDrive -Name DllTo -PSProvider FileSystem -Root "\\WTDHSAPPLxx\d\ServerDLLDev"
I can dir either one and contents are displayed.
I can't copy a text file between these 2 drives using Copy-Item:
PS C:\WINDOWS\system32> Copy-Item DllFrom/HelloWorld.txt DllTo/HelloWorld.txt
Copy-Item : Cannot find path 'C:\WINDOWS\system32\DllFrom\HelloWorld.txt' because it does
At line:1 char:1
+ Copy-Item DllFrom/HelloWorld.txt DllTo/HelloWorld.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\WINDOWS\syst...\HelloWorld.txt:String) [
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
I changed the location to the DLLFrom drive and tried it.
Set-Location DllFrom:
PS DllFrom:\>Copy-Item HelloWorld.txt DllTo
and
PS DllFrom:\> Copy-Item \\WPDHSFMSL03\adap\Database\Install\KareAssistTest\HIDn\HelloWorld.txt DllTo
Nothing happened, command line refreshed, but no file showed up.
DllFrom and DllTo are drives, and need a colon in their name when you refer to them. You create them with a -Name without the colon, but later use it when using the drive, just like a single letter drive C: or other providers like HKCU:
Try: Copy-Item DllFrom:/HelloWorld.txt DllTo:/HelloWorld.txt
There is more overview on PSDrives at 4sysops. From the linked page:
Remember that the PSDrive name does not include the colon (:) but you need to include it when using the drive to set location.
When you did Copy-Item HelloWorld.txt DllTo, you'll find a copy of HellowWorld.txt in your current directory named "DllTo"

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

Powershell - NewItemUnauthorizedAccessError

I receive the following error when using the New-Item cmdlet to create backup copies of files:
new-item : Access to the path 'C:\Program Files (x86)\PRTG Network Monitor\webroot\mailtemplates' is denied.
At line:1 char:21
+ foreach ($i in $a) {new-item -itemtype file -name $i.bak}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Program File...t\mailtemplates:String) [New-Item], UnauthorizedAcc
essException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
Here is the command I am executing:
pwd
C:\Program Files (x86)\PRTG Network Monitor\webroot\mailtemplates
$a = ls
foreach ($i in $a) {new-item -itemtype file -name $i.bak}
I am logged on as the local Administrator
Administrator is member of Administrators Group
Administrators Group has full permissions on that folder
I started the POSH console as Administrator
Execution Policy is set to remote signed (just in case it matters)
I can use the New-Item cmdlet to create a test.txt file within that folder
I can use the GUI to create copies of all files within that directory
Not sure where to go from here.
try this:
foreach ($i in $a) {new-item -itemtype file -name "$($i.basename).bak"}