首页 > 解决方案 > 我可以在不使用 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  

这行得通,但我认为我可以做得更好。

标签: powershell

解决方案


这是我完成的脚本 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

推荐阅读