New-LocalUser / LocalGroup路径?

时间:2018-12-12 09:24:00

标签: powershell

我的老板给了我一个在PowerShell中执行的任务,他要我编写脚本。

该脚本应创建一个文件夹并询问文件夹名称,然后使用读写将其分为2个组,然后使2个用户(每组一个)并询问哪个用户应该属于哪个组。然后,将组的权限授予文件夹。

我已经准备好第一部分,创建一个文件夹并询问其名称:

$foldername1 = read-host -Prompt 'input folder name'
new-item "c:\temp\$foldername1" -type Directory

问题是当我创建组和用户时。有没有路? 语法为:

New-LocalUser [-Name] <String> [-AccountExpires <DateTime>] [-AccountNeverExpires] [-Confirm] [-Description <String>] [-Disabl
ed] [-FullName <String>] -NoPassword [-UserMayNotChangePassword] [-WhatIf] [<CommonParameters>]

new-localgroup

相同

如何为正在创建的用户/组选择路径?

我是Powershell的新手,也是我的实习生。

2 个答案:

答案 0 :(得分:0)

path中没有local user这样的东西。

您将看到使用PowerShell或GUI的本地用户:Start => Computer Management => System Tools => Local Users and Groups

答案 1 :(得分:0)

此示例可能与您要查找的内容保持一致。

$foldername1 = read-host -Prompt 'input folder name'
new-item "c:\temp\$foldername1" -type Directory
$group1 = read-host -Prompt 'input first group name'
$group2 = read-host -Prompt 'input second group name'
New-LocalGroup -Name $group1
New-LocalGroup -Name $group2
$user1 = read-host -Prompt 'input first user name'
$user2 = read-host -Prompt 'input second user name'
New-LocalUser -Name $user1
New-LocalUser -Name $user2
$addGroup1 = read-host "Which user in" $group1
if ($addGroup1 -match $user1)
{
Add-LocalGroupMember -Group $group1 -Member $user1
}
if ($addGroup1 -match $user2)
{
Add-LocalGroupMember -Group $group1 -Member $user2
}
$addGroup2 = read-host  "which user in" $group2
if ($addGroup2 -match $user1)
{
Add-LocalGroupMember -Group $group2 -Member $user1
}
if ($addGroup2 -match $user2)
{
Add-LocalGroupMember -Group $group2 -Member $user2
}
write-host "Current members in" $group1 $group2
Get-LocalGroupMember -Group $group1
Get-LocalGroupMember -Group $group2


$rule1 = New-Object System.Security.AccessControl.FileSystemAccessRule 
("$group1","ReadAndExecute","Allow")
$rule2 = New-Object System.Security.AccessControl.FileSystemAccessRule 
("$group2","ReadAndExecute","Allow")
$acl = get-acl "c:\temp\$foldername1"
$acl.SetAccessRule($rule1)
$acl.SetAccessRule($rule2)
$acl | select -ExpandProperty access