用powershell变量查询mysql数据库

j2cgzkjk  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(386)

我正试图将mysql数据库中的数据解析到powershell中,然后使用这些数据根据powershell变量upn更新ad帐户,但我无法使mysql数据和powershell变量正常工作。我不是一个sql的家伙,所以不确定它是否与mysql语法或powershell脚本的编写方式。
这是脚本末尾的命令,我无法使用它,但我在下面包含了整个代码

$mysqlquery1 =  Invoke-MySqlQuery -Connection $oConnection  -Query 'Select * from user_informations where login='"$user.sAMAccountName"

# 

# Script.ps1

# 

$GetUsersemail = Get-ADUser -Filter * -Properties mail, sAMAccountName, userPrincipalName -SearchBase "OUNAME"| Where-Object {($_.mail -ne $null)} | Select-Object mail,sAMAccountName,userPrincipalName

Import-Module activedirectory
Set-Location C:\MySQL\MySQL-master
Unblock-File -Path .\MySQL.psm1
Import-Module .\mysql.psm1

[System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
[string]$sMySQLUserName = sqluserid
[string]$sMySQLPW = 'password'
[string]$sMySQLDB = 'DB'
[string]$sMySQLHost = 'IP'
[string]$sConnectionString = "server="+$sMySQLHost+";port=3306;uid=" + $sMySQLUserName + ";pwd=" + $sMySQLPW + ";database="+$sMySQLDB+";SslMode=none"

$oConnection = New-Object MySql.Data.MySqlClient.MySqlConnection($sConnectionString)
$Error.Clear()

try

{
    $oConnection.Open()
    write-warning ("it connected")
$dataSet = New-Object System.Data.DataSet
}
catch
{
    write-warning ("Could not open a connection to Database $sMySQLDB on Host $sMySQLHost. Error: "+$Error[0].ToString())
}

ForEach ($user in $GetUsersemail) {
    $spn = $null
    $fullemail = $null
    $spnsplit = $user.mail.split("@")
    $spn = "@" + $spnsplit[1]
    $fullemail = $user.sAMAccountName + $spn

        if ($fullemail -ne $user.userPrincipalName) {

    Switch ($spn) {
            "@upn1.com" {
                Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                           }
            "@upn2.com" {   
                    Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                             }
             "@upn3" { 
                    Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                    }
            "@upn4" {
                    Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                    }
             "@upn5" {
                    Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                    }
              "@upn6" {
                    Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                    }
              "@upn7" {
                    Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                   }
               "@upn8" {
                    Set-ADUser -UserPrincipalName $fullemail -Identity $user.sAMAccountName
                    }
                    }
                    }

               $mysqlquery1 =  Invoke-MySqlQuery -Connection $oConnection  -Query 'Select * from user_informations where login='"$user.sAMAccountName" 
    }

   $oConnection.Close()
dly7yett

dly7yett1#

您需要小心powershell字符串引用;单引号字符串 '' 其中不能有变量,双引号字符串 "" 可以,例如。 " $xyz" 但你不能用 " $xyz.property" 因为powershell对待 .property 作为文本的一部分,而不是变量的一部分。为此,你需要一个子表达式, " $($xyz.property)" 根据需要字符串如何被引用,请尝试:

"select * from user_informations where login='$($user.sAMAccountName)'"

在外面加双引号,所以你可以在里面加单引号和子表达式。结果应该是:

select * from user_informations where login='john.hancock'

(应该是吗 user_information ,单数?)

相关问题