我可以在Azure自动中通过Runbook将所有Azure活动目录用户导出到MySQL数据库吗?

问题描述 投票:0回答:1

我可以在Azure自动化中通过Runbook将所有Azure的Active Directory用户导出到MySQL数据库吗?有人拥有某种代码,例如仅具有用户的名字和姓氏的人吗?

mysql azure azure-active-directory azure-automation
1个回答
0
投票

是的,可以。您的代码看起来像这样。

######## Connect to Azure AD ##################
# Get Azure Run As Connection Name
$connectionName = "AzureRunAsConnection"
# Get the Service Principal connection details for the Connection name
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName         

# Logging in to Azure AD with Service Principal
"Logging in to Azure AD..."
Connect-AzureAD -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint

######## Create CSV of all users ##################

$users = Get-AzureADUser -All $true
$userList = @()

foreach ($user in $users){
    $userex = $user | Select-Object -ExpandProperty ExtensionProperty
    $userProperties = [ordered] @{
        accountEnabled=$user.GivenName
        userPrincipalName=$user.Surname

    }
    $u = new-object PSObject -Property $userProperties
    $userList += $u
}
$userList | Export-Csv  .\users.csv -NoTypeInformation

$csvFullPath = Resolve-Path .\users.csv

######## Import CSV data into MySql ##################

[system.reflection.assembly]::LoadWithPartialName("MySql.Data")
$mysqlConn = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection
$mysqlConn.ConnectionString = "SERVER=localhost;DATABASE=loadfiletest;UID=root;PWD=pwd"
$mysqlConn.Open()
$MysqlQuery = New-Object -TypeName MySql.Data.MySqlClient.MySqlCommand
$MysqlQuery.Connection = $mysqlConn
$MysqlQuery.CommandText = "LOAD DATA LOCAL INFILE '$csvFullPath' INTO TABLE loadfiletest.testtable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '""' LINES TERMINATED BY '\r\n' (GivenName, Surname)"
$MysqlQuery.ExecuteNonQuery()
© www.soinside.com 2019 - 2024. All rights reserved.