View Single Post
Old 01-29-2008   #10 (permalink)
Robin Moffatt


 
 

Re: start-psjob - user credentials

On Jan 29, 11:28 am, Shay Levi <n...@xxxxxx> wrote:
Quote:

> Sorry, for some reason I can only see one thread of this post, all chained
> threads are lost
>
here it is

I have a simple script that connects to a SQL server using integrated
security. If I run the script from the console, it works. If I run it
from start-psjob it fails, even if I pass -credential of my current
user (and that has access to the SQL server in question)

Running Profiler against the SQL server shows the interactive
execution of the script connecting to the server with the correct
domain ID of my current user, but when the job's run as start-psjob
(with or without -credential) it connects to the SQL server as NT
AUTHORITY\ANONYMOUS LOGON

1) To help with debugging, how can I find out what the authenticated
user of the current powershell context is?
2) How can I connect to a SQL server from a Start-PSJob-initiated
script if it won't run under my credential?

My scripts and output are below

Thanks,
moff.

connecttest.ps1:
--------------
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString =
"Server=sgb187;Database=buy;Integrated Security=SSPI"
$SqlConnection.open()
$sqlConnection.state
-------------

PSH C:\PS > .\connecttest.ps1
Open
PSH C:\PS > Start-PSJob "C:\ps\connecttest.ps1"

SessionId Name State HasMoreData
Command
--------- ---- ----- -----------
-------
43 Running True C:\ps
\connecttest.ps1

PSH C:\PS > get-psjob|receive-psjob
Receive-PSJob : Exception calling "Open" with "0" argument(s): "Login
failed for user 'NT AUTHORITY\ANONYMOUS LOGON'."
At line:1 char:24
+ get-psjob|receive-psjob <<<<
PSH C:\PS > Start-PSJob "C:\ps\connecttest.ps1" -credential (get-
credential)

cmdlet get-credential at command pipeline position 1
Supply values for the following parameters:
Credential

SessionId Name State HasMoreData
Command
--------- ---- ----- -----------
-------
45 Running True C:\ps
\connecttest.ps1

PSH C:\PS > get-psjob|receive-psjob
Receive-PSJob : Exception calling "Open" with "0" argument(s): "Login
failed for user 'NT AUTHORITY\ANONYMOUS LOGON'."
At line:1 char:24
+ get-psjob|receive-psjob <<<<
My System SpecsSystem Spec