Using environment variables from a powershell job step does not work

Hi, 

I've created this environment variable from Powershell:

$env:RDSInstanceName = "MyServer\myinstance"

I've create a job step to read from this variable:

$erroractionpreference = "Stop"

$DBName = 'MyDB'

$ServerName = Get-ChildItem Env:RDSInstanceName

$Srv = $ServerName.value 

$Srv

SL SQLSERVER:\SQL\"$Srv"\Databases\"$DBName"\Tables

I am getting an error message when running this job step:

Executed as user: ... A job step received an error at line 10 in a PowerShell script. The corresponding line is '$ServerName = Get-ChildItem Env:RDSInstanceName'. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Cannot find path 'RDSInstanceName' because it does not exist.  '.  Process Exit Code -1.  The step failed.

It works if I run this in the powershell window.
Any ideas?
Thanks

 

  • Your account and the job step are running as different users, so permissions are probably the issue. Are you targeting local instance of the SQL or remote one? 

    From MSDN: Because job steps run in the context of a specific Microsoft Windows user, that user must have the permissions and configuration necessary for the job step to execute. For example, if you create a job that requires a drive letter or a Universal Naming Convention (UNC) path, the job steps may run under your Windows user account while testing the tasks. However, the Windows user for the job step must also have the necessary permissions, drive letter configurations, or access to the required drive. Otherwise, the job step fails. To prevent this problem, ensure that the proxy for each job step has the necessary permissions for the task that the job step performs.

    msdn.microsoft.com/.../ms187056.aspx