简体   繁体   中英

How to run powershell script using default credential from sql job agent

I am downloading a file from sharepoint. I have already scheduled the job in SQL job agent Its working fine when i use the following code

 $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()

But the problem here i face is whenever i update my password i need to update the secure string as well

So i wanted to use the default credentials

so i used the following script

$webclient = New-Object System.Net.WebClient

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

but its getting failed with the following error

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

went through different blogs all were suggesting the same approach which i have followed Any help in this regard?

As far as I understand default credentials users the account and password the sql agent process is started and since it will not match the SharePoint online account it will fail. It would be easier if you create a powershell that updates all secure strings once password is changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM