locked
How to access an expanded property as an object? RRS feed

  • Question

  • suppose i have the following

    Function Query($Query) {
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
    $SqlConnection.ConnectionString = "Server=$Server;Initial Catalog=$Database;Integrated Security=SSPI" 
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
    $SqlCmd.Connection = $SqlConnection 
    $SqlCmd.CommandText = $Query 
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
    $SqlAdapter.SelectCommand = $SqlCmd 
    $DataSet = New-Object System.Data.DataSet 
    $a=$SqlAdapter.Fill($DataSet)
    $SqlConnection.Close() 
    $DataSet.Tables[0] }
    
    $PServers = Query "SELECT DISTINCT [pserver] FROM [dbo].[$Table]" | Select -ExpandProperty pserver;
    $QServers = Query "SELECT DISTINCT [qserver] FROM [dbo].[$Table]" | Select -ExpandProperty qserver;
    
    foreach($server in $PServers + $QServers)
    { 
        if($server is $PServer) {"PServer"}
        else {"QServer"}
    }

    How do i output the statement based on the $server type from either $PServer or $QServer?

    I'd need to access $Pserver or $Qserver as an object but I cant remove expandproperty otherwise i get

    System.Data.DataRow! System.Data.DataRow! System.Data.DataRow! 

    i tried if($server.Item(pserver)){...} and i got

    error: Method invocation failed because [System.String] does not contain a method named 'Item'

    Tuesday, August 20, 2019 1:44 AM

Answers

  • I would start by learning SQL and how it works.  Look at the documentation for a the datatable and then learn enough PowerShell to understand how to use loops with objects.

    All of the PS stuff you should learn before trying to post in any technical forum.  It is like learning how to walk or talk.  You have to do that before you can drive.


    \_(ツ)_/

    figured it out! this is all i needed:

        if ($server -in $PServer) {
            "PServer"
        } else {
            "QServer"
        }

    it was the -in keyword that i needed. thats all

    • Marked as answer by cataster Tuesday, August 20, 2019 3:04 PM
    Tuesday, August 20, 2019 3:04 PM

All replies

  • You code makes it impossible to tell the difference between the different strings.  

    What are you trying to do?


    \_(ツ)_/

    Tuesday, August 20, 2019 2:09 AM
  • You code makes it impossible to tell the difference between the different strings.  

    What are you trying to do?


    \_(ツ)_/

    I am just trying to output based on whichever server the current iteration is 

    if($server is $PServer) {"PServer"}
        else {"QServer"}
    }

    If the server/iteration is a PServer, output something like ”PServer”

    Otherwise, it's a QServer, so output something about QServer, I.e. ”QServer”

    Tuesday, August 20, 2019 2:22 AM
  • Tere is no way to tell that with a collection of strings.


    \_(ツ)_/

    Tuesday, August 20, 2019 2:36 AM
  • Tere is no way to tell that with a collection of strings.


    \_(ツ)_/

    I had a suggestion, by removing expand property we can.select object and access as object therefore

    But if I remove expandproperty I am getting system.data.datarow errors when I run the script. 

    Tuesday, August 20, 2019 2:54 AM
  • Sorry but that won't help.  The DataTable cannot tell you which query it came from so there s no way to do what you are trying to do.

    You still haven't said what you are trying to do.  What you stated as a target cannot be done.  

    You can add a tag to each query to identify the rows.

    "SELECT DISTINCT 'Query1' as QType, [pserver] FROM [dbo].[$Table]"

    You will have to check the property to see which "QType" the row belongs to.


    \_(ツ)_/

    Tuesday, August 20, 2019 3:00 AM
  • Sorry but that won't help.  The DataTable cannot tell you which query it came from so there s no way to do what you are trying to do.

    You still haven't said what you are trying to do.  What you stated as a target cannot be done.  

    You can add a tag to each query to identify the rows.

    "SELECT DISTINCT 'Query1' as QType, [pserver] FROM [dbo].[$Table]"

    You will have to check the property to see which "QType" the row belongs to.


    \_(ツ)_/

    Wait, I like the tag idea. How do I incorporate this in the forloop?

    As in, what do you mean by check thr property to see which Qtype the row belongs to?

    Tuesday, August 20, 2019 3:15 AM
  • I would start by learning SQL and how it works.  Look at the documentation for a the datatable and then learn enough PowerShell to understand how to use loops with objects.

    All of the PS stuff you should learn before trying to post in any technical forum.  It is like learning how to walk or talk.  You have to do that before you can drive.


    \_(ツ)_/

    Tuesday, August 20, 2019 3:20 AM
  • I would start by learning SQL and how it works.  Look at the documentation for a the datatable and then learn enough PowerShell to understand how to use loops with objects.

    All of the PS stuff you should learn before trying to post in any technical forum.  It is like learning how to walk or talk.  You have to do that before you can drive.


    \_(ツ)_/

    figured it out! this is all i needed:

        if ($server -in $PServer) {
            "PServer"
        } else {
            "QServer"
        }

    it was the -in keyword that i needed. thats all

    • Marked as answer by cataster Tuesday, August 20, 2019 3:04 PM
    Tuesday, August 20, 2019 3:04 PM
  • That doesn't make any sense based on your question and explanation.


    \_(ツ)_/

    Tuesday, August 20, 2019 3:11 PM
  • That doesn't make any sense based on your question and explanation.


    \_(ツ)_/

    my post explains exactly whats needed lol. i dont know whats so confusing?

    since i am combining in the forloop the $PServers and $QServers so that the foreach loop can iterate through all servers of both types, i wanted to output a message that details the source of the current iteration, i.e. is the current iteration a pserver or a qserver. 

    since my query is a datatable row, expand property is required otherwise i'd get errors. but because of expand property, i could not access the $PServer or $QServer as objects.

    i.e. 

    if($server.$PServer) {...} else {...}

    so i needed a different way, and the -in keyword was the magic way :)

    Tuesday, August 20, 2019 3:29 PM
  • Ok.  I see what you are trying to do now.  I just couldn't understand your question/discussion.  If you could have explained why it would have been easier to understand.


    \_(ツ)_/

    Tuesday, August 20, 2019 4:27 PM