{"id":32,"date":"2019-10-02T05:04:14","date_gmt":"2019-10-02T09:04:14","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/?p=32"},"modified":"2019-10-02T08:07:55","modified_gmt":"2019-10-02T12:07:55","slug":"powershell-script-for-exporting-print-server-info-to-oracle","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2019\/10\/02\/powershell-script-for-exporting-print-server-info-to-oracle\/","title":{"rendered":"PowerShell script for exporting Print Server info to Oracle"},"content":{"rendered":"<p>The example below covers how a PowerShell script can connect to a Windows Print Server, how it can query printers information, and how it can connect to an Oracle database to perform a query.<\/p>\n<pre class=\"code\"># Start configuring parameters\nParam (\n\t[string]$Printservers = \"printserver1\",\n\t[string]$OracleServer = \"orcl\",\n\t[string]$OracleUser = \"scott\",\n\t[string]$OraclePassword = \"tiger\"\n\t[string]$sql = \"insert into printer_list (print_server, printer_name, printer_location, printer_comment, printer_ip, printer_driver_name, printer_driver_version, printer_driver, entry_dt) values(:print_server, :printer_name, :printer_location, :printer_comment, :printer_ip, :printer_driver_name, :printer_driver_version, :printer_driver, sysdate) \"\n)\n# End configuring parameters\n\nForEach ($Printserver in $Printservers) { # Start looping through each print server\n\t$Printers = Get-WmiObject Win32_Printer -ComputerName $Printserver\n\n\tForEach ($Printer in $Printers) { # Start looping through each printer\n\t\t[System.Reflection.Assembly]::LoadWithPartialName(\"System.Data.OracleClient\")\n\t\t$connectionString = \"User Id=$OracleUser;Password=$OraclePassword;Data Source=$OracleServer;\"\n\t\t$connection = $null\n\t\t$command = $null\n\n\t\tTry {\n\t\t\t$connection = New-Object System.Data.OracleClient.OracleConnection($connectionString)\n\t\t\t$command = New-Object System.Data.OracleClient.OracleCommand -ArgumentList $sql, $connection\n\t\t\t$connection.Open()\n\t\t\t\n\t\t\t$NoOutput = $command.Parameters.Add(\"print_server\", $Printserver)\n\t\t\t$NoOutput = $command.Parameters.Add(\"printer_name\", $Printer.Name)\n\t\t\t\n\t\t\t$Location = $Printer.Location\n\t\t\tif (!$Location) {\n\t\t\t\t$Location = \" \"\n\t\t\t}\n\t\t\t$NoOutput = $command.Parameters.Add(\"printer_location\", $Location)\n\t\t\t\n\t\t\t$Comment = $Printer.Comment\n\t\t\tif (!$Comment) {\n\t\t\t\t$Comment = \" \"\n\t\t\t}\n\t\t\t$NoOutput = $command.Parameters.Add(\"printer_comment\", $Comment)\n\t\t\t\n\t\t\t$NoOutput = $command.Parameters.Add(\"printer_ip\", $Printer.Portname)\n\t\t\t$Drivers = Get-WmiObject Win32_PrinterDriver -Filter \"__path like '%$($Printer.DriverName)%'\" -ComputerName $Printserver\n\t\t\t\n\t\t\t$DriverVersion = \" \"\n\t\t\t$Driver = \" \"\n\t\t\tForEach ($Driver in $Drivers) {\n\t\t\t\t$Drive = $Driver.DriverPath.Substring(0,1)\n\t\t\t\t$DriverVersion = (Get-ItemProperty ($Driver.DriverPath.Replace(\"$Drive`:\",\"\\\\$PrintServer\\$Drive`$\"))).VersionInfo.ProductVersion\n\t\t\t\t$Driver = Split-Path $Driver.DriverPath -Leaf\n\t\t\t}\n\t\t\t\n\t\t\t$Drivername = $Printer.Drivername\n\t\t\tif (!$Drivername) {\n\t\t\t\t$Drivername = \" \"\n\t\t\t}\n\t\t\t$NoOutput = $command.Parameters.Add(\"printer_driver_name\", $Drivername)\n\t\t\t\n\t\t\t$NoOutput = $command.Parameters.Add(\"printer_driver_version\", $DriverVersion)\n\t\t\t$NoOutput = $command.Parameters.Add(\"printer_driver\", $Driver)\n\t\t\t\n\t\t\t$command.ExecuteNonQuery()\n\t\t}\n\t\tFinally {\n\t\t\tif ($connection -ne $null) {\n\t\t\t\t$connection.Close()\n\t\t\t\t$connection.Dispose()\n\t\t\t}\n\n\t\t\tif ($command -ne $null) {\n\t\t\t\t$command.Dispose()\n\t\t\t}\n\t\t}\n\t} # End looping through each printer\n} # End looping through each print server\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This article provides a PowerShell example for exporting a list of printers from a Windows Print Server and then inserting the records into an Oracle database table.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,26],"tags":[],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-oracle","category-windows"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=32"}],"version-history":[{"count":2,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/32\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}