Giter VIP home page Giter VIP logo

Comments (25)

gabrielsroka avatar gabrielsroka commented on September 16, 2024

@srirao28
Import-Users calls New-OktaUser which is:
https://github.com/gabrielsroka/OktaAPI.psm1/blob/master/Modules/OktaAPI.psm1#L240-L242

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

Thank you for the comments !

Could you please elaborate a little more on how to achieve my above two tasks.

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

@srirao28

As I wrote in the readme file, this module assumes you're familiar with the Okta API.

For example:
https://developer.okta.com/docs/reference/api/users/#create-user-with-password

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

Great,

I am a begginer to powershell and so finding it difficult to consume all the pointers you are helping me with.

For now, configured a virtual box and trying to figure out by running every cmdlet you created in your module.

Will spend time to obsorb the concepts. Thank you for so much for your time !!

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

You're welcome.

If you noticed, Import-Users doesn't actually mention passwords anywhere, but there are other examples in the same file that do. If you combine the pieces together, you can probably do what you need. For example:

function Import-UsersWithPassword() {
<# Sample users.csv file. Make sure you include the header line as the first record.
login,email,firstName,lastName,password,groupId
[email protected],[email protected],Test,A1,password1,00g5gtwaaeOe7smEF0h7
[email protected],[email protected],Test,A2,password2,00g5gtwaaeOe7smEF0h7
#>
    $users = Import-Csv users.csv
    $importedUsers = @()
    foreach ($user in $users) {
        $newUser = @{
            profile = @{
                login = $user.login
                email = $user.email
                firstName = $user.firstName
                lastName = $user.lastName
            }
            credentials = @{
                password = @{
                    value = $user.password
                }
            }
            groupIds = @($user.groupId)
        }
        $message = ""
        try {
            $oktaUser = New-OktaUser $newUser $true
        } catch {
            try {
                $oktaUser = Get-OktaUser $user.login
            } catch {
                $oktaUser = $null
                $message = "Invalid user."
            }
        }
        $importedUsers += [PSCustomObject]@{id = $oktaUser.id; login = $user.login; message = $message}
    }
    $importedUsers | Export-Csv importedUsers.csv -NoTypeInformation
    "$($users.count) users read."
}

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

wow !!

This is really awesome and nice of you !!

I will save above contents to a file with extension .ps1 and run it for myself on my virtualbox.

Will keep you informed on my outcomes, thank you again so much !!

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

You're welcome.

May I ask why you're running virtualbox? PowerShell runs on Windows, mac and Linux (see my readme for more info).

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

Sure !!

I am using my work laptop, it does have PowerShell, but it has a rule that blocks me from executing any scripts. So, had to set up VBox/CentOS, configure PowerShell and trigger the script from there.

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

I see. It makes sense that it might be locked down.

Did you look at:
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.security/set-executionpolicy?view=powershell-7.1

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

Thank you for the reference document !!

I will surely review and see if that works for me.

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

hi @srirao28

i'm following up. did this work for you? if so, please close this issue (or i'll do it in a few days).

you can always reopen it if necessary.

thanks.

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

Sorry for the delay. Yes, this did work for me, this is great !

QQ - any inputs on the best approach to create groups in okta via csv file.

When to use below snippet ?

# Create a group.
$profile = @{name = $name; description = $description}
$group = New-OktaGroup @{profile = $profile}

** planning to follow below steps:

  1. add below function to file and name it as importgroups.ps1
  2. prepare csv file with required group name & description.
  3. trigger the ps1 script
# Read groups from CSV and create them in Okta.
function New-Groups() {
<# Sample groups.csv file with 2 fields. Make sure you include the header line as the first record.
name,description
PowerShell Group,Members of this group are awesome.
#>
    $groups = Import-Csv groups.csv
    $importedGroups = @()
    foreach ($group in $groups) {
        $profile = @{name = $group.name; description = $group.description}
        try {
            $oktaGroup = New-OktaGroup @{profile = $profile}                   # <---- SEE HERE
            $message = "New group"
        } catch {
            Get-Error $_
            try {
                $oktaGroup = Get-OktaGroups $group.name 'type eq "OKTA_GROUP"'
                $message = "Found group"
            } catch {
                Get-Error $_
                $oktaGroup = $null
                $message = "Invalid group"
            }
        }
        $importedGroups += [PSCustomObject]@{id = $oktaGroup.id; name = $group.name; message = $message}
    }
    $importedGroups | Export-Csv importedGroups.csv -notype
    "$($groups.count) groups read." 
}

Any inputs appreciated !!

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

the 3-line snippet is just an example of how to use New-OktaGroup by itself.

the New-Groups function already calls New-OktaGroup, in a loop, with other stuff (it's about the 12th line in the function).

here's a shorter, simpler version without error checking that might be easier to understand. see line 10 (SEE HERE)

function New-GroupsNoErrorChecking() {
<# Sample groups.csv file with 2 fields. Make sure you include the header line as the first record.
name,description
PowerShell Group,Members of this group are awesome.
#>
    $groups = Import-Csv groups.csv
    $importedGroups = @()
    foreach ($group in $groups) {
        $profile = @{name = $group.name; description = $group.description}
        $oktaGroup = New-OktaGroup @{profile = $profile}                             # <----- SEE HERE
        $importedGroups += [PSCustomObject]@{id = $oktaGroup.id; name = $group.name}
    }
    $importedGroups | Export-Csv importedGroups.csv -notype
    "$($groups.count) groups read." 
}

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

Nice explanation, Thank you for all your time !!

I will run it for myself and see how things works for me and share my further thoughts..

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

Update:

Unfortunately, your script is not working for me. Looks like I messed up something..

---contents of csv file- groups.csv
name, group description
cog-readonly, read only group - cognos
cog-admins, admin group - cognos
cog-sit, sit environment group - cognos

--- contents of ./New-Groups.ps1

#import the OktaAPI module
Import-Module OktaAPI
#Connect-Okta "<API Token>" "https://tenant.okta.com"

#function New-Groups() {
function New-Groups($csvPath){

# Sample groups.csv file with 2 fields. Make sure you include the header line as the first record.
# name,description
# PowerShell Group,Members of this group are awesome.
#
#    $groups = Import-Csv groups.csv
     $groups = Import-Csv $csvPath
    $importedGroups = @()
    foreach ($group in $groups) {
        $profile = @{name = $group.name; description = $group.description}
        try {
            $oktaGroup = New-OktaGroup @{profile = $profile}                   # <---- SEE HERE
            $message = "New group"
        } catch {
            Get-Error $_
            try {
                $oktaGroup = Get-OktaGroups $group.name 'type eq "OKTA_GROUP"'
                $message = "Found group"
            } catch {
                Get-Error $_
                $oktaGroup = $null
                $message = "Invalid group"
            }
        }
        $importedGroups += [PSCustomObject]@{id = $oktaGroup.id; name = $group.name; message = $message}
    }
    $importedGroups | Export-Csv importedGroups.csv -notype
    "$($groups.count) groups read." 
}

any suggestions appreciated !!

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

the header line in csv has to match the code that reads the CSV:

# Sample groups.csv file with 2 fields. Make sure you include the header line as the first record.
# name,description
# PowerShell Group,Members of this group are awesome.

has to match this

        $profile = @{name = $group.name; description = $group.description}

your file is using group description. change it to description and see if it works better.

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

You said "it's not working". I would need more information in order to help you.

Are getting an error? If so, what is it? Is it doing anything? Are you calling the New-Groups function? You've defined it, but I don't see where you're calling it. Is there more code?

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

It looks like Get-Error code no longer works. in Windows PowerShell 5, i get nothing. In PowerShell 6/7, I get an error.

I don't have time to fix this now.

I suggest you re-write the code. Here are two approaches:

  1. if you get an error, just continue, or
  2. search for the group. If it exists, skip it. If it doesn't exist, import it. this is slower than approach 1, but won't create errors

Unless you're finding other problems with my module, I'm going to close this issue. If you need additional help writing your code, I'm available at an hourly rate.

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

from oktaapi.psm1.

srirao28 avatar srirao28 commented on September 16, 2024

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

function Enable-OktaUser($id, $sendEmail = $true) {
Invoke-Method POST "/api/v1/users/$id/lifecycle/activate?sendEmail=$sendEmail"
}

function Add-OktaGroupMember($groupid, $userid) {
$null = Invoke-Method PUT "/api/v1/groups/$groupid/users/$userid"
}

There are no bulk operations in the API as far as I know. You can check the documentation. Otherwise you'll have to call these functions in a loop.

from oktaapi.psm1.

gabrielsroka avatar gabrielsroka commented on September 16, 2024

See also

# Read users from CSV, create them in Okta, and add to a group. See also next function.
function Import-Users() {
<# Sample users.csv file with 5 fields. Make sure you include the header line as the first record.
login,email,firstName,lastName,groupId
[email protected],[email protected],Test,A1,00g5gtwaaeOe7smEF0h7
[email protected],[email protected],Test,A2,00g5gtwaaeOe7smEF0h7
#>
$users = Import-Csv users.csv
$importedUsers = @()
foreach ($user in $users) {
$profile = @{login = $user.login; email = $user.email; firstName = $user.firstName; lastName = $user.lastName}
$message = ""
try {
$oktaUser = New-OktaUser @{profile = $profile} $false
} catch {
try {
$oktaUser = Get-OktaUser $user.login
} catch {
$oktaUser = $null
$message = "Invalid user."
}
}
if ($oktaUser) {
try {
Add-OktaGroupMember $user.groupId $oktaUser.id
} catch {
$message = "Invalid group."
}
}
$importedUsers += [PSCustomObject]@{id = $oktaUser.id; login = $user.login; message = $message}
}
$importedUsers | Export-Csv importedUsers.csv -NoTypeInformation
"$($users.count) users read."
}

from oktaapi.psm1.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.