使用Powershell变量查询MYSQL DB

时间:2018-07-25 15:50:02

标签: mysql powershell automation windows-scripting

我正在尝试将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() 

1 个答案:

答案 0 :(得分:2)

您需要小心使用PowerShell字符串引号;单引号字符串'abc'内不能包含变量,双引号字符串"abc"可以例如"abc $xyz",但您不能使用"abc $xyz.property",因为PowerShell将.property视为文本的一部分,而不是变量的一部分。为此,您需要一个子表达式"abc $($xyz.property)"

根据您需要如何结束对字符串的引用,尝试:

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

外部使用双引号,因此您可以在内部加上单引号和子表达式。那应该像这样出来:

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

(单数应该是user_information吗?)