Powershell - 使用参数中包含的括号调用 icacls [英] Powershell - calling icacls with parantheses included in parameters

查看:53
本文介绍了Powershell - 使用参数中包含的括号调用 icacls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Powershell 还很陌生,但我在 VBScript 和 Python 方面有很多经验.我正在努力成为一名优秀的 Windows 管理员并更多地使用 Powershell.所以,这就是我想要做的:父文件夹包含数十个以 AD 用户名命名的子文件夹(例如,Users\username1、Users\username2,其中 Users 是父文件夹).我想遍历每个文件夹名称,解析出子文件夹名称,并将其传递给 icacls 以根据用户名应用权限.我做了一个多班轮,因为我遇到了管道问题.这是我尝试了几种不同方法后的结果:

I'm pretty new to Powershell, but I have lots of experience in VBScript and Python. I'm trying to be a good Windows admin and get into Powershell more. So, here is what I'm trying to do: A parent folder contains dozens of sub-folders that are named as AD usernames (ex. Users\username1, Users\username2, where Users is the parent folder). I want to loop through each folder name, parse out the sub-folder name, and pass that to icacls to apply permissions based on the username. I did a multi-liner because I was running into issues piping. This is what I have after trying several different approaches:

$root_folder = "c:\temp\test"
$cmd1 = "icacls "
$cmd2 = " /grant cm\"
$cmd3 = ":`(OI`)`(CI`)F"
$paths_collection = get-childitem $root_folder | foreach-object -process {$_.FullName}
foreach ($path in $paths_collection)
{$string = $path.split("\");$last = $string[-1];$command = $cmd1 + $path +$cmd2 +$last +$cmd3;invoke-expression $command}

最初并没有那么粗糙,但是当我遇到问题时,我开始将其分解.

It wasn't originally this rough, but I started breaking it apart when I was running into issues.

问题 - 在 $cmd3 中,(OI)(CI) 没有完全进入调用表达式.如果我将 $cmd3 更改为 ":F" 它可以工作,但我必须使用有问题的参数设置继承.请帮忙.我一整天都在绞尽脑汁想这个.无法真正找到专门处理此问题的任何内容(尝试反引号,将 $command 引用为$command"等)

THE PROBLEM - in $cmd3, the (OI)(CI) is not coming in cleanly to the invoke-expression. If I change $cmd3 to just ":F" it works, but I have to set inheritance using the offending parameters. PLEASE HELP. I've been racking my brain all day on this one. Couldn't really find anything that dealt with this issue specifically (tried backticks, referencing the $command as '$command', etc.)

错误:术语OI"不被识别为 cmdlet、函数、脚本文件或可运行程序的名称.检查拼写名称,或者如果包含路径,请验证路径是否正确,然后重试.在行:1 字符:56+ icacls C:\temp\test\garthwaitm/grant domain\user1:(OI <<<< )(IO)F+ CategoryInfo : ObjectNotFound: (OI:String) [], CommandNotFoundException+ FullQualifiedErrorId : CommandNotFoundException

ERROR: The term 'OI' 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 line:1 char:56 + icacls C:\temp\test\garthwaitm /grant domain\user1:(OI <<<< )(IO)F + CategoryInfo : ObjectNotFound: (OI:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

推荐答案

只是补充一下这个老问题,在 PowerShell 3.0 中,您现在可以使用 --% 来告诉 PowerShell 停止处理线上的任何其他内容,这样您就可以使用这样的东西:

Just to add to this old question, in PowerShell 3.0 you can now use --% to tell PowerShell to stop processing anything else on the line, so you can use something like this:

icacls.exe $path --%/grant "Everyone:(OI)(CI)(F)"

icacls.exe $path --% /grant "Everyone:(OI)(CI)(F)"

这篇关于Powershell - 使用参数中包含的括号调用 icacls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆