powershell - 我可以在不使用 CSV 文件的情况下在一行中组合命令并传递变量吗
问题描述
我正在尝试更改新用户主目录的权限。我所写的作品。但是,我想从 Get-Acl 测试中传递数据并在测试 ACL“Everyone”之后修改它而不导出到 CSV 文件。
这就是我要去的地方:如果用户主页具有“每个人的完全控制权”,则将其删除并替换为 user.account ELSE 获得下一个主页。
由于我的 CSV 文件有“好”数据,我可以使用 If-Then-Else 来确定是否修改 ACL。
在我写这篇文章的时候,我的请求是我对接下来要尝试什么的想法......
Get-Acl -Path $HomeDirectory | where accesstostring -like *everyone* | Select pschildname | Export-Csv c:\test\acl.csv -Append
这行得通,但我认为我可以做得更好。
解决方案
这是我完成的脚本 Get-Module
$import_mods = Read-Host -Prompt 'do you NEED to load modules? "y" or "n" (Y)'
if ($import_mods -ine "n")
{
Import-Module ShareUtils, PowerShellAccessControl, Microsoft.PowerShell.Security
}
$newhome = "\\xray\HDs\"
out-file -filepath C:\test\acl.csv
Get-ChildItem -Path \\xray\hd -Directory | select PSChildName | Export-Csv C:\test\xray.csv
$csv1= import-csv -path C:\test\xray.csv
foreach ($user in $csv1)
{
$SAM = $user.PSChildName
$account = get-aduser -Identity $SAM -Properties *
$HomeDirectory = "$($newhome)$($Account.SamAccountName)"
$Acl = Get-Acl -Path $HomeDirectory -Filter *
if ($Acl.AccessToString -like "*everyone*")
{
Write-Output $user.pschildname | out-file -filepath C:\test\acl.csv -Append
$Ar = New-Object system.Security.AccessControl.FileSystemAccessRule($Account.SamAccountName, "modify", "ContainerInherit, ObjectInherit", "None", "Allow")
$Ar1 = New-Object system.Security.AccessControl.FileSystemAccessRule("yourdomain\Domain Admins", "full", "ContainerInherit, ObjectInherit", "None", "Allow")
$Ar2 = New-Object system.Security.AccessControl.FileSystemAccessRule("yourdomain\sadmins", "full", "ContainerInherit, ObjectInherit", "None", "Allow")
$Acl.setaccessrule($Ar2)
$Acl.setaccessrule($Ar1)
$Acl.Setaccessrule($Ar)
# next line disables inheritance and for my enviroment this is there "everyone" comes from
$Acl.SetAccessRuleProtection($true,$false)
Set-Acl $HomeDirectory $Acl
Write-Host $SAM "fixed user"
} else {Write-Host $SAM "good user"}
}
#3.0 rename & date acl.csv to record affected folders
$destpath = 'C:\test\'+"acl_"+(get-date -Format yyyyMMdd)+'.txt'
$sourcepath = 'C:\test\acl.csv'
Move-Item -Path $sourcepath -Destination $destpath
exit
推荐阅读
- selenium - 在 Jmeter 中使用 Selenium 在网页中查找元素
- react-native - onPress 在 React Native Flatlist 中不起作用
- css - 尽管设置了宽度和高度,但图像在加载后导致回流?
- c++ - 虚幻引擎中C++ dll的触发事件
- payment - 如何使用 Stripe 附加付款方式?
- sql - 使用 GROUP BY 包括所有仓库的所有产品的总和
- asp.net - 如何获取 HyperLinkField 的文本值
- excel - 如何使用用户窗体 (VBA) 将文本框连接到另一个工作表
- flutter - 未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'length'
- r - Shiny App中的DataTable未在每一行中显示图标