powershell - 如何使用来自 sql 作业代理的默认凭据运行 powershell 脚本

标签 powershell sharepoint sharepoint-2010 powershell-2.0 sql-job

我正在从共享点下载文件。 我已经在 SQL 作业代理中安排了作业 当我使用以下代码时它工作正常

 $UserName = "xxxx"

$PswdPath = "D:\securestring.txt"
$SecurePassword = cat $PswdPath| convertto-securestring
$fileName = [System.IO.Path]::GetFileName($FileUrl)
$DownloadPath = "D:\Excel\"
$downloadFilePath = [System.IO.Path]::Combine($DownloadPath,$fileName)

$client = New-Object System.Net.WebClient 
$client.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$client.DownloadFile($FileUrl, $downloadFilePath)
$client.Dispose()

但我面临的问题是,每当我更新密码时,我也需要更新安全字符串

所以我想使用默认凭证

所以我使用了下面的脚本

$webclient = New-Object System.Net.WebClient

$webclient.UseDefaultCredentials = $true
$webclient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$webclient.DownloadFile($FileUrl, $DownloadPath)

但它因以下错误而失败

Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (401) Unauthorized."

浏览了不同的博客,都提出了我所遵循的相同方法 在这方面有什么帮助吗?

最佳答案

据我所知,默认凭据用户帐户和密码启动了 sql 代理进程,由于它与 SharePoint 在线帐户不匹配,因此它将失败。如果您创建一个 powershell 在密码更改后更新所有安全字符串,那就更容易了。

关于powershell - 如何使用来自 sql 作业代理的默认凭据运行 powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084643/

相关文章:

powershell - 无法复制元数据

powershell - 在PowerShell中使用Windows事件日志

javascript - 使用js代码在SharePoint中隐藏页面标题

C# SharePoint Online 远程身份验证

c# - 如何使用客户端对象模型授予用户对某些文件夹的权限?

sharepoint - 更改 AssemblyVersion 后的 Webpart "Not registered as safe"

Powershell WebClient 放弃遇到 SNI 错误

string - PowerShell 中 "System.Version"的版本字符串太长(或太短)

powershell - 使用 PowerShell 在 Windows 机器上设置 NTP 服务器

c# - 如何将 SPFieldNumber 转换为 int