Copying directory fails sometimes with "access denied" - windows

I am executing a PowerShell script and this line:
Copy-Item -Path "$A_DIRECTORY" -Destination "$ANOTHER_DIRECTORY" -Recurse -Force
intermittently fails with the following error:
Copy-Item : Access is denied
At C:\mydir\build.ps1:224 char:5
+ Copy-Item -Path "$A_DIRECTORY" -Destination "$ANOTHER_DIRECTORY" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.CopyItemCommand
Now, the error message is clear, but it does not make sense because the target directory does not exist. I have write permissions and also tried to run as administrator with same result.
Why would this statement fail sometimes but not always? Perhaps it is also worth noting that $ANOTHER_DIRECTORY is deleted in the command preceding this one. The delete operation never fails.

Related

BrokenCimSession when stopping a scheduled task

When running the following on one of our Windows machine
$name = "My task"
Stop-ScheduledTask -TaskName $name -ErrorAction SilentlyContinue
I get this error:
Stop-ScheduledTask : Cannot connect to CIM server. The system cannot find the file specified.
+ Stop-ScheduledTask -TaskName $name -ErrorAction SilentlyContinue
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (PS_ScheduledTask:String) [Stop-ScheduledTask], CimJobException
+ FullyQualifiedErrorId : CimJob_BrokenCimSession,Stop-ScheduledTask
It does not look like a permission issus since it would look more like
Stop-ScheduledTask : Access is denied.
+ [void](Stop-ScheduledTask -TaskName $name -ErrorAction SilentlyContinue)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (PS_ScheduledTask:Root/Microsoft/...p_ScheduledTask) [Stop-Schedul
edTask], CimException
+ FullyQualifiedErrorId : HRESULT 0x80070005,Stop-ScheduledTask
A couple of facts to help:
I'm not running this remotely
Get-ScheduledTask will throw the same CimJob_BrokenCimSession error
Are you an administrator on the system? The error says permission denied (Misread OP)
When troubleshooting, it generally helps if you don't have -erroraction silentlycontinue if you actually want to see what's erroring. Obviously, you don't have it in a try/catch block, so you can see the error, but having the option to suppress errors doesn't make sense here.
More suggestions:
Are you running this command remotely? If you're running it against many servers, check your server list.
What happens if you get all the tasks, e.g. $tasks = get-scheduledtask, then reference the one you want with $tasks | where taskname -like "whatever" | stop-scheduledtask (or its index number in the array, e.g. $tasks[5]).
I didn't get any errors when I ran the preceding command against tasks that weren't running, but perhaps it does return an error in some cases if the task isn't in the running state.

Make a copy of swapfile.sys to new location [File not found error]

In PowerShell, I am trying to make a copy of the Windows swapfile (swapfile.sys)
PS C:\> Copy-Item swapfile.sys -Destination C:\Users\
However, I recieve the error:
Copy-Item : Cannot find path 'C:\swapfile.sys' because it does not exist.
At line:1 char:1
+ Copy-Item swapfile.sys -Destination C:\Users\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\swapfile.sys:String) [Copy-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand
I can confirm the file exists with attrib *.sys
Perhaps this is due to the fact it is a hidden file, or a type of file that cannot be copied? I want to investigate this file with a HexEditor and was hoping to make a stand-alone copy for this.

Errors when opening Power Shell or VSC

When I open Windows PowerShell or VSC it prints this message:
Invoke-Expression : At line:1 char:768
... es\Git\cmd;"C:\Users\Shawn\AppData\Local\Microsoft\WindowsApps;C:\Use ...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'C:\Users\Shawn\AppData\Local\Microsoft\WindowsApps' in expression or statement.
At C:\Users\Shawn\anaconda3\shell\condabin\Conda.psm1:101 char:9
Invoke-Expression -Command $activateCommand;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo : ParserError: (:) [Invoke-Expression], ParseException
FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.InvokeExpressionCommand
How do I remedy this so that I can be sure that my system will run things correctly.

search for a folder buried in a folder and move the folder to a new destination

I want to move the Favorites folder from a folder that changes its folder guid daily from Appsense.
Text between quotes changes.
C:\appsensevirtual\S-1-5-21-220523388-2000478354-839522115-60875\'{647CFC75-E4C0-4F13-9888-C37BA083416C}'\_Microsoft Office 2010
I have found this but it never copies to the H: (Homedrive).
Get-ChildItem "C:\Appsensevirtual" -Recurse -Filter "Favorites*" -Directory |
Move-Item -Destination "H:\Favorites"
If i run I get this in an Powershell Administrator Window (powershell 2)
PS C:\temp> .\favorites.ps1
Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At C:\temp\favorites.ps1:1 char:76
+ Get-ChildItem "C:\Appsensevirtual" -Recurse -Filter "Favorites*" -Directory <<<< | Move-Item -Destination "H:\Favorites"
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
You solution is:
Remove -Directory from your command, and add -Force which will parse system & hidden folders.
Get-ChildItem "C:\Appsensevirtual" -Recurse -Filter "Favorites*" -Force |
Move-Item -Destination "H:\Favorites"

Is mkdir still atomic? (Windows 7 filesystems mounted on SAN)

We have some old applications that communicate via directory-based queues. Each item in the queue is a file, and there's a header file that maintains an ordered list of the filenames for the items in the queue.
Naturally, this old code needs to lock the queue while items are pushed and popped. What it's doing is creating a lock subdirectory, on the assumption that mkdir() is an atomic operation - if multiple processes attempt to create a directory, only one of them is going to succeed.
One of my co-workers has been trying to chase down an obscure problem, and he thinks the causes is that this locking is no longer working, when the processes are running on different machines, and when the filesystem in question is mounted on a SAN.
Is there any possibility that he might be correct?
Very old question I know, but I hope someone finds this interesting.
I was also getting confusing results using PowerShell to create a shared folder for use as a mutex, so I created a test script.
function New-FolderMutex {
try {
New-Item -ItemType directory -Path .\TheMutex -ErrorAction Stop > $null
$true
} catch {
$false
}
}
function Remove-FolderMutex {
Remove-Item -Path .\TheMutex
}
1..100 | % {
if (New-FolderMutex) {
Write-Host "Inside loop $_"
Remove-FolderMutex
}
}
This script is run while the current directory is in a network share.
When I ran this script simultaneously in two separate PowerShell consoles, it was clear from the error messages that the approach was doomed. There are a number of different errors produced by the call to Remove-Item, even though it is being called only by the process that created the folder. It seems that behind the scenes there are a whole bunch of non-atomic steps occurring.
Of course, the OP was asking about mkdir (probably the system call) and my example uses the much higher level PowerShell cmdlets, but I hope this is of some interest.
Example output from one of two processes (edited for brevity)
Inside loop 30
Inside loop 31
Inside loop 32
Inside loop 33
Inside loop 34
Remove-Item : Access to the path 'H:\My Documents\PowerShell\MutexFolder\TheMutex' is denied.
At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5
+ Remove-Item -Path .\TheMutex
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (H:\My Documents...Folder\TheMutex:String) [Remove-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : RemoveItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.RemoveItemCommand
Inside loop 39
Remove-Item : H:\My Documents\PowerShell\MutexFolder\TheMutex is a NTFS junction point. Use the Force parameter to delete or modify.
At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5
+ Remove-Item -Path .\TheMutex
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (H:\My Documents...Folder\TheMutex:DirectoryInfo) [Remove-Item], IOException
+ FullyQualifiedErrorId : DirectoryNotEmpty,Microsoft.PowerShell.Commands.RemoveItemCommand
Inside loop 42
Remove-Item : Could not find a part of the path 'H:\My Documents\PowerShell\MutexFolder\TheMutex'.
At H:\My Documents\PowerShell\MutexFolder\Test-FolderMutex.ps1:93 char:5
+ Remove-Item -Path .\TheMutex
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (H:\My Documents...Folder\TheMutex:String) [Remove-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand
Inside loop 44
Inside loop 45

Resources