Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何使用PSGSUITE PS模块添加google组?_Arrays_Powershell_Object - Fatal编程技术网

Arrays 如何使用PSGSUITE PS模块添加google组?

Arrays 如何使用PSGSUITE PS模块添加google组?,arrays,powershell,object,Arrays,Powershell,Object,好的,我在一家小企业工作,他们使用谷歌电子表格作为“电话列表” 用于查找和联系员工。我安装了PSGSUITEpowershell模块,它似乎工作得很好,但我对powershell和一般编码都是新手。我与电话列表一起制作的筛选表将员工分别放在这些组中。示例然后是代码 "phone list" Name # Company code Ext. Department Job Title Email Hayden 111-222-333 JOP IT Techn

好的,我在一家小企业工作,他们使用谷歌电子表格作为
“电话列表”
用于查找和联系员工。我安装了
PSGSUITE
powershell模块,它似乎工作得很好,但我对powershell和一般编码都是新手。我与电话列表一起制作的筛选表将员工分别放在这些组中。示例然后是代码

"phone list" 
Name    #   Company code    Ext.    Department  Job Title   Email
Hayden  111-222-333 JOP     IT  Technician  example@example.com
“过滤器2表”

我想将这些电子邮件添加到相应的谷歌群组中

## NOVA BEAZ ##
## add groups in google based on company title 
###
####
# Import Modules
Import-Module PSGSuite
# Create Array of Groups
$Title = (Import-GSSheet -SpreadsheetId "1NtCT5ruoL4Kf4-ec55xe-L8esXcSY8orfd-zOFK4q4k" -SheetName "Filter" -Headers "None"  -Range "A1:1")
$Title = $Title | % { $_ }
$Groups = (Get-GSgroup -Fields "Name" )
    if($Title = $Groups)
        #{add that users email to the group}
    #else 
        {echo "there is now group that matches that"}
主要的问题是,我真的不知道如何正确地运行数组并选择该行中的所有电子邮件以添加到google组,我想我需要一个数组或对象列表形式来存储我的电子邮件,我希望它是动态的。

摘自我的关于如何使用PSGusite模块的文章

请检查以下内容是否回答了您的问题。如果没有,请告诉我

用户进程 首先,我们需要导入模块,然后使用命令
getgsdrivefilelist
查找存储数据的Google工作表

接下来,我们使用命令
importgssheet
导入用户和组数据

从GSheet获取数据 创建组织单位 我们使用
Get-GSOrganizationalUnit
来确定OU是否存在。然后我们使用
newgsorganizationalUnit
创建它,如果它没有

foreach ($Group in $GroupData) {
    $SplitPath = $Group.OrgUnitPath -Split '/'
    $ParentPath = $SplitPath[0..($SplitPath.Count -2)] -join '/'
    $OUPath = $SplitPath[-1]

    $OrgUnit = Get-GSOrganizationalUnit -SearchBase $Group.OrgUnitPath -SearchScope Base -ErrorAction SilentlyContinue
    if ($OrgUnit) {
        "Org Unit {0} exists at {1}" -f $OrgUnit.OrgUnitPath,$OrgUnit.ParentOrgUnitPath
    } else {
        "Org Unit {0} does not exist; attempting to create in {1}" -f $Group.OrgUnitPath,$ParentPath
        try {
            $GSOrgUnit = New-GSOrganizationalUnit -Name $OUPath.ToLower() -ParentOrgUnitPath $ParentPath -Description $Group.Description
            "Created {0} : {1}" -f $GSOrgUnit.OrgUnitPath,$GSOrgUnit.Description
        }
        catch {
            "Unable to create {0}" -f $Group.OrgUnitPath
        }
    }
}
创建组 使用命令
getgsgroup
,我们检查组是否存在。如果该组不存在,请使用
New GSGroup
从电子表格创建该组

foreach ($Group in $GroupData) {
    $GSGroup = Get-GSGroup -Group $Group.Name -ErrorAction SilentlyContinue
    if ($GSGroup) {
        "Group {0} exists" -f $Group.Name
    } else {
        "Group {0} does not exist; attempting to create" -f $Group.Name
        try {
            $NewGSGroup = New-GSGroup -Name $Group.Name -Email $Group.Email -Description $Group.Description
            "Created {0} : {1}" -f $NewGSGroup.Name,$NewGSGroup.Description
        }
        catch {
            "Unable to create {0}" -f $Group.Name
        }
    }
}
创建用户 创建电子表格中列出的用户

foreach ($Group in $GroupData) {
    $GSGroup = Get-GSGroup -Group $Group.Name -ErrorAction SilentlyContinue
    if ($GSGroup) {
        "Group {0} exists" -f $Group.Name
    } else {
        "Group {0} does not exist; attempting to create" -f $Group.Name
        try {
            $NewGSGroup = New-GSGroup -Name $Group.Name -Email $Group.Email -Description $Group.Description
            "Created {0} : {1}" -f $NewGSGroup.Name,$NewGSGroup.Description
        }
        catch {
            "Unable to create {0}" -f $Group.Name
        }
    }
}
  • 首先,根据用户类型确定部门
  • 使用部门设置组织单位路径的变量
  • 为CustomSchema创建所需的哈希表,以将EmployeeType添加到用户
  • 生成随机安全密码
  • 使用命令
    newgsuser
    ,创建新用户
  • 如果用户创建成功,请使用命令
    New GSUserAlias
    尽最大努力根据用户全名创建电子邮件别名
  • Get RandomPassword函数是一个模型。您需要提供自己的密码方法

    您可以从哈希表中省略CustomSchema。如果您感兴趣,博客文章将展示如何手动创建新属性

    将用户分配到组 接下来,我们使用
    Get GSUserList
    获取父OU中所有用户的列表,然后使用
    add GSGroupMember
    将用户添加到组中

    $UserToGroupList = Get-GSUserList -SearchBase '/test' -SearchScope Subtree
    foreach ($User in $UserToGroupList) {
        switch -regex ($User.OrgUnitPath) {
            'academics' { $GroupName = 'Academics'}
            'business' { $GroupName = 'Business'}
        }
        try {
            Add-GSGroupMember -Identity $GroupName -Member $User.User -ErrorAction Stop | Out-Null
            'Added {0} to group {1}' -f $User.User,$GroupName
        }
        catch {
            'Failed to add {0} to group {1}' -f $User.User,$GroupName
        }
    }
    
    注意:我手动创建了一个/test组织单位,并阻止了自动分配许可证,因为我正在使用我的个人G套件帐户。我不希望在月底有任何惊喜


    仅链接的答案通常位于堆栈溢出上。随着时间的推移,链接可能会萎缩,变得不可用,这意味着你的答案对未来的用户来说是无用的。如果你能在实际帖子中提供你答案的一般细节,并引用你的链接作为参考,那将是最好的。@AustenHolland理解。今晚晚些时候我会更新它。
    foreach ($User in $UserData) {
        $Domain = $User.Email.Split('@')[1]
        switch ($User.UserType) {
            'Faculty' { $Department = 'Academics'}
            'Staff'   { $Department = 'Business' }
        }
    
        # Set OU path
        $OrgUnitPath = $GroupData.Where({$_.Name -eq $Department}).OrgUnitPath
    
        # Set employee type custom schema
        $CustomSchemas = @{
            CustomUniversity = @{
                EmployeeType = $User.UserType
            }
        }
    
        # Set a random secure string
        $Password = ConvertTo-SecureString -String (Get-RandomPassword) -AsPlainText -Force
    
        $NewGSUserParams = @{
            PrimaryEmail = $User.Email
            FullName = $User.FullName
            GivenName = $User.GivenName
            FamilyName = $User.FamilyName
            OrgUnitPath = $OrgUnitPath
            CustomSchemas = $CustomSchemas
            Password = $Password
        }
        $NewUser = New-GSUser @NewGSUserParams -ErrorAction SilentlyContinue
        if ($NewUser) {
            'Created user {0} with primary email {1}' -f $User.FullName,$User.Email
        } else {
            'Failed to create user {0}' -f $User.Email
        }
    
        New-GSUserAlias -User $NewUser.PrimaryEmail -Alias ( $NewUser.Name.FullName.Replace(' ',''),$Domain -join '@') -ErrorAction SilentlyContinue | Out-Null
    }
    
    $UserToGroupList = Get-GSUserList -SearchBase '/test' -SearchScope Subtree
    foreach ($User in $UserToGroupList) {
        switch -regex ($User.OrgUnitPath) {
            'academics' { $GroupName = 'Academics'}
            'business' { $GroupName = 'Business'}
        }
        try {
            Add-GSGroupMember -Identity $GroupName -Member $User.User -ErrorAction Stop | Out-Null
            'Added {0} to group {1}' -f $User.User,$GroupName
        }
        catch {
            'Failed to add {0} to group {1}' -f $User.User,$GroupName
        }
    }