Im am new to powershell and currenly working out on a code to export powershell result into a specific database for example ITAMMVCDB.mdf and table name Hardware.
ive created this database(SQL Server 2008) with the table (assuming all attribute is type varchar(5000))and trying to export a powershell script result into this database.
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)
begin {}
process {
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $Computer | ? {$_.IPEnabled}
$OsName = Get-WmiObject Win32_OperatingSystem -ComputerName $Computer | Select Name, Version
$Memory = Get-WmiObject Win32_ComputerSystem -ComputerName $Computer | Select TotalPhysicalMemory, Manufacturer, Model , Domain
$Disk = Get-WMIObject Win32_LogicalDisk -ComputerName $Computer | Select DeviceID,Size
$Serial = Get-WMIObject Win32_SystemEnclosure -ComputerName $Computer | Select SerialNumber
$Processor = Get-WMIObject Win32_Processor -ComputerName $Computer | Select Name
$Serial = Get-WMIObject Win32_SystemEnclosure -ComputerName $Computer | Select SerialNumber
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$os = $OsName.Name
$Version=$OsName.Version
$mem= $memory.TotalPhysicalMemory
$Manufacturer= $memory.Manufacturer
$Model= $memory.Model
$Domain = $Memory.Domain
$DiskSize = $Disk.Size
$SerialNo = $Serial.SerialNumber
$ProcessorSpeed = $Processor.Name
$SerialNo = $Serial.SerialNumber
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $os
$OutputObj | Add-Member -MemberType NoteProperty -Name Version -Value $Version
$OutputObj | Add-Member -MemberType NoteProperty -Name TotalPhysicalMemory -Value $mem
$OutputObj | Add-Member -MemberType NoteProperty -Name Manufacturer -Value $Manufacturer
$OutputObj | Add-Member -MemberType NoteProperty -Name Model -Value $Model
$OutputObj | Add-Member -MemberType NoteProperty -Name DiskSize -Value $Disk
$OutputObj | Add-Member -MemberType NoteProperty -Name Processor -Value $ProcessorSpeed
$OutputObj | Add-Member -MemberType NoteProperty -Name SerialNumber -Value $SerialNo
$OutputObj | Add-Member -MemberType NoteProperty -Name Domain -Value $Domain
$OutputObj | Export-CSV -Path “C:\y3s1\report.csv”
}
}
}
}
end {}
i managed to export it to a csv file and works fine. how should i modify the code above to export the result to “ITAMMVCDB.mdf” database and in table named “Hardware”
thanks in advance.
Use the sqlcmd.exe command-line utility which is included with SQL Server 2005 and higher with a simple T-SQL BULK INSERT command: