【问题标题】:Powershell and SQL: data from sql query to an arrayPowershell 和 SQL:从 sql 查询到数组的数据
【发布时间】:2012-06-13 17:40:18
【问题描述】:

问题在于用于查找所选用户已登录的计算机的 SQL 查询。我执行查询,使用适配器将结果填充到表中,然后将表对象读入数组。我不完全理解 Powershell 中数组的语法和功能,所以我可能做错了这部分。在将表读入数组的函数内部,数组的内容看起来不错(只是计算机名称),但是当我将该数组从函数中传递出来并将其分配给变量时,数组具有以下数据:{ #,compName}。它似乎是找到的计算机数量,然后是名称,如果找到一台计算机,则数组是 {1,ComputerName}。但是,如果找到多台计算机(2 台或更多台),则数组为 {2, ComputerNameComputerName}。

这里是带有SQL查询的函数和用户选择计算机的函数。

Function GetComputerList {
    param ($u)
    #use the display name of the user to get their login name
    $queryUser = Get-ADUser -f{DisplayName -eq $u} #-Properties sAMAccountname | Select sAMAccountname
    $queryName = "'"
    $queryName += $queryUser.SamAccountName
    $queryName += "'"
    $query = "SELECT SYS.Netbios_Name0 FROM v_R_System SYS WHERE User_Name0 = $queryName ORDER BY SYS.User_Name0, SYS.Netbios_Name0"

    $connection = new-object system.data.sqlclient.sqlconnection( "Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;")
 
    $adapter = new-object system.data.sqlclient.sqldataadapter ($query, $connection)

    $table = new-object system.data.datatable
    
    $adapter.Fill($table)

    $i = 1
    foreach($object in $table) {
        <#Write-Host "$i. $($object.Netbios_Name0)"
        $i++#>
        $compArray += $object.Netbios_Name0
    }
    foreach($object in $compArray) {
        Write-Host "$i. $($object)"
    }
    
    return @($compArray)
}


Function SelectComputer {
    param ($a)
    $computerNum = Read-Host "Please select a computer. (by number)"
    $computer = ($a[$computerNum])
    return $computer
}

他们是这样称呼的:

$computerArray = GetComputerList -u $selectedUser
$selectedComputer = SelectComputer -a $computerArray

我完全迷失了,感谢任何意见。

【问题讨论】:

    标签: sql data-binding powershell active-directory


    【解决方案1】:

    您可以简化您的 GetComputerList 函数,该函数在我的测试中会产生所需的结果:

    Function GetComputerList {
        param ($u)
        #use the display name of the user to get their login name
        $queryUser = Get-ADUser -f{DisplayName -eq $u} #-Properties sAMAccountname | Select sAMAccountname
       
        $query = @"
            SELECT SYS.Netbios_Name0 
            FROM v_R_System SYS 
            WHERE User_Name0 = '$($queryUser.SamAccountName)'
            ORDER BY SYS.User_Name0, SYS.Netbios_Name0
        "@
        $connection = new-object system.data.sqlclient.sqlconnection( "Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;") 
        $adapter = new-object system.data.sqlclient.sqldataadapter ($query, $connection)
        $table = new-object system.data.datatable
        $adapter.Fill($table) | out-null
        $compArray = @($table | select -ExpandProperty Netbios_Name0)
    
        return @($compArray)
    }
    

    【讨论】:

    • 一切似乎都如我所愿,谢谢!我确保将 $compArray 声明为实际数组并将其返回,并使用 out-null 删除 .Fill 数据。仅选择 Property 与选择 ExpandProperty 有什么区别?
    • 选择属性,即“...| Select Nebios_Name0”返回一个具有单个属性的对象,而-expandproperty 返回一个字符串数组。
    • 啊,这很方便。谢谢。
    【解决方案2】:

    首先,您需要指定$compArray 实际上是一个数组。您从不声明它,因此 PowerShell 将其视为字符串,因此您可以将计算机名称附加到彼此。在GetComputerList 的某个地方声明$compArray 是一个数组:

    $compArray = @()
    

    其次,数组中的第一个数字实际上是 .NET 函数的返回值,该函数被添加到您的管道中。最有可能的罪魁祸首是$adapter.Fill($table)。将其返回值赋给一个变量:

    $numRows = $adapter.Fill($table)
    

    【讨论】:

    • 我建议像下面对 Fill 方法的调用一样使用管道输出 null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多