Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:43:23+00:00 2026-06-05T16:43:23+00:00

The issue is with the SQL query that is used to find computers that

  • 0

The issue is with the SQL query that is used to find computers that the selected user has logged into. I execute the query, use an adapter to fill the results into a table, and then read the table objects into an array. I don’t fully understand the syntax and functionality of an array in Powershell so I may be doing this part wrong. Inside the function where the table is read into the array, the contents of the array seem fine (just the computer name) but when I pass that array out of the function and assign it to a variable, the array has the following data: {#, compName}. It appears to be the number of computers found, then the names, if one computer is found the array is {1, ComputerName}. However if multiple computers are found, 2 or more, the array is {2, ComputerNameComputerName}.

Here is the function with the SQL query and the function where the user selects the computer.

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
}

And they are called like this:

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

I’m totally lost, any input is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:43:24+00:00Added an answer on June 5, 2026 at 4:43 pm

    You could simplify your GetComputerList function which in my testing produce the desired results:

    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)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using SQL Server 2000 Query Analyser, and one issue we have is that
I have an issue with a my SQL query. I need to execute a
I have this issue: first, I execute a SQL query using Java and then
I'm having an issue extracting a substring in SQL query results. Here's the situation:
I'm somewhat new to SQL and need help with query syntax. My issue involves
I've run into a syntax issue with SQL. What I'm trying to do here
I have a complex SQL Query, that needs to be filtered further. Part of
I'm having a memory leak issue in a service program that runs SQL scripts
I have a performance issue regarding a quite simple query that run for more
We have a query that takes 2 seconds to run in Sql Server Management

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.