Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

First off, I'm new to stack. I have referenced stack many times in the past, but recently I have been stuck on this issue for quite sometime. So here goes.

My goal: I am attempting to correlate an array output from VMware that matches a custom value on each VM machine. ( an asset ID ) to a value ( ID Key ) on a microsoft SQL 2000 server.

As such, since this server is pre 2005 I am unable to use the invoke-sqlcmd powershell command. I have to utilize the full SQL connection string and command structure to return a value out of this database. This sql statement and script works fine on its own. Meaning that the sql portion of this script, functioning on its own will pull results out of the database with a manual tag number put in place of my variable "$etag". I'm fairly new to powershell, and sql use from powershell.

So here is my script with names of the protected taken out.

#========================================================================
# Created on:   12/4/2013 2:01 PM
# Created by:   Shaun Belcher
# Filename:     
#========================================================================


function get-inventory
{

Add-PSSnapin VMware.VimAutomation.Core
$date=get-date
$vcenterserver = @("srv-1","srv-2","srv-3")

Connect-VIServer -server $vcenterserver
$toAddr="[email protected]"
$fromAddr="[email protected]"
$smtpsrv="mail.domain.com"
#Variables
$mdesks=@()
$sqlServer = "serverdb"
$sqlDBNAME = "instance"
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$sqlConnection = New-Object System.Data.SqlClient.SqlConnection
$DataSet = New-Object System.Data.DataSet
$sqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.connection = $sqlConnection
$sqlAdapter.SelectCommand = $sqlCmd


#db Connection

$sqlConnection.ConnectionString = "Server = $sqlServer; Database = $sqlDBname;   Integrated Security=True;"
$SqlCmd.connection = $SqlConnection
$SqlCmd.commandtext = $sqlQuery
$sqlAdapter.SelectCommand = $sqlCmd
$sqlQuery += "SELECT INVHARDW_PropTag as proptag, invhardw_clientID as ClientID,     invhardw_notes as Notes FROM INV_Hardware where invhardw_proptag = '$etag';"
$SqlCmd.commandtext = $sqlQuery
$sqlAdapter.SelectCommand = $sqlCmd

$sqlAdapter.Fill($DataSet)
$DataSet.Tables[0]
$sqlConnection.Close()





$mdesks = @($DataSet.Tables[0] | select propTag, ClientID, Notes)

$virtuals= @(Get-VM | select Name,vmhost,memoryMB,@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}})
$etags = @(Get-vm | Get-Annotation |select value,@{N="mDeskNote";E={[string]::Join(',',($mdesk | Where-Object {$mdesks.propTag = $_;}))}},@{N="mDeskClientID";E={[string]::Join(',',($mdesk | Where-Object {$mdesks.propTag = $_;}))}})





if($virtuals -ne $null){
$body = @("
    <center><table border=1 width=50 % cellspacing=0 cellpadding=8 bgcolor=Black cols=3>
    <tr bgcolor=White><td>Virtual Machine</td><td>Host Machine</td><td>Memory Allocated</td><td>DatastoreList</td><td>Asset Tag</td><td>App Note</td><td>App Client ID</td></tr>")


$i = 0
do {

        #if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($virtuals[$i].Name)</td></tr>";$i++}
        #else {$body += "<tr bgcolor=#EFEFEF><td>$($virtuals[$i].Name)</td></tr>";$i++}
        if($i % 2){$body += "<tr bgcolor=#D2CFCF><td>$($virtuals[$i].Name)</td><td>$($virtuals[$i].VMHost)</td><td>$($virtuals[$i].MemorymB)</td><td>$($virtuals[$i].datastore)</td><td>$($etags[$i].value)</td><td>$mdesks[$i].notes</td><td>$mdesks[$i].ClientID</td></tr>";$i++}
        else {$body += "<tr bgcolor=#EFEFEF><td>$($virtuals[$i].Name)</td><td>$($virtuals[$i].VMHost)</td><td>$($virtuals[$i].memorymb)</td><td>$($virtuals[$i].datastore)</td><td>$($etags[$i].value)</td><td>$mdesks[$i].notes</td><td>$mdesks[$i].ClientID</td></tr>";$i++}
}

while ($virtuals[$i] -ne $null)

$body += "</table></center>"
# Send email.
if($attachmentPref){
    $virtuals | Export-CSV "Inventory $($date.month)-$($date.day)-$($date.year).csv"
    Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "$vcenterserver Inventory = $countvms" -Body "$body" -Attachments "Inventory $($date.month)-$($date.day)-$($date.year).csv" -SmtpServer "$smtpsrv" -BodyAsHtml
    Remove-Item "Inventory $($date.month)-$($date.day)-$($date.year).csv"
}
Else{
    Send-MailMessage -To "$toAddr" -From "$fromAddr" -Subject "Inventory $vcenterserver = $countvms" -Body "$body" -SmtpServer "$smtpsrv" -BodyAsHtml
}
}

Disconnect-VIServer -Server $vcenterserver -Confirm:$false exit



get-inventory

This returns the information and sends it in an email with columns and rows of the information. Again, these are two working scripts that just do not return the result that is sought after.

share|improve this question
    
I have trouble understanding what you are really asking about here. Could you please update your question with more details as to what you're having trouble with? –  robert.westerlund Jan 9 '14 at 23:06
    
Sorry, I tried to upload the results as a picture but it wouldn't let me. What I'm looking for is a match of the field Etag to PropTag. I can pull the Etag from VMware, and I can pull the propTag all separately. As soon as I combined these scripts the Etag still functions. However, none of the SQL info is pulled or is able to be referenced. It displays as a blank array or one that isn't referenced correctly. it looks like this. mdesks[0].ClientID iterating through the array values. –  ShaunB417 Jan 13 '14 at 14:01
    
I'm sorry to say, but there seem to be several issues with your script. The first that I notice is the creation of the $query string which uses += instead of = and also uses an unset variable $etag. Second, the expressions in your $etag variable creation seem to have piped themselves into a corner. I suggest starting with separating things into functions to make sure you're able to test each part separately. As it is now it seems like you're doing too much on each line in some cases, making the script harder to read. Perhaps that will make it easier for you to find the errors? –  robert.westerlund Jan 13 '14 at 16:58
    
Thank you for your input, however, $etag is working. It's selecting the single component of $etags. This portion of the script works correctly. The part that doesn't is matching the proptag to etag. The += is a function to add to an array, not set it equal. I do not want to overwrite whats there, but add to it. –  ShaunB417 Jan 14 '14 at 17:37
    
If $etag is working and $query is an already existing array you are working with global variables, which aren't visible in the script you provided above. You are absolutely correct that the proptag matching isn't working. You are using the $mdesks and the $_ variables incorrectly in that script, that's why I suggesteed writing it in smaller functions; so you could try out each part separately and debug them more easily. –  robert.westerlund Jan 14 '14 at 18:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.