Powershell数组与PowerShell的SQL表数据集相关

时间:2014-01-09 22:04:09

标签: sql arrays powershell correlation powercli

首先,我是堆叠的新手。我过去多次引用堆栈,但最近我一直坚持这个问题。所以这里。

我的目标:       我正在尝试关联VMware的阵列输出,该输出与每台VM计算机上的自定义值相匹配。 (资产ID)到Microsoft SQL 2000服务器上的值(ID Key)。

因此,由于此服务器是2005年之前,我无法使用invoke-sqlcmd powershell命令。我必须利用完整的SQL连接字符串和命令结构从该数据库中返回一个值。这个sql语句和脚本可以自行运行。这意味着此脚本的sql部分单独运行将从数据库中取出结果,并使用手动标记号代替我的变量“$ etag”。我是PowerShell的新手,也是PowerShell的sql使用。

所以这是我的脚本,其中包含受保护的名称。

#========================================================================
# 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="user@domain.com"
$fromAddr="user@domain.com"
$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

这将返回信息并将其发送到包含信息的列和行的电子邮件中。同样,这些是两个工作脚本,只是不返回所追求的结果。

0 个答案:

没有答案