SETSPN Error: Ldap Error(0x1 -- Operations Error): ldap_get_next_page_s - sql

I am getting an error and I don't understand why? I have googled and looked as much as I can and still do not have a clear answer. Here is what I am fast with.
Checking domain DC=corp,DC=local
Ldap Error(0x1 -- Operations Error): ldap_get_next_page_s
+ CategoryInfo : NotSpecified: (Ldap Error(0x1 ...get_next_page_s:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : computer01.corp.local
Error occured when searching for existing SPN: 0x00000001
Checking domain DC=corp,DC=local
Ldap Error(0x1 -- Operations Error): ldap_get_next_page_s
+ CategoryInfo : NotSpecified: (Ldap Error(0x1 ...get_next_page_s:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : computer01.corp.local
Error occured when searching for existing SPN: 0x00000001
This is the command I am running
$comp = "computer01.corp.local"
Invoke-Command -ComputerName $comp -ScriptBlock {
Invoke-Expression "SETSPN -S MSSQLSvc/$comp:1433 CORP\user.name"
Invoke-Expression "SETSPN -S MSSQLSvc/$comp CORP\user.name"
}

The variable $comp does not exist in your remote session, ie it is $null inside your -ScriptBlock{..} statement. Try this:
$comp = "computer01.corp.local"
Invoke-Command -ComputerName $comp -ScriptBlock {
param($comp)
Invoke-Expression "SETSPN -S MSSQLSvc/$comp:1433 CORP\user.name"
Invoke-Expression "SETSPN -S MSSQLSvc/$comp CORP\user.name"
} -ArgumentList $comp
You can add Write-Host "Working on computer:$comp" below param($comp) to verify you passed variable $comp correctly.

Related

How do i prevent root-site deployments in bamboo from failing when IIS is already stopped

I am deploying root-site deployments and it keeps failing whenever it seems that IIS is stopped. Below is the error i am getting from the failed log in bamboo.
VERBOSE: Performing action iisreset /Stop on web4.testweb.******.com
C:\bambooscripts\ClusterAwareDeploy-RootSiteAndComDependencies.v2.ps1 : Aborting due to an error!
At D:\AtlassianData\bamboo-agent2-home\temp\4489217-4554753-124649565-ScriptBuildTask-17453259024217579627.ps1:23 char:6
+ "# | c:\bambooscripts\ClusterAwareDeploy-RootSiteAndComDependencies.v ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,ClusterAwareDeploy-RootSiteAndComDependencies.v2.ps1
ErrorDetails :
Exception : System.Management.Automation.RemoteException: The Windows Process Activation Service service is not started.
FullyQualifiedErrorId : NativeCommandError
InvocationInfo : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {}
ScriptStackTrace : at <ScriptBlock>, <No file>: line 1
TargetObject : The Windows Process Activation Service service is not started.
Category : NotSpecified
Activity :
Reason : RemoteException
TargetName : The Windows Process Activation Service service is not started.
TargetType : String
The Windows Process Activation Service service is not started.
At C:\bambooscripts\IISFunctions.ps1:319 char:22
+ ... 'Stop' { $result = Invoke-Command -ComputerName $hostName -ErrorAc ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The Windows Pro...is not started.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : p********db.******.lan
Failing task since return code of [powershell -NonInteractive -ExecutionPolicy bypass -Command D:\AtlassianData\bamboo-agent2-home\temp\4489217-4554753-124649565-ScriptBuildTask-17453259024217579627.ps1] was 1 while expected 0

Unable to connect to SQL server via PowerShell using secure password

I'm trying to connect to SQL server via PowerShell using the below (I'm new to this). I always get the error "login failed" when I use secure password (from Get-Credential or password stored in file). But if I pass the password as plaintext instead of secure string, it connects successfully. Could any one please suggest a method to pass secure password, ideally stored in an external file.
The code I ran and the error is below:
$cred = Get-Credential
$pwd = $cred.Password
$uid = $cred.UserName
$SQLServer = "."
$SQLDBName = "TestDB"
#Initialize connection string
$connString = "Data Source=$SQLServer;Database=$SQLDBName;User ID=$uid;Password=$pwd"
#Create a SQL connection object
$conn = New-Object System.Data.SqlClient.SqlConnection $connString
#Attempt to open the connection
$conn.Open()
if($conn.State -eq "Open")
{
# We have a successful connection here
# Notify of successful connection
Write-Host "Test connection successful"
$conn.Close()
}
Exception calling "Open" with "0" argument(s): "Login failed for user 'TestUser'."
At line:18 char:1
+ $conn.Open()
+ ~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
Further details of error from SQL server:
Login failed for user 'TestUser'. Reason: Password did not match that for the login provided. [CLIENT: <local machine>]
Change this
$pwd = $cred.Password
to this
$pwd = $cred.GetNetworkCredential().Password
However, I would advise against storing a plain text password in memory like this. Your method requires it to--at best--be passed as a parameter in plain text, so you need a better method.
Try using this sqlserver module which supports the -Credential parameter in the Invoke-Sqlcmd function.
I was able to pass secure string as password by adding Integrated Security = True; parameter in connection string.
Thank you.

Invoke-SqlVulnerabilityAssessmentScan SetParent failed for Database

I am in need of some help folks. We are trying to get our SQL Vulnerability Assessment to be done via batch in PowerShell. I'm having some issues setting it all up though and am hoping someone can help me out.
I've tried a few different things but end of the day, I am still getting this error:
Invoke-SqlVulnerabilityAssessmentScan : SetParent failed for Database '[master]'.
At line:1 char:1
+ Invoke-SqlVulnerabilityAssessmentScan -Credential $cred -ServerInstan ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-SqlVulnerabilityAssessmentScan], FailedOperationException
+ FullyQualifiedErrorId : Microsoft.SqlServer.Management.Smo.FailedOperationException,Microsoft.SqlServer.Management.PowerShell.VulnerabilityAssessment.InvokeVulnerabilityAssessmentScan
$cred = Get-Credential
$instance = "server\sqldb"
Invoke-SqlVulnerabilityAssessmentScan -Credential $cred -ServerInstance $srv -DatabaseName "master"
I also tried this:
$cred = Get-Credential
$instance = "server\sqldb"
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server -argumentlist $instance
$db = New-Object Microsoft.SqlServer.Management.Smo.Database -argumentlist $srv, "master"
Invoke-SqlVulnerabilityAssessmentScan -Credential $cred -ServerInstance $srv -DatabaseName $db
but still gives me the same result.
Any help with this is immensely appreciated!

Diagnose SQL running from Power Shell

I am trying to run a SQL from Power Shell(which is on my windows 7 64 bit desktop) and the remote database host is MS SQL Server 2012.
The code is:
$Server= ".\DB_HOST_NAME"
$Database = "master"
$UserSqlQuery= $("select count(*) from [master].[sys].[some_table]")
# executes a query and populates the $datatable with the data
function ExecuteSqlQuery ($Server, $Database, $SQLQuery) {
$Datatable = New-Object System.Data.DataTable
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "server='$Server';database='$Database';trusted_connection=true;"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
$Datatable.Load($Reader)
$Connection.Close()
$Datatable
return $Datatable
}
# declaration not necessary, but good practice
$resultsDataTable = New-Object System.Data.DataTable
$resultsDataTable = ExecuteSqlQuery $Server $Database $UserSqlQuery
Write-Host "Statistic: " $resultsDataTable
Write-Host "Message: Transaction Delay is " $resultsDataTable.Rows.Count
When I run this from Windows PowerShell, I get following errors:
Exception calling "Open" with "0" argument(s): "A network-related or
instance-specific error occurred while establishing a connection to
SQL Server. The server was not found or was not accessible. Verify
that the instance name is correct and that SQL Server is configured to
allow remote connections. (provider: SQL Network Interfaces, error: 26
- Error Locating Server/Instance Specified)" At H:\test2.ps1:11 char:5
+ $Connection.Open()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException
Exception calling "ExecuteReader" with "0" argument(s): "ExecuteReader
requires an open and available Connection. The connection's current
state is closed." At H:\test2.ps1:15 char:5
+ $Reader = $Command.ExecuteReader()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
Exception calling "Load" with "1" argument(s): "Value cannot be null.
Parameter name: dataReader" At H:\test2.ps1:16 char:5
+ $Datatable.Load($Reader)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
So the first error is at $Connection.Open() but I don't know anything about power shell to figure out what is wrong. I have tried running the same SQL on the same DB host and it does return a number.
Can I get some help to know what am I doing wrong here?
This code:
$Server= ".\DB_HOST_NAME"
should be:
$Server= "DB_HOST_NAME"
The specification ".\DB_HOST_NAME" is interpreted as a named instance (DB_HOST_NAME) running on your local box.

Powershell expand variable in scriptblock

I am trying to follow this article to expand a variable in a scriptblock
My code tries this:
$exe = "setup.exe"
invoke-command -ComputerName $j -Credential $credentials -ScriptBlock {cmd /c 'C:\share\[scriptblock]::Create($exe)'}
How to fix the error:
The filename, directory name, or volume label syntax is incorrect.
+ CategoryInfo : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : remote_computer
You definitely don't need to create a new script block for this scenario, see Bruce's comment at the bottom of the linked article for some good reasons why you shouldn't.
Bruce mentions passing parameters to a script block and that works well in this scenario:
$exe = 'setup.exe'
invoke-command -ComputerName $j -Credential $credentials -ScriptBlock { param($exe) & "C:\share\$exe" } -ArgumentList $exe
In PowerShell V3, there is an even easier way to pass parameters via Invoke-Command:
$exe = 'setup.exe'
invoke-command -ComputerName $j -Credential $credentials -ScriptBlock { & "C:\share\$using:exe" }
Note that PowerShell runs exe files just fine, there's usually no reason to run cmd first.
To follow the article, you want to make sure to leverage PowerShell's ability to expand variables in a string and then use [ScriptBlock]::Create() which takes a string to create a new ScriptBlock. What you are currently attempting is to generate a ScriptBlock within a ScriptBlock, which isn't going to work. It should look a little more like this:
$exe = 'setup.exe'
# The below line should expand the variable as needed
[String]$cmd = "cmd /c 'C:\share\$exe'"
# The below line creates the script block to pass in Invoke-Command
[ScriptBlock]$sb = [ScriptBlock]::Create($cmd)
Invoke-Command -ComputerName $j -Credential $credentials -ScriptBlock $sb