我可以在Azure自动化中通过Runbook将所有Azure的Active Directory用户导出到MySQL数据库吗?有人拥有某种代码,例如仅具有用户的名字和姓氏的人吗?
是的,可以。您的代码看起来像这样。
######## 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()