1
我是PowerShell新手,正在編寫代碼將PowerShell導出到特定數據庫,例如ITAMMVCDB.mdf和表名硬件。將powershell導出到sql數據庫中
ive創建此數據庫(SQL Server 2008)與表(假定所有屬性是類型varchar(5000))並嘗試將PowerShell腳本結果導出到此數據庫。
[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 {}
我設法將其導出到一個CSV文件,並正常工作。我應該如何修改上面的代碼將結果導出到「ITAMMVCDB.mdf」數據庫並在名爲「硬件」的表中
在此先感謝。
謝謝你的解決方案。當我在sql studio mgmt中作爲查詢運行時,它工作正常。我如何在PowerShell中運行它?它沒有工作,當我使用$查詢。我是否缺少一些組件? – said
我做了一個測試並創建了一個ITAMMVCDB.mdf數據庫和硬件表,它對我很好,但我需要將-NoTypeInformation添加到CSV創建。你遇到了什麼錯誤。 –