Page 1 of 1

MS SQL and Powershell Studio

Posted: Thu Mar 26, 2020 1:40 am
by swehner
Hi all,

i have a Problem using SQL and PowerShell-Studio lates Version 64Bit.
in PowerShell-Studio the Code doesn't work. If i use
PowerShell-ISE the Code works fine.
  1. $sqlCon = New-Object System.Data.SqlClient.SqlConnection
  2. $sqlCon.ConnectionString = "Data Source=OFWHSQT1\INFRASTRUKTUR; Integrated Security=True; Initial Catalog=Anmeldedatenbank"
  3. $sqlCon.open()
  4.  
  5. $sqlCmd = New-Object System.Data.SqlClient.SqlCommand
  6. $sqlCmd.Connection = $sqlCon
  7. $strCmd = "Select * from Usermappings where username = '$env:USERNAME' and MappingType ='P'"
  8. $sqlCmd.CommandText = $strCmd
  9.  
  10. $sqlReader = $sqlCmd.ExecuteReader()
  11. # +++ Datensätze einlesen
  12. # F_WriteLogFile ("Folgende Drucker wurden in der Datenbank gefunden!")
  13. $intLoopcount = 0
  14.  
  15. [color=#FF0000]The following part doesn't work. PowerShell jumps straight  to SQLReader Close[/color]
  16.  
  17. While ($sqlReader.Read())
  18. {
  19.     [String]$strServer = $sqlReader.Item("PrintServer").ToString().Trim()
  20.     [String]$strShareName = $sqlReader.Item("ShareName").ToString().ToUpper()
  21.     [String]$strPrinterName = $sqlReader.Item("PrinterName").ToString().ToUpper()
  22.     [String]$strPath = "\\" + $strServer + "\" + $strShareName
  23.     [String]$strKey = "\\" + $strServer + "\" + $strPrinterName
  24.     $dicDBprinter.Add($strKey, $strPath)
  25.     [Int]$intLoopcount = $intLoopcount + 1
  26.     #F_WriteLogFile("`t" + $strPrinterName)
  27.     If ($intLoopcount -gt 30)
  28.     {
  29.         #F_WriteLogFile "Möglicher Fehler in DB, Druckeranbindung in Schleife hängengeblieben!!"
  30.         [Bool]$bolLoopdetect = $True
  31.         Exit
  32.     }
  33. }
  34. $sqlReader.Close()

Re: MS SQL and Powershell Studio

Posted: Thu Mar 26, 2020 7:23 am
by brittneyr
What errors are you receiving?

Re: MS SQL and Powershell Studio

Posted: Fri Mar 27, 2020 12:37 am
by swehner
Hi. I Get no error.
$sqlReader say's that $sqlReader has rows, but the while loop jumps directly to $sqlReader.close()

If i use the PowershellIse it works fine.

Re: MS SQL and Powershell Studio

Posted: Fri Mar 27, 2020 10:31 am
by jvierra
SQlReader does not have rows in the normal sense. It has to be moved forward with a "read()" command then the data for a row is available. The object can only be read once then it must be closed and reopened to use it again. There is no need to use the reader. Just load the DataTable and you can then do anything you need easily.

The code you are using is badly converted VB.Net code that is floating around the Internet. No programmer would do this with PowerShell. I can tell this because all of the constructs are from the VB.Net examples of how to code with a reader. They are also old and may have come from a bad VBScript example converted to ADO.Net or PowerShell. We haven't used Hungarian notation for 20 years but old VB and VBScri0pt coders seem to continue using it.

Here is how to code a reader in PowerShell:

Code: Select all

$intLoopcount = 0
While ($sqlReader.Read()){
    
    $strServer = $sqlReader.Item('PrintServer')
    $strShareName = $sqlReader.Item('ShareName')
    $strPrinterName = $sqlReader.Item('PrinterName')
    $strPath = "\\$strServer\$strShareName"
    $strKey = "\\$strServer\$strPrinterName"
    Write-Host $strKey,$strPath 
    $dicDBprinter.Add($strKey, $strPath)
    
    If ($intLoopcount++ -ge 30){
        #F_WriteLogFile "Möglicher Fehler in DB, Druckeranbindung in Schleife hängengeblieben!!"
        Write-Host 'Exiting PowerShell'
        pause
        Exit
    }
}

Re: MS SQL and Powershell Studio

Posted: Fri Mar 27, 2020 10:51 am
by jvierra
I just ran your code with my changes in PowerShell Studio and it ran just fine.