【问题标题】:Powershell User Issue Through SQL Server Agent通过 SQL Server 代理的 Powershell 用户问题
【发布时间】:2014-01-07 10:40:05
【问题描述】:

我目前正在尝试通过SQL Server Agent 运行Powershell 脚本,如果我在桌面上通过Powershell ISE 运行它,它会毫无错误地完成其任务。简单脚本如下(仅用于测试):

$test = "G:\test.txt"

if (Test-Path $testFile)
{
    Remove-Item $test
}

当我通过 SQL Server 代理运行它时,它会产生一个成功的输出 - 没有任何错误,但确实表明它在作业历史日志中以不同的用户身份运行,例如 domain\localmachine,而当我运行通过 Powershell ISE 编写脚本,它显示domain\you

作为说明,我无法手动确认这一点,因为我尝试在本地和通过 SQL Server 代理在作业中运行以下脚本以查看输出,但作业失败(因此我怀疑这是用户问题)。因此,我信任 SQL Server 代理,因为 domain\locallmachine 正在运行作业(它不会删除文件的原因)。

([Environment]::UserDomainName + "\" + [Environment]::UserName) | out-file pssaved.txt
"$env:userdomain\$env:username" | out-file -append pssaved.txt
[Security.Principal.WindowsIdentity]::GetCurrent().Name | out-file -append pssaved.txt

## Locally this produces domain\you
## On SQL Server Agent, I receive the error: The error information returned by PowerShell is: 'SQL Server PowerShell provider error: Path SQLSERVER:\pssaved.txt does not exist. Please specify a valid path.' 

有没有办法通过 SQL Server 代理以我的域用户身份运行作业,例如 domain\you 而不是 domain\localmachine(至少,这会消除这种错误的可能性)?

【问题讨论】:

  • 您可以在 remove-item 上使用 -Credential 参数,但这会涉及在过程脚本中存储一组凭据。
  • 这里有错字吗?错误文本显示SQLSERVER:\psaved.txt,但您的脚本指定pssaved.txt
  • @KeithHill 是的,我的错。

标签: sql-server powershell sql-server-2012


【解决方案1】:

您可以为此使用代理。 Check it out.

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 2017-07-05
  • 2020-02-25
  • 1970-01-01
相关资源
最近更新 更多