我通过env
注入器将凭据传递给脚本(注意这对我来说适用于Invoke-Command
),并尝试运行Start-Job
,但jenkins不喜欢它:
$user = $ENV:user
$pass = $ENV:pass
write-output (cat env:username)
write-output (cat env:user)
write-output (cat env:pass)
$pass = $pass | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($user), $pass
Start-Job -Credential $cred -ScriptBlock {'test'}
write-output (get-job | Receive-Job)
get-job | remove-job
这是我得到的错误(确认用户名和密码是正确的,当我从控制台运行这个脚本时,它的工作原理相同)
Started by user ME
[EnvInject] - Loading node environment variables.
Building in workspace C:Program Files (x86)Jenkinsjobsmyjobworkspace
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:WindowsTEMPhudson1723222179976241861.ps1'"
MYJENKINSSRV$
correctdomaincorrectuser
correctPassword
Id Name PSJobTypeName State HasMoreData Location
-- ---- ------------- ----- ----------- --------
1 Job1 BackgroundJob Failed False localhost
[localhost] An error occurred while starting the background process. Error
reported: Access is denied.
+ CategoryInfo : OpenError: (localhost:String) [], PSRemotingTran
sportException
+ FullyQualifiedErrorId : -2147467259,PSSessionStateBroken
Finished: SUCCESS
我有时在使用PowerShell时遇到凭据问题,我通常可以通过使用这个来修复它:
$username = Username
$password = Password
$cred = New-Object -TypeName System.Management.Automation.PSCredential ($username, $password)
$Credentials = Get-Credential $cred
基本上是在Get-credentials中输入凭据,然后使用它作为凭据