无法验证参数“路径”上的参数

问题描述 投票:0回答:2

你好我正在尝试编写一个脚本来创建一个文件夹,然后为用户添加完全控制权限。

New-Item -ItemType Directory -Force -Path C:\Users\Gabe\Documents\Testfolder
Start-Sleep -Seconds 5
Get-ACL -Path "C:\Users\Gabe\Documents\Testfolder".SetAccessRule'('New-Object System.Security.AccessControl.FileSystemAccessRule("TestUser1","FullControl","Allow")')'
Get-ACL -Path "C:\Users\Gabe\Documents\Testfolder" | Set-Acl -Path "Testfolder"
(Get-ACL -Path "C:\Users\Gabe\Documents\Testfolder").Access | Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags -AutoSize

但是每次我运行它时都会出现此错误。任何帮助表示赞赏。

Get-Acl : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:3 char:15
+ ... t-ACL -Path "C:\Users\Gabe\Documents\Testfolder".SetAccessRule'('New- ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Acl], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAclCommand

我是 Powershell 的新手,不确定接下来要尝试什么。

powershell
2个回答
0
投票

@stackprotector 现在我收到以下错误。

无法将参数“rule”转换为“New-Object System.Security.AccessControl.FileSystemAccessRule("TestUser1","FullControl","Allow")",对于“SetAccessRule” 键入“System.Security.AccessControl.FileSystemAccessRule”:“无法转换”新对象 System.Security.AccessControl.FileSystemAccessRule("TestUser1","FullControl","Allow")"类型"System.String"的值类型 “System.Security.AccessControl.FileSystemAccessRule”。 在行:3 字符:1

  • (Get-ACL -Path "C:\Users\Gabe\Documents\Testfolder").SetAccessRule('N ...
  •   + CategoryInfo          : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
    

0
投票

要添加到@stackprotector,你实际上需要another一组括号也围绕

New-Object
创建(以确保在将值传递回
New-Object
之前首先执行
SetAccessRule
命令):

(Get-ACL -Path "C:\Users\Gabe\Documents\Testfolder").SetAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("TestUser1","FullControl","Allow")))

这会创建 ACL 规则,但(不确定问题的格式)接下来您必须将其传回

Set-Acl
以实际设置 ACL。

其实我喜欢把它分解成几个陈述:

$Directory = "C:\Users\Gabe\Documents\Testfolder"

$Acl = Get-Acl $Directory
$AccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("TestUser1","FullControl","Allow")
$Acl.SetAccessRule($AccessRule)
$Acl | Set-Acl $Directory
© www.soinside.com 2019 - 2024. All rights reserved.