I'm having problem getting symbolic links to work in powershell as I can't create the folder on the network drive. I've tried Test-NetConnection -ComputerName crex2cloud -Port 445 I get a response but when I try to create the symbolic link with the following command; New-Item -ItemType SymbolicLink -path $pathh -Target $target it doesn't work.
$pathh variable is the source and $target is the target folder which I want to create, although each time I attempt to create a symbolic link using the above command I get this error;
New-Item : Cannot find path 'T:\csync' because it does not exist.
At line:1 char:1
+ New-Item -ItemType SymbolicLink -path $pathh -Target $target
When I try using the UNC path, first there is a pause; then this error;
New-Item : Cannot find path '\\crex2cloud\csync' because it does not exist.
At line:1 char:1
+ New-Item -ItemType SymbolicLink -path $pathh -Target $target
Use Test-Path instead of Test-NetConnection
Then you can also wrap it in an if statement:
if (Test-path '\\crex2cloud\csync') {Write-host "path found"}
else
{New-Item -ItemType SymbolicLink -path $pathh -Target $target}
Also double check your permissions on the share. You can use the admin share also.
"\\server\c$\whateverfolder"
Related
I tried with -force but I still get error message
Copy-Item : An item with the specified name C:\folder2 already
exists.
Copy-Item 'c:\folder1' -Destination 'c:\folder2' -force
You can create the destination folder first and then copy everything inside the source folder by adding \* to the path:
# first create the destination folder if it does not already exist
$null = New-Item -Path 'c:\folder2' -ItemType Directory -Force
# then copy all from the source folder to the destination
Copy-Item -Path 'c:\folder1\*' -Destination 'c:\folder2' -Recurse -Force
By using switch -Force on the New-Item command, the cmdlet returns either the object of a newly created folder or the object of an existing folder.
In both cases, we do not need that output, so we'll ignore it using $null =
I am trying to automate log copying between multiple remote computers and a shared folder on said remote computer's network. The script is working fine for the most part, but occasionally (I cannot figure out why) an extension-less file is created instead of a file folder, and the logs' contents are copied into it. When I edit it with notepad++, I can see the contents of the log file I was trying to copy into the folder.
I have this script on multiple remote computers, which all transfer the logs to the same shared network folder (but to different sub-directories). On each machine, I have the script executing at the same time (3:00 AM) via a Windows Task.
$sharedFolderPath = "\\xXxXxXx\xXxXxX\xXxXx\"
$yesterdaysDate = (get-date).AddDays(-1).ToString("yyyy-MM-dd")
#Create Daily Directories
if (-Not(Test-Path "$sharedFolderPath$yesterdaysDate")) {
new-item -Path "$sharedFolderPath" -Name $yesterdaysDate -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "BMS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "BPS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "DDS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "POS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "SCAPPS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "TRCS" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "Optitrack" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterDaysDate\DDS" -Name "Layer Files" -ItemType directory
new-item -Path "$sharedFolderPath\$yesterDaysDate\TRCS" -Name "Catch-All" -ItemType directory
}
Working correctly, I'd expect a folder called "2019-06-30" (or whatever yesterday's date is) to be made, with folders BMS, BPS, DDS, Optitrack, POS, SCAPPS, and TRCS inside, that have their respective logs stored in them.
After the code I provided, I've added these lines:
#fail-safes
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "DDS" -ItemType directory -Force
new-item -Path "$sharedFolderPath\$yesterdaysDate" -Name "BMS" -ItemType directory -Force
Which has seemed to fix the problem. However, for the sake of being robust, I would like to know what's causing this so that I can fix it properly.
I have the following script:
param([Parameter(Mandatory=$true)][string]$dest)
New-Item -force -path "$dest\1\" -itemtype directory
New-Item -force -path "$dest\2\" -itemtype directory
New-Item -force -path "$dest\3\" -itemtype directory
Copy-Item -path "C:\Development\1\bin\Debug\*" -destination "$dest\1\" -container -recurse -force
Copy-Item -path "C:\Development\2\bin\Debug\*" -destination "$dest\2\" -container -recurse -force
Copy-Item -path "C:\Development\3\bin\Debug\*" -destination "$dest\3\" -container -recurse -force
The script takes a string and copies all files and folders from the static origin path to the given root string, amending some folders for structure clarity.
It works fine but prints out the results from the "New-Item" commands and I would like to hide that. I've looked at the net and other questions on SE but no definitive answers to my problem were found.
In case someone is wondering - I am using "New-item" at the beginning in order to circumvent a flaw in PS' -recurse parameter not copying all subfolders correctly if the destination folder does not exist. (I.e. they are mandatory)
Option 1: Pipe it to Out-Null
New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo
Option 2: assign to $null (faster than option 1)
$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo
Option 3: cast to [void] (also faster than option 1)
[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo
See also: What's the better (cleaner) way to ignore output in PowerShell?
I'm running my PowerShell as Admin and I try to create a symbolic link to another directory.
To do so, I want to use the New-Item cmdlet as described in the
Microsoft documentation.
New-Item -ItemType SymbolicLink -Path C:\Temp -Name TestDir -Value C:\LinkedDir
I made sure, all directories (except the symbolic link itself) exist, but still I get this error:
New-Item : Type unkonwn Typ. Only "file" and "directory" can be used.
In Line:1 Row:9
+ New-Item <<<< -ItemType SymbolicLink -Path C:\Temp -Name TestDir -Value C:\LinkedDir
+ CategoryInfo : InvalidArgument: (:) [New-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewItemCommand
If I use the New-Item cmdlet just like this
PS C:\Temp> New-Item -ItemType SymbolicLink
I can enter the paths manually, so this means, that my PowerShell actually knows the cmdlet.
Does anyone know what the problem is and can help me out?
The code above does not work with PowerShell 2.0 .
After Mark Wragg asked which PS version I use, I upgraded to 4.0 and it worked.
So it seems, that some parameters from the New-Item cmdlet are not supported in PS 2.0.
New-Item SymbolicLink error Workaround:
On my computer, this command does't work:
New-Item -ItemType SymbolicLink -Path $link_fullpath -Target $Target
If $link and $target are on the same unit, for example, if $Target= "y:\xxx\myfile1" and $link= "y:\zzz\Myfile2" (both on Y:). If it's not on the same unit, it works.
On the other hand and fortunately, New-Item works if I use -Name like this:
New-Item -ItemType SymbolicLink -name $name_link -Path $path_link -Target $target
Where $name_link = "Myfile2" , $path_link = "y:\zzz" and $Target = "y:\xxx\myfile1"
So, use -Name in New-Item SymbolicLink if you have the same error.
I'd like to create link to a folder on desktop of remote computer. I do not have permissions to execute scripts on that computer, but I can copy files to that computer.
My idea was to create link to folder on local computer and then copy the link to remote computer.
But, I am getting error New-Item : Cannot find path 'C:\SomeFolder' because it
does not exist.
Here is my command:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -Force
Any ideas for workaround?
Try adding the -force parameter:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -force
You can do using mklink also . Make sure that the destination folder is available . You can use the Test-Path to check that :
$destination = "c:\SomeFolder"
if(Test-Path $destination)
{
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
# OR you can use the new-item also. Just commented in the below line
# New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value $destination
}
else
{
New-Item $destination -ItemType Directory -Force
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
}
Hope it helps