1
votes

I am using a SQL Server job to get some wmiobject details into a table. This code runs with default installation, but fails when I execute it on a named instance. So far I have seen only one difference in connection, for the named instance, SQL Agent is using with a user name which has "$" as part of the name (i.e. NT Service\SQLAgent$instance)

Is there anyway I can overcome this? Changing the agent account is not an option as most of the servers run with this account and my code needs to work with all accounts.

    $conn = New-Object System.Data.SqlClient.SqlConnection("Data Source=$SqlSvr;Initial Catalog=$Database; Integrated Security=SSPI") 

$conn.Open()

Executed as user: NT Service\SQLAgent$instance. A job step received an error at line 21 in a PowerShell script. The corresponding line is ' $conn.Open() '... The error information returned by PowerShell is: 'Exception calling "Open" with "0" argument(s): "Login failed for user '(DOMAIN)(COMPUTER)$'." '. Process Exit Code -1.

I tried with "identity impersonate =true" but it didn't accept the command.

Any ideas?

1
You say this is a SQL Agent Job. Is it executing SQL PowerShell or Operating System(CmdExec)? Why not create a Proxy account and use it in the Job\Step\RunAs?Woody

1 Answers

0
votes

Ether use ' instead of " or use the escape Char ` or use char(36)

"blahblah`$blahblah"

'blahblah$blahblah'

"blahblah$([char](36))blahblah"