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"}
Related
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"
I am trying to execute an powershell script (the script is unzipping the folders in source path with the same name as the .zip folder)
powershell script:
param([string] $sourcepath)
$sourcepath = "F:\Worflow\"
function unzip ($file) {
$dirname = $sourcepath +(Get-Item $file).Basename
New-Item -Force -ItemType directory -Path $dirname
expand-archive -path $file -DestinationPath $dirname -F
}
$zipFiles = Get-LongChildItem -Path $sourcepath -Recurse | Where-Object {$_.Name -like "*.zip"}
foreach($file in $zipFiles)
{
unzip ($file)
}
SSIS Executue process task Arguments:
"-ExecutionPolicy Unrestricted -File C:\MyDataFiles\Unzip.ps1"
The powershell script works fine when ran in PS instance. But the code is failing from SSIS task with an error :
Get-LongChildItem : The term 'Get-LongChildItem' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try
again.
At C:\MyDataFiles\Unzip.ps1:17 char:13
+ $zipFiles = Get-LongChildItem -Path $sourcepath -Recurse | Where-Obj ...
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Get-LongChildItem:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Anyhelp would be appreciated. The goal is using powershell to zip and unzip folders or files.
Looks like you are attempting to use a command without first loading the module.
Get-LongChildItem is part of the open source module PSAlphaFS
Try importing the module in your script first.
Of course it needs to be installed too. ;)
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 need to delete all the content of a folder which may include broken links among others. The folder path is provided by a variable. Problem is that PowerShell fails to remove the broken links.
$folderPath = "C:\folder\"
Attempt 1:
Remove-Item -Force -Recurse -Path $folderPath
Fails with error:
Remove-Item : Could not find a part of the path 'C:\folder\brokenLink'.
At line:1 char:1
+ Remove-Item -Force -Recurse -Path $folderPath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\folder\brokenLink:String) [Remove-Item], DirectoryNot
FoundException
+ FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Attempt 2:
Start-Job { cmd /c rmdir $folderPath }
Fails because $folderPath gets passed as is instead of its value:
Start-Job { cmd /c rmdir $folderPath } | select command
Command
-------
cmd /c rmdir $folderPath
Any suggestion besides using the .NET framework?
EDIT
By broken link I'm referring to a folder which points to a previously mounted partition, that doesn't exist anymore. The folder is still available but when attempting to navigate into it this error occurs because the destination doesn't exist anymore:
Error:
C:\folder\brokenLink refers to a location that is unavailable. It
could be on a hard drive on this computer, or on a network. Check to
make sure that the disk is properly inserted, or that you are
connected to the Internet or your network, and then try again. If it
still cannot be located, the information might have been moved to a
different location.
This will work:
$folderPath = "C:\folderContaingBrokenSymlinks\";
$items = ls $folderPath -Recurse -ea 0;
foreach($item in $items){
if($item.Attributes.ToString().contains("ReparsePoint")){
cmd /c rmdir $item.PSPath.replace("Microsoft.PowerShell.Core\FileSystem::","");
}
else{
rm -Force -Recurse $item;
}
}
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:\