Avatar of mcrmg
mcrmg
 asked on

PS error

Hi,


This is my PS script that I have been using for months. It has been working fine. (I use it once a month.) When I wanted to use it today, I am getting the error.  I am not sure what is happening.. Help..thanks


Exception calling "Fill" with "1" argument(s): "The target principal name is incorrect.  Cannot generate SSPI context."
At C:\Users\Book1.ps1:45 char:2
+     $sqlAdapter.Fill($dataSet)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SqlException
 
Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Users\Book1.ps1:46 char:28
+ ... s[0].Rows | Export-Csv -Delimiter $Delimiter -Path $CsvFile -NoTypeIn ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCsvCommand


****************************

here is the code


################################################## $sqlParameters = @{    'CurrentMonth' = '11/30/2021' } ################################################## function readDataNWrite2File {   param(    [string]$SQLServer,    [string]$SQLDBName,    [string]$delimiter,    [string]$SqlFile,    [HashTable]$sqlParameters,    [string]$csvFile,    [string]$xlsxFile,    [string]$xlsxCombineFile )     $SqlQuery = get-content $SqlFile      #echo $SQLServer      #echo $SQLDBName     #echo $delimiter     #echo $SqlFile     #echo $csvFile     #echo $xlsxFile     echo $sqlParameters    $sqlConnection = New-Object -TypeName System.Data.SqlClient.SqlConnection    $sqlConnection.ConnectionString = "Server = $($SQLServer); Database = $($SQLDBName); Integrated Security = True;"    $sqlCmd = New-Object -TypeName System.Data.SqlClient.SqlCommand    $sqlCmd.CommandText = $sqlQuery    $sqlCmd.Connection = $sqlConnection    $sqlCmd.CommandTimeout = 0    ForEach ($key in $SqlParameters.Keys) {       [void]$sqlCmd.Parameters.AddWithValue($key, $SqlParameters[$key])    }    $sqlAdapter = New-Object -TypeName System.Data.SqlClient.SqlDataAdapter    $sqlAdapter.SelectCommand = $sqlCmd    # Creating Dataset    $dataSet = New-Object -TypeName System.Data.DataSet    $sqlAdapter.Fill($dataSet)    $dataSet.Tables[0].Rows | Export-Csv -Delimiter $Delimiter -Path $CsvFile -NoTypeInformation     $sqlConnection.Close()     # Convert CSV to EXCEL     # load into Excel     $excel = New-Object -ComObject Excel.Application      $excel.Visible = $false     # change thread culture     [System.Threading.Thread]::CurrentThread.CurrentCulture = 'en-US'     $excel.Workbooks.Open($csvFile).SaveAs($xlsxFile,51)     $excel.Quit()     # if the excel exists then add to Excel, else, create a new one      if(![System.IO.File]::Exists($xlsxCombineFile)){         #echo "no"             # Create a new Excel workbook with one empty sheet             $excel = New-Object -ComObject excel.application              $workbook = $excel.Workbooks.Add(1)             # Save & close the Workbook as XLSX.             $Workbook.SaveAs($xlsxCombineFile,51)             $excel.Quit()      }      # Copy individual Excel into a combined Excel         # $xlsxFile # source's fullpath          # $xlsxCombineFile # destination's fullpath          $xl = new-object -c excel.application          $xl.displayAlerts = $false # don't prompt the user          $wb2 = $xl.workbooks.open($xlsxFile, $null, $true) # open source, readonly          $wb1 = $xl.workbooks.open($xlsxCombineFile) # open target          $sh1_wb1 = $wb1.sheets.item(1) # sheet order in destination workbook          $sheetToCopy = $wb2.sheets.item($sheetName) # source sheet to copy          $sheetToCopy.copy($sh1_wb1) # copy source sheet to destination workbook          $wb2.close($false) # close source workbook w/o saving          $wb1.close($true) # close and save destination workbook          $xl.quit()  } #Variable to hold variable   $SQLServer = "192.168.78.22"   $SQLDBName = "myDB"    $delimiter = "," $exportRoot = "\\192.168.78.88\C$\Users\SQL2EXCEL\" $xlsxCombineFile = "${exportRoot}OUTPUT\Book1.xlsx" $sqlFilePath = "\\192.168.78.88\C$\Users\CheckReports\" # Remove output file if exists if (Test-Path $xlsxCombineFile) {   Remove-Item $xlsxCombineFile } # AllLoansInSystem $SqlFile = "${sqlFilePath}\1-CHECK-CheckName.sql" $csvFile = "${exportRoot}CheckName.csv" $xlsxFile = "${exportRoot}CheckName.xlsx" $sheetName = "CheckName" echo $sheetName readDataNWrite2File -SQLServer $SQLServer -SQLDBName $SQLDBName -delimiter $delimiter -SqlFile $SqlFile -sqlParameters $sqlParameters -csvFile $csvFile -xlsxFile $xlsxFile -xlsxCombineFile $xlsxCombineFile #Get-Content -Path $csvFile $folder = [uri]"${exportRoot}OUTPUT" foreach ($w in (New-Object -ComObject Shell.Application).Windows()) {     if ($w.LocationURL -ieq $folder.AbsoluteUri) { $w.Quit();  } } explorer.exe "${exportRoot}OUTPUT"

Open in new window


Open in new window

Powershell

Avatar of undefined
Last Comment
mcrmg

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mcrmg

ASKER
yes, I can connect to the server w/o an issue. What is strange is that I am the only person who has the access to the server.  The script all of the sudden not working...
mcrmg

ASKER
if I changed the connection string to this

$sqlConnection.ConnectionString = "Server = $($SQLServer); Database = $($SQLDBName); Integrated Security = false; UID=sa;PWD=myPWD"

I am still getting the same error

thanks
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes