【问题标题】:Hidden Parent and Subdirectories with Powershell使用 Powershell 隐藏父目录和子目录
【发布时间】:2019-07-08 01:50:05
【问题描述】:

这是我的问题。我试图弄清楚如何隐藏父导演和我用 powershell 创建的所有子目录。这是我正在使用的代码... $f=ni -ItemType 目录 -Path 'C:[name][name][name]\' -Force $f.attributes = '隐藏' 我的问题是它只会隐藏最后一个文件夹而不是整个路径。请帮助我的头发和牙齿都没有了。

$f=ni -ItemType 目录 -Path 'C:[name][name][name]\' -Force $f.attributes = '隐藏'

$f=ni -ItemType 目录 -Path 'C:[name][name][name]\' -Force $f.attributes = '隐藏'

预期结果是隐藏父目录和子目录。实际发生的只是最后一个子目录被隐藏了。

【问题讨论】:

  • [1] 发布代码时请不要使用别名。它们很难阅读,并且可能在其他系统上不存在。 ///// [2] 请阅读有关如何发布格式化代码的说明。这是降价,所以你可能已经知道了。 [grin] ///// [3] 你为什么期望在较低级别的目录上设置任何属性会改变较高级别的目录? [皱眉]
  • 所以这开始让我感到沮丧... $f=ni -ItemType Directory -Path 'C:[name][name][name]\' -Force $f.attributes = 'Hidden ' 我不知道为什么它都被塞进一行并且“\'s”都被删除了......但是到目前为止我的脚本。
  • Lees 评论的最后一部分是你需要阅读的。您正在将变量设置为完整路径,即该路径中的最后一个目录。如果要更改顶部的属性...您需要获取顶部并将其设置在那里。所以,创建你的结构然后获取父目录并设置隐藏属性。
  • 感谢 Lee_Dailey 和 M0lochwalker...让我看看这是否可行。
  • 想想 $f 是什么。从那里去。

标签: powershell


【解决方案1】:

改为在 C:\Name 上设置属性。

【讨论】:

  • 这是一种谨慎的做法,就像 Windows 上的默认隐藏共享一样,但它不会在子文件夹(OP 主请求)上设置属性,因为这种类型没有递归我的回复中提到的事情(整个粗体部分)。然而,我仍然不明白为什么 OP 要为整个树执行此操作,而不仅仅是设置 ACL。
  • 听起来你同意我的回答是足够的。如果需要,OP 可以使用 -recurse。
  • New-Item cmdlet 没有 -recurse 开关。因此,请注意 OP 不能按照书面形式使用递归来完成这项工作。
  • 我不建议他们在 New-Item 上使用 -recurse。我建议他们获取根目录并使用该项目。
  • 我忘记了那一小段重要信息。如果我在父级上设置属性,所有子文件夹也会收到该属性。我试图做一些无法做到的事情。一旦有人说我不得不停下来对自己说......“Duh dummy不能那样做。”
【解决方案2】:

首先,为什么是 wo 行? 他们在做同样的事情。

这不是特定于 PowerShell 的问题。这是 Windows 文件系统约束。因此,如果您在 DOS (cmd.exe) 或任何其他语言中执行此操作并尝试此操作,也会发生同样的事情。

属性应用于您传递给它的单个事物。目录树不是一个单一的东西,它是一个集合。

所以,如果你要这样做,你将不得不迭代树。

最后,如果你隐藏父级,我不确定你为什么需要隐藏子级,因为默认情况下它们会被隐藏(即使未设置属性) ,好吧,除非您知道父名称并导航到子项。然而,无论哪种方式发现隐藏的东西都非常简单。所以,我不确定你想通过这个来完成什么。

无论如何,这就是我的意思......

New-Item -ItemType Directory -Path 'E:\Parent\Child\GrandChild' -Force

# Results

<#
    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild 

Notice that doing what you are doing, only returns the last thing, by Windows 
file system design. This is why you are only getting the setting on the one 
item.
#>



# Not all of them that were created - To get all things in a tree, you have to recurse.
Get-ChildItem -Path 'E:\Parent' -Recurse

# Results

<#

    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild
#>


<#
Since there is no -recurse in New-Item, as per the help files info ...

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-item?view=powershell-6

... after doing the first line to create the tree, iterate to set the attribute 
on the children and grandchildren, etc., first,  then act on the parent.

Again, breaking down the object.
#>


($NewTree = New-Item -ItemType Directory -Path 'E:\Parent\Child\GrandChild' -Force)

# Results

<#
    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   7:16 PM                GrandChild 
#>

$NewTree.Name

# Results
# GrandChild


$NewTree.FullName

# Results
# E:\Parent\Child\GrandChild

$NewTree.Parent

# Results

<#
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:10 PM                Child  
#>

$NewTree.PSChildName

# Results
# GrandChild


Get-ChildItem -Path 'E:\Parent' -Recurse

# Results 

<#
 Get-ChildItem -Path 'E:\Parent' -Recurse


    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:30 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         7/7/2019   8:30 PM                GrandChild
#>

# So, iterate the children and set the attribute

($ChildFolders = (Get-ChildItem -Path 'E:\Parent' -Recurse)) | 
ForEach{$PSItem.Attributes = 'Hidden'}

# Then set the final parent.
($ParentFolder = Get-ChildItem -Directory -Path 'E:\' | 
Where Name -eq 'Parent').Attributes = 'Hidden'

Get-ChildItem -Path 'E:\Parent' -Recurse

# Resutls

<#

#>


Get-ChildItem -Path 'E:\Parent' -Force -Recurse

# Results

<#
 Get-ChildItem -Path 'E:\Parent' -Force -Recurse


    Directory: E:\Parent


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--h--         7/7/2019   8:39 PM                Child


    Directory: E:\Parent\Child


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d--h--         7/7/2019   8:39 PM                GrandChild
#>

【讨论】:

  • 第一行创建了一个变量,第二行设置了属性......他们不做同样的事情。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 2017-08-03
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多