Help reading results

Welcome Forums General PowerShell Q&A Help reading results

Viewing 0 reply threads
  • Author
    Posts
    • #6070
      Participant
      Topics: 1562
      Replies: 0
      Points: 1
      Rank: Member

      by Britjamez at 2013-04-11 09:16:02

      Looking for some help here … Here’s the code first:

      #Set some variables n stuff<br />$EpochTime=88888888<br />add-pssnapin SqlServerCmdletSnapin100<br />add-pssnapin SqlServerProviderSnapin100<br />$GLOBAL:machine = $env:COMPUTERNAME.Tolower&#40;&#41;<br />$MachineName = $machine</p><p>#Function for SQL Query<br />function Stats-Query&#40;$SQLServer&#41;<br />{<br />    invoke-sqlcmd -query &quot;EXEC MYQUERYNAME&quot; -database DATABASENAME -serverinstance $SQLServer    <br />}</p><p>#Results<br />$Results = Stats-Query $MachineName</p><p>#Manipulate results<br />foreach&#40;$Result in $Results&#41;<br />{<br />$str = $Result<br />$str2 = -join &#40;&#40;$str.length-1&#41;..0 | Foreach-Object { $str[$_] }&#41;<br />$rv=[regex]&#39;_&#39;<br />$str2 = $rv.Replace&#40;$str2,&quot;.&quot;,1&#41;<br />$str =  -join &#40;&#40;$str2.length-1&#41;..0 | Foreach-Object { $str2[$_] }&#41;</p><p>Write &quot;servers.&quot; + $MachineName+ &quot;.&quot; + $str + &quot; &quot;+ $Result + &quot; &quot; + $EpochTime&#41;<br />}

      All works fine, except my output. My column names from the sproc are unknown (always change), and the resultset will only have 1 row.
      so 30ish columns and 1 row.

      What I eventually want, is to get an output of:
      servers.MACHINENAME.MODIFIEDCOLUMNNAME COLUMNVALUE 88888888

      How do I do this?!?

      I think it’s my foreach loop that’s wrong! I am stumped!

      by DonJ at 2013-04-11 22:39:56

      You’re going to have to share a bit more detail – what is in $results? What’s sproc (stored procedure?)?

      Can you give a concrete example of what the input might look like, and what the corresponding output should look like?

Viewing 0 reply threads
  • The topic ‘Help reading results’ is closed to new replies.