Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
swehner
Posts: 7 Joined: Sun Mar 03, 2013 12:42 pm
Post
by swehner » Thu Mar 26, 2020 1:40 am
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.
$sqlCon = New-Object System.Data .SqlClient.SqlConnection
$sqlCon .ConnectionString = "Data Source=OFWHSQT1\INFRASTRUKTUR; Integrated Security=True; Initial Catalog=Anmeldedatenbank"
$sqlCon .open( )
$sqlCmd = New-Object System.Data .SqlClient.SqlCommand
$sqlCmd .Connection = $sqlCon
$strCmd = "Select * from Usermappings where username = '$env:USERNAME' and MappingType ='P'"
$sqlCmd .CommandText = $strCmd
$sqlReader = $sqlCmd .ExecuteReader( )
# +++ Datensätze einlesen
# F_WriteLogFile ("Folgende Drucker wurden in der Datenbank gefunden!")
$intLoopcount = 0
[ color= #FF0000]The following part doesn't work. PowerShell jumps straight to SQLReader Close[/color]
While ( $sqlReader .Read( ) )
{
[ String ] $strServer = $sqlReader .Item( "PrintServer" ) .ToString( ) .Trim( )
[ String ] $strShareName = $sqlReader .Item( "ShareName" ) .ToString( ) .ToUpper( )
[ String ] $strPrinterName = $sqlReader .Item( "PrinterName" ) .ToString( ) .ToUpper( )
[ String ] $strPath = "\\" + $strServer + "\" + $strShareName
[ String ] $strKey = "\\" + $strServer + "\" + $strPrinterName
$dicDBprinter .Add( $strKey , $strPath )
[ Int ] $intLoopcount = $intLoopcount + 1
#F_WriteLogFile("`t" + $strPrinterName)
If ( $intLoopcount -gt 30 )
{
#F_WriteLogFile "Möglicher Fehler in DB, Druckeranbindung in Schleife hängengeblieben!!"
[ Bool ] $bolLoopdetect = $True
Exit
}
}
$sqlReader .Close( )
brittneyr
Site Admin
Posts: 687 Joined: Thu Jun 01, 2017 7:20 am
Answers: 10
Been upvoted: 8 times
Post
by brittneyr » Thu Mar 26, 2020 7:23 am
What errors are you receiving?
Brittney Ryn
SAPIEN Technologies, Inc.
swehner
Posts: 7 Joined: Sun Mar 03, 2013 12:42 pm
Post
by swehner » Fri Mar 27, 2020 12:37 am
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.
jvierra
Posts: 14691 Joined: Tue May 22, 2007 9:57 am
Answers: 7
Has voted: 2 times
Been upvoted: 5 times
Post
by jvierra » Fri Mar 27, 2020 10:31 am
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
}
}
jvierra
Posts: 14691 Joined: Tue May 22, 2007 9:57 am
Answers: 7
Has voted: 2 times
Been upvoted: 5 times
Post
by jvierra » Fri Mar 27, 2020 10:51 am
I just ran your code with my changes in PowerShell Studio and it ran just fine.