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 8576773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:00:02+00:00 2026-06-11T20:00:02+00:00

I have the following powershell command i have converted from VB script: $conn =

  • 0

I have the following powershell command i have converted from VB script:

$conn = New-Object System.Data.SqlClient.SqlConnection(“Data Source=SERVER; Initial Catalog=DATABASE; Integrated Security=SSPI”) 
$conn.Open() 
$cmd1 = $conn.CreateCommand() 
$cmd1.CommandType = [System.Data.CommandType]::Text 
$cmd1.CommandText =“SELECT guid, name, [Serial Number] FROM dbo.wrksta as a inner join Inv_AeX_HW_Serial_Number as b on a.GUID=b._resourceGUID ORDER BY Name” 
$data = $cmd1.ExecuteReader() 
$cmd2 = $conn.CreateCommand() 
$cmd2.CommandType = [System.Data.CommandType]::Text 
$cmd2.CommandText =“SELECT top 1 [User] FROM dbo.Evt_aex_client_logon WHERE _resourceGuid = (SELECT top 1 Guid FROM dbo.Wrksta WHERE Name='" + $data["name"] + "' ORDER BY WrkstaID DESC) AND Event = 'logon' AND _eventTime > getdate()-60 GROUP BY [User] ORDER BY count([User]) desc” 
$data2 = $cmd2.ExecuteReader() 
$dt = new-object “System.Data.DataTable” 
$dt.Load($data2) 
$dt | format-table 
$conn.Close() 

If you look at the second select statement I am wanting to use a value from the first variable $data but im unsure how to write it. in VB it was like this WHERE Name='” + workstation(“name”).value + “‘ is the following the correct way to write this in powershell? ‘” + $data[“name”] + “‘. Im after the value from the name column stored in the $date variable

The VB used to do this previously is below

connString = "Provider=sqloledb;Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=SSPI;"
objSQL.Open connString 

sqlWorkstations = "SELECT guid, name, [Serial Number] FROM dbo.wrksta as a inner join Inv_AeX_HW_Serial_Number as b on a.GUID=b._resourceGUID ORDER BY Name"
Set workstation = objSQL.Execute(sqlWorkstations)

Do Until (workstation.EOF)

sqlUser = "SELECT top 1 [User] FROM dbo.Evt_aex_client_logon WHERE _resourceGuid = (SELECT top 1 Guid FROM dbo.Wrksta WHERE Name='" + workstation("name").value + "' ORDER BY WrkstaID DESC) AND Event = 'logon' AND _eventTime > getdate()-60 GROUP BY [User] ORDER BY count([User]) desc"
set user = objSQL.Execute(sqlUser)

if not user.eof then
  userName = user("User").value
else
  username = "??????"
end if
  • 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-11T20:00:04+00:00Added an answer on June 11, 2026 at 8:00 pm

    Make things a lot easier on yourself and load the SqlServerCmdletSnapin100 snap-in. If you’re dealing with SQL Server, it handles most of the boilerplate for you and distills running queries to a single cmdlet.

    Your code above assumes that the first query returns only a single row. This may or may not always be the case; I don’t know your database. If it returns multiple records, then your code will only give you data corresponding to the first record from the first query.

    Using SqlServerCmdletSnapin100‘s Invoke-SQLCmd and handling the case of multiple records being returned, do this:

    $data1 = Invoke-Sqlcmd -ServerInstance SERVER -Database DATABASE -Query “SELECT guid, name, [Serial Number] FROM dbo.wrksta as a inner join Inv_AeX_HW_Serial_Number as b on a.GUID=b._resourceGUID ORDER BY Name”;
    foreach ($row in $data1) {
        $name = $row.name;
        $data2 = invoke-sqlcmd -ServerInstance SERVER -Database DATABASE -Query “SELECT top 1 [User] FROM dbo.Evt_aex_client_logon WHERE _resourceGuid = (SELECT top 1 Guid FROM dbo.Wrksta WHERE Name='$name' ORDER BY WrkstaID DESC) AND Event = 'logon' AND _eventTime > getdate()-60 GROUP BY [User] ORDER BY count([User]) desc” ;
        $data2 |ft -AutoSize
    }
    

    It’s also possible that you could retrieve all of your data with a single properly-written query but again, without knowing the data that’s hard to say. You really ought to consult with your DBA regarding these queries to make sure they’re written properly and match your requirements properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Powershell code: Add-Type -Assembly System.Configuration [ExeConfigurationFileMap] $configMap = New-Object ExeConfigurationFileMap
I am running the following command: ([xml](new-object net.webclient).DownloadString( http://blogs.msdn.com/powershell/rss.aspx )).rss.channel.item | format-table title,link The
I have the following powershell script: $BizTalkHosts = BTSSvc* Foreach($svc in Invoke-Command -Computer d-vasbiz01
I have the following string expression in a PowerShell script: select count(*) cnt from
I have set of powershell scripts which gets invoke from command prompt. Currently script
I have the following script in Powershell ISE . cd E:\Data @ xxxx.zip yyyy.zip
I have a powershell script with the following code in it... $appdir = Split-Path
I have the following tiny PowerShell script that's meant to kill some certain processes
I have the following script, $createZip = { Param ([String]$source, [String]$zipfile) Process { echo
I have a powershell script that works wonderfully from the powershell shell and from

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.