【问题标题】:Substitute icacls.exe with powershell用 powershell 替换 icacls.exe
【发布时间】:2020-07-17 19:15:59
【问题描述】:

我想用 Powershell 命令替换以下 CMD 命令:

Icacls.exe "%SystemDrive%\xxx" /grant *S-X-X-XX-XXX:(CI)(OI)(F) /t /c

我也知道如何使用 Icacls 做到这一点,但我认为使用 PowerShell 有更好的方法。

如果有人能在这方面帮助我,我会很高兴。 :-) 谢谢。

【问题讨论】:

  • 您并没有说您搜索的内容。例如,使用“PowerShell Icacls.exe 替换”。您将看到几乎在所有情况下,icacles 仍然是 PowerShell 脚本中的首选工具。继续使用现有工具并没有错,而且在某些情况下,它们更加直接和高效。我们都喜欢这里的 PowerShell,但要为所需的用例使用正确的工具。 PowerShell 确实提供了 Get-Acl 和 Set Acl cmdlet 以供使用,但“但我认为使用 PowerShell 有更好的方法”这一点可能只是选择与价值的问题。

标签: powershell


【解决方案1】:

内置帮助文件,为您提供指导。

Set-Acl更改指定项的安全描述符,例如 作为文件或注册表项。

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-Acl).Parameters
(Get-Command -Name Get-Acl).Parameters.Keys
<#
# Results

Path
InputObject
LiteralPath
Audit
AllCentralAccessPolicies
Filter
Include
Exclude
...
#>
Get-help -Name Get-Acl -Examples
<#
# Results

Get-Acl C:\Windows

Get-Acl -Path "C:\Windows\k*.log" | 
Format-List -Property PSPath, Sddl

Get-Acl -Path "C:/Windows/k*.log" -Audit | 
ForEach-Object { $_.Audit.Count }

Get-Acl -Path "HKLM:\System\CurrentControlSet\Control" |
Format-List

Get-Acl -InputObject (Get-StorageSubsystem -Name S087)
#>
Get-help -Name Get-Acl -Full
Get-help -Name Get-Acl -Online


(Get-Command -Name Set-Acl).Parameters
(Get-Command -Name Set-Acl).Parameters.Keys
<#
# Results

Path
InputObject
LiteralPath
AclObject
CentralAccessPolicy
ClearCentralAccessPolicy
Passthru
Filter
Include
Exclude
...
#>
Get-help -Name Set-Acl -Examples
<#
# Results

$DogACL = Get-Acl -Path "C:\Dog.txt"

Set-Acl -Path "C:\Cat.txt" -AclObject $DogACL

Get-Acl -Path "C:\Dog.txt" | 
Set-Acl -Path "C:\Cat.txt"

$NewAcl = Get-Acl File0.txt

Get-ChildItem -Path "C:\temp" -Recurse -Include "*.txt" -Force | 
Set-Acl -AclObject $NewAcl
#>
Get-help -Name Set-Acl -Full
Get-help -Name Set-Acl -Online

您还可以通过 Microsoft PowerShellGallery.com 使用其他模块。

Find-Module -Name '*acl*', '*ntfs*' | 
Format-Table -AutoSize
<#
# Results

Version     Name                    Repository Description                                                                                                                                 
-------     ----                    ---------- -----------                                                                                                                                 
1.0.1       ACL-Permissions         PSGallery  A couple of ACL utilities, for repairing c...
1.30.1.28   ACLReportTools          PSGallery  Provides Cmdlets for reporting on Share ACLs.                                                                                               
1.7         ACLHelpers              PSGallery  Modules to help work with ACLs (Access Control Rights)                                                                                      
1.0.1.0     ACLCleanup              PSGallery  A set of tools to help you clean your files...
0.1.2       ACLTools                PSGallery  Module for managing NTFS Acls on files and folders                                                                                          
...
0.4         FileAclTools            PSGallery  Tools for quickly fixing file system ACLs                                                                                                   
...                                                                                                  
4.2.6       NTFSSecurity            PSGallery  Windows PowerShell Module for managing file ...
1.4.1       cNtfsAccessControl      PSGallery  The cNtfsAccessControl module contains DSC re...
1.0         NTFSPermissionMigration PSGallery  This module is used as a wrapper to the popular ...
#>

所以,对于你所展示的

# Review current settings
Get-Acl -Path $env:SystemDrive | 
Format-List -Force
<#
# Results

Path   : Microsoft.PowerShell.Core\FileSystem::C:\Windows\system32
Owner  : NT SERVICE\TrustedInstaller
Group  : NT SERVICE\TrustedInstaller
Access : CREATOR OWNER Allow  268435456
         NT AUTHORITY\SYSTEM Allow  268435456
         NT AUTHORITY\SYSTEM Allow  Modify, Synchronize
         BUILTIN\Administrators Allow  268435456
         BUILTIN\Administrators Allow  Modify, Synchronize
         BUILTIN\Users Allow  -1610612736
         BUILTIN\Users Allow  ReadAndExecute, Synchronize
         NT SERVICE\TrustedInstaller Allow  268435456
         NT SERVICE\TrustedInstaller Allow  FullControl
         APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES Allow  ReadAndExecute, Synchronize
         APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES Allow  -1610612736
         APPLICATION PACKAGE AUTHORITY\ALL RESTRICTED APPLICATION PACKAGES Allow  ReadAndExecute, Synchronize
         APPLICATION PACKAGE AUTHORITY\ALL RESTRICTED APPLICATION PACKAGES Allow  -1610612736
Audit  : 
Sddl   : O:S-1-5-80-956008885-34...
#>

说明

Set-Acl cmdlet 更改指定的安全描述符 项,例如文件或注册表项,以匹配 您提供的安全描述符。

要使用 Set-Acl,请使用 Path 或 InputObject 参数来识别 您要更改其安全描述符的项目。然后,使用 用于提供安全性的 AclObject 或 SecurityDescriptor 参数 具有您要应用的值的描述符。 Set-Acl 应用 提供的安全描述符。它使用的值 AclObject 参数作为模型并更改项目中的值 安全描述符以匹配 AclObject 参数中的值。

参数 -AclObject 指定具有所需属性值的 ACL。 Set-Acl 更改 Path 或 InputObject 参数指定的项目的 ACL 匹配指定安全对象中的值。

您可以将 Get-Acl 命令的输出保存在变量中,然后 使用 AclObject 参数传递变量,或键入 Get-Acl 命令。

表 1 类型:对象 位置:1 默认值:无 接受 管道输入:True (ByValue) 接受通配符:False

所以,你只是做这样的事情......按照上面的例子

$AclSettings = 'WhatEverSettingsYouWant'
Set-Acl -Path $env:SystemDrive -AclObject $AclSettings

这里有一个关于 StackOverflow 的类似问题:

Setting Inheritance and Propagation flags with set-acl and Powershell

然后是这样的指导:

这是描述标志的 MSDN 页面以及结果是什么 他们的各种组合。 https://msdn.microsoft.com/en-us/library/ms229747(v=vs.100).aspx

InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit 

PropagationFlags.None

这里有一些简洁的 PowerShell 代码,用于将新权限应用于 文件夹通过修改其现有的 ACL(访问控制列表)。

# Get the ACL for an existing folder
$existingAcl = Get-Acl -Path 'C:\DemoFolder'

# Set the permissions that you want to apply to the folder
$permissions = $env:username, 'Read,Modify', 'ContainerInherit,ObjectInherit', 'None', 'Allow'

# Create a new FileSystemAccessRule object
$rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $permissions

# Modify the existing ACL to include the new rule
$existingAcl.SetAccessRule($rule)

# Apply the modified access rule to the folder
$existingAcl | Set-Acl -Path 'C:\DemoFolder'
<#
Each of the values in the $permissions variable list pertain to the parameters of this constructor for the FileSystemAccessRule class.
#>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 2013-10-08
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    相关资源
    最近更新 更多