通过SQL Server代理作业执行时,我的PowerShell脚本无法创建PS会话, 另一方面,当我尝试从Powershell编辑器执行脚本时,它运行良好。
我试图将输出捕获到一个文本文件中,该文件表明脚本可以正常执行,直到-------
echo "Credentials received" >> C:\Users\username\Desktop\ABC.txt
#Set Execution policy for the first run
#Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
#This is better for scheduled jobs
$User = "xyz@abc.com"
$pass = "0100skdhfsdhlfsj7eb0100000044afdcf1458d41449af94347f8d9d962000012464664603660000c000000010000000cb11e38eb73dd5d612111f4461d953e90000000004800000a00000001000000085512ba758402a4c5f4504af6f407ba3180000003bf98f386b5341815358f54166f931802814a6154f86daf8140000009a3c5af0588baa420ed836dc0ac1d1f4d0fb4649"
$PWord = ConvertTo-SecureString -string $pass
$UserCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord
echo "Credentials received" >> C:\Users\username\Desktop\ABC.txt
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
echo "Session" >> C:\Users\username\Desktop\ABC.txt
Import-PSSession $Session -AllowClobber
echo "Session set up" >> C:\Users\username\Desktop\ABC.txt
Remove-PSSession -Id $Session.Id
exit
当我从Powershell编辑器运行同一段代码时,它正在正确执行。
从SQL Agent计划时,我期望它具有相同的行为。
答案 0 :(得分:0)
由于加密是特定于用户的,因此我们应该在sql代理作业中为使用密码进行加密的用户创建一个代理帐户。然后使用特定的代理运行作业。可以。
使用的加密技术:
$password = read-host -prompt "Enter Password"
write-host "$password is: "
$secure = ConvertTo-SecureString $password -force -asPlainText
$bytes = ConvertFrom-SecureString $secure
#Encrypted Password
echo $bytes
注意:-在此处输入代码上面提到的技术是用户和系统特定的。 请在运行密码的脚本所在的同一台计算机上对密码进行加密。