I have the following code which sucessfully stops a service on remote computer when run from the powershell console.
$Computer = "192.168.24.23"
$service = "Credential Checking LIVE"
Get-Service -ComputerName $Computer -name $service | Stop-Service
When i run this through a SQL Agent job the -ComputerName parameter is ignored.
Any ideas on why this would be and what i can do to rectify the problem?
I'd check the following
1) Does the SQL agent have rights to execute? (like briantist said)
2) Have you tried putting the logic in a script and executing the script from SQL?
3) Did you forget that you had to enter Set-Execution policy while testing?
Related
I have a really frustrating situation here. There is a PowerShell script that logs into the Azure SQL, queries table, and saves results into the .csv file. Connection string looks like this:
$DatabaseUrl = "mycompany-***-***-dev-use4-01.database.windows.net"
$DatabaseName = "MyDataBase"
$conn = New-Object System.Data.SqlClient.SQLConnection
$conn.ConnectionString = "Data Source=$DatabaseUrl;Initial Catalog=$DatabaseName;Connect Timeout=20"
$conn.AccessToken = $token
$conn.Open
On $conn.Open line the script throws this error:
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: TCP Provider, error: 0 - An
attempt was made to access a socket in a way forbidden by its access permissions
Now, the frustration comes when this script works perfectly fine in PowerShell ISE, but not in PowerShell CLI (command line). I have searched millions of posts probably by now, still not finding any solution. I tried to run CLI as an administrator, tried to Import-Module -SQLServer -Scope CurrentUser and others. Honestly, I have absolutely no idea why the script would work in ISE but not in CLI. Are those 2 different environments/scripting languages? Thank you for all your help
I'm using Azure DevOps pipelines, and have a PowerShell task that runs stuff with Invoke-Sqlcmd. The PowerShell works fine when run from my computer, but when it runs through the pipeline it says it can't find or doesn't have access to the server. I don't see anything in the failed connection logs on my sql servers...
I assume whatever account the pipeline is attempting to connect under does not have access. How can I find out what that account is?
If you're curious, here's the simple PS, it just updates a table:
Invoke-Sqlcmd -ServerInstance "myremoteserver" -Query "--update the table"
You can add a powershell task to run below script to get the current user account that your pipeline is using
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
New to Powershell and scripting. I have the following script that intermittently returns the System error 1312 A specified logon does not exist. It may have already been terminated.
Get-Content D:\Logbooks.txt | ForEach-Object {Invoke-Command -computername $_ -FilePath "D:\Folder\Script.ps1"
if (-not$?){Write-Warning "$_ - SQL SCRIPT FAILED"} else {Write-Host "$_ - SQL SCRIPT SUCCESSFUL"-ForegroundColor "Green"}}
I wrote that script to execute this one.
net use \computername\D$ password /User:username
sqlcmd -S .\SQLEXPRESS -o C:\test.txt -d database -i "\computername\D$\Folder\SQL Script.sql"
I am using these to execute a SQL script on remote machines running SQL Express. All computers are running Windows 7. I also created a second admin account on the computer I am running the scripts from and using that login after the net use command. It works fine sometimes but other times I get the error from only a couple of remote computers.
It was a network issue. I was continually running the script to test every little change. Attempting to reconnect to all the remote computers constantly was too much for our small slow network.
I have a Powershell script that loops through a list of SQL Servers and creates server logins and database users.
The script runs on a separate server, under the administrator credentials on that server, and connects to the other SQL Servers via linked servers.
#Get administrator credentials
$password = get-content C:\Powershell\General\password.txt | convertto-securestring;
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\administrator",$password;
When this script is run manually (either directly through a Powershell window or using a batch file through a command prompt) it works perfectly well. I am logged onto the executing server as administrator when running the script manually.
I then tried to run this Powershell script using an SSIS package on the executing server, using the Execute Process Task to run a batch file. The package was executed from a SQL Agent Job. Although both the job and the package seemed to execute successfully, the DDL statements were not executed against the linked servers.
SQL Agent on the executing server is run under a designated Service Account. SSIS runs under the Network Service account.
Does anybody have any thoughts on what I might be doing wrong? I am happy to provide details of the script or anything else that is required.
Thanks
Ash
UPDATE: ok we have a little more information.
I took out the lines I posted above as I have discovered I don't actually need the administrator credentials I was retrieving.
I logged onto the server with the script on it using the service account. As per #ElecticLlama's suggestion I set a Profiler trace on the destination server. When running the script manually (or running a batch file manually that runs the Powershell script) everything works well and the Profiler shows the DDL actions, under the service account login.
When running a job through SQL Agent (either a CmdExec job or an SSIS package) that runs the same batch file, I get the following error:
'Login failed for user 'DOMAIN\ServiceAccount'. Reason: Token-based server access validation failed with an infrastructure error.'
Anybody have any further thoughts?
Thnaks to everyone for their help. Once I got that last error a quick search revealed I just had to restart SQL Agent and now everything works as it should. Thanks in particular to #ElecticLlama for pointing me in the right direction.
Ash
I have set up an SQL server agent job with a few steps that needs to be processed each week. One of which is a PowerShell script that will throw the following (uncaught) custom exception:
$endfile = Test-Path "C:\Temp\1_${loc}_${lastpayweek}.csv"
IF ($endfile -eq $true)
{
throw "target file already exists"
}
Now when this occurs, I can see in the server agent history that an exception was thrown but the step is still reported as a success. Without a failed, the server agent will not send out a notification email to let me know of the failure.
I am calling the PowerShell script like this:
powershell C:\scripts\rename_timesheet_export.ps1 -loc 3
Why is it reported as a success?
You need to set you $ErrorActionPreference to stop:
$ErrorActionPreference = "Stop"
By default this is set to Continue, so when Powershell is called from SQL Agent the job will continue on error unless you change the setting.
I have a few more tips on calling Powershell from SQL Agent here:
http://blogs.technet.com/b/heyscriptingguy/archive/2013/05/06/10-tips-for-the-sql-server-powershell-scripter.aspx
Is this your answer:
Why are my powershell exit codes always "0"?
a lot of things say using -file is the issue but it looks like you aren't