2016-09-14 63 views
0

代码:如何调试“术语”X“不被识别为cmdlet,函数,脚本文件或可操作程序的名称”?

G$Folders = Get-childItem C:\permissiontest\ 
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit 
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly 
$objType = [System.Security.AccessControl.AccessControlType]::Allow 

foreach ($TempFolder in $Folders) 
{ 
echo "Loop Iteration" 
$Folder = $TempFolder.FullName 

$acl = Get-Acl $Folder 
$permission = "MRGROUP\ro","Read", $InheritanceFlag, $PropagationFlag, $objType 
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission 

$acl.SetAccessRule($accessRule) 
Set-Acl $Folder $acl 
} 

错误:

G$Folders : The term 'G$Folders' is not recognized as the name of a cmdlet, function, script file, or 
operable program. Check the spelling of the name, or if a path was included, verify that the path is correct 
and try again. 
At C:\script\aclper.ps1:1 char:1 
+ G$Folders = Get-childItem C:\permissiontest\ 
+ ~~~~~~~~~ 
    + CategoryInfo   : ObjectNotFound: (G$Folders:String) [], CommandNotFoundException 
    + FullyQualifiedErrorId : CommandNotFoundException 
+1

那么'G $文件夹'不是一个命令。你期望'G $ name'会做什么?大概是,摆脱G并在第一行使它成为'$ Folders ='。 – TessellatingHeckler

+0

欢迎来到StackOverflow。如果你想让人们帮忙,你至少应该在你的问题上付出一些努力。你从字面上甩掉了脚本和错误,没有上下文。你甚至没有花时间来格式化(我只是为你做的)。 –

回答

1

替换:

G$Folders = Get-childItem C:\permissiontest\ 

有:

$Folders = Get-childItem C:\permissiontest\ 

这将摆脱这种错误

+0

非常感谢,而且成功。如果我希望将其应用于父文件夹并仍包含子文件夹和文件,那该怎么办? – mkbell

+0

Helo,现在我试着在服务器上执行它,请看下面的错误信息。捕获错误 使用“1”参数调用“SetAccessRule”的异常:“无可设置标志。 参数名称:inheritanceFlags”在C:\ temp \ aclper.ps1:15 char: SetAccessRule <<<<($ accessRule) + CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException – mkbell

+0

问题在于inheritanceFlags,所以在第2行中,您定义它看起来像另一个错字-bor应该可能是 - 或 – BenH

相关问题