Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Help create Update Statement to SQL DB

$
0
0

Using a post called "Output to SQL" I was able to create a script to pull specific information and insert into SQL DB, however how do I perform an update on the SQL DB?

The other issue I have is in regards to the Disk drives associated with the server.  Some will have just one (C:) and others can have up to 5 Drives, (C:,D:,E:,F:,etc)

 

Here is my code:

#server_id, server_name, domain_name, Manufacturer, Model,Memory
#Server    Machine_Type    Version    Domain    Free(GB)    Used(GB)    Total(GB)    SubtractedAmount    BilledAllocation    WWN
#Server    Machine_Type    Version    Domain    DriveLetter    Free(GB)    Used(GB)    Total(GB)


$Output = Get-WmiObject -Class Win32_ComputerSystem |
    Select-Object Name,Domain,Manufacturer,Model
   
$DiskSpace =  gwmi -query "select * from Win32_Volume where DriveType='3' AND DriveLetter IS NOT NULL"  | Select `
@{Name="Device";Expression={$_.DriveLetter}},`
@{Name="Capacity";Expression={[math]::round(($($_.Capacity)/1GB),2)}},`
@{Name="FreeSpace";Expression={[math]::round(($($_.FreeSpace)/1GB),2)}},`
@{Name="UsedSpace";Expression={[math]::round((($_.Capacity - $_.FreeSpace)/1GB),2)}},`
@{Name="PercentFree";Expression={[math]::round(($($_.FreeSpace)/$($_.Capacity)*100),2)}}

$ResourceInfo = get-wmiobject Win32_ComputerSystem | select @{name="Memory";expression={($_.TotalPhysicalMemory/1GB).tostring("N0")}},NumberOfProcessors
  
$ServerInfo =  (Get-WmiObject Win32_OperatingSystem) | select  caption, csname, version

# Output to SQL Server table
##Connect to the SQL server and the Database

$connectionString = "Data Source=sqlServer;Initial Catalog=ServerDB;Integrated Security=SSPI" -replace "\s+"," "
$conn = New-Object System.Data.SqlClient.SqlConnection($connectionString)

$conn.Open()


## Create your command
$cmd = $conn.CreateCommand()
#Create SQL Select Statement to see if server exist in sCheck

$ServerExist = "Select * from server where server_name = '$Output.Name'"

if ($ServerExist) {
 update statement
 }
 
 else    {
   #Create SQL Insert Statement with your values
    $insert_stmt = "INSERT INTO Server(server_name, domain_name, Manufacturer, Model, Memory)
        VALUES ('$($Output.Name)','$($Output.Domain)','$($Output.Manufacturer)','$($Output.Model)','$($Output.TotalPhysicalMemory)')" -replace "\s+"," "
   
    $cmd.CommandText = $insert_stmt

    ## Invoke the Insert statement
    $cmd.ExecuteNonQuery()
 
 }
 
   if ($conn -and ($conn.state -eq 'Open'))
    {
        $conn.Close()
    }

If I could please get some assistance with the Update Statement and options for inserting the disk drive info so I can later put into a report. 

 

We have a manger who runs a query against an existing DB but not all the data is found in that one DB.  I am going to have to fix one so I figured I work on a Powershell option since the other uses vbscripting.

 

Please let me know if more data is needed.


Viewing all articles
Browse latest Browse all 6937

Trending Articles