【问题标题】:Cannot create or update file in cmd, Powershell, nor Vscode. But I can through Sublime, git bash and File Explorer无法在 cmd、Powershell 或 Vscode 中创建或更新文件。但我可以通过 Sublime、git bash 和文件资源管理器
【发布时间】:2019-05-23 07:23:58
【问题描述】:

我使用的是 Windows 10。

当我尝试使用 Powershell 创建文件时,出现以下错误

(注意:同样的行为发生在 cmd.exe不是 Git Bash

PS C:\Users\John\Documents> New-Item hi.txt -type file
New-Item : Could not find file 'C:\Users\John\Documents\hi.txt'.
At line:1 char:1
+ New-Item hi.txt -type file
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : WriteError: 
  (C:\Users\John\Documents\hi.txt:String) [New-Item], 
  FileNotFoundException
+ FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.
  NewItemCommand

我可以从文件资源管理器和 Git Bash 正常创建、打开和更新文件。

我已经检查了目录的权限级别,并且我确实可以完全控制该目录。

PS C:\Users\John> (get-acl .\Documents\).access


FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Users
IsInherited       : False
InheritanceFlags  : None
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : JOHN-DEEPBOOK\JOHN
IsInherited       : False
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : NT AUTHORITY\SYSTEM
IsInherited       : True
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : BUILTIN\Administrators
IsInherited       : True
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

FileSystemRights  : FullControl
AccessControlType : Allow
IdentityReference : JOHN-DEEPBOOK\JOHN
IsInherited       : True
InheritanceFlags  : ContainerInherit, ObjectInherit
PropagationFlags  : None

尝试以管理员身份运行 PowerShell,结果同样如此。

这不是唯一存在此问题的目录。 /Desktop 和 /Contacts 也有同样的问题。我认为这些内置目录可能有一些特别之处,但 /Downloads 工作正常。它们都具有相同的权限级别。

我发现的另一个有趣的行为是,当我从 PowerShell 调用 Sublime 时,它​​遇到了无法保存文件的相同问题。但是当我从开始菜单调用它时,它工作正常。

我也尝试在 Vscode 中进行编辑,但当我尝试保存文件时它崩溃了。当我从 PowerShell 和开始菜单调用时,它都崩溃了。

这似乎更像是一个 Windows 问题,而不仅仅是 PowerShell,因为我在使用 cmd.exe 和 Vscode 时遇到了同样的问题。

如果您对为什么会发生这种情况有任何线索,欢迎提出任何建议!

【问题讨论】:

  • 探索$error 并查看.exception 看看是否可以获得更多信息。
  • 您可以尝试使用 Sysinternal 的 Process Monitor 进行监控。
  • 检查您的个人资料是否确实被重新定位,或者文档库是否也被重新定位。很可能您在访问重新定位的存储时遇到了一些问题。

标签: windows powershell visual-studio-code file-permissions


【解决方案1】:

感谢所有 cmets,但没有一个起作用。

我将自己回答这个问题,因为我找到了解决方法。我将 Documents 库重新定位到一个新文件夹 Documents2,它解决了所有权限问题。

我尝试将 Documents2 重新定位回 Documents(删除并创建新文件夹后),但再次出现权限问题。仍然不知道为什么会发生这种情况,但我暂时先离开它。

【讨论】:

    【解决方案2】:

    我只是偶然发现了同样的问题,似乎受控文件夹访问会默默地阻止 pwsh.exe 写入文档或以其他方式访问它(默默地,如:不会有典型的“拒绝访问”通知)。

    如果您进入受控文件夹访问的“保护历史记录”窗格,您将看到授权给 pwsh.exe 的块。允许该行为,powershell 将按预期工作。

    【讨论】:

      猜你喜欢
      • 2021-12-17
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多