我需要能够以不同的用户远程(通过Jenkins)运行PowerShell脚本。由于它将作为Jenkins工作执行,Get-Credential
不适合我。下面是我创建的脚本,但它根本不起作用。
$uname='domain\username'
$pwd='password'
$passw=Convertto-SecureString -String $pwd -AsPlainText -force
$mycred=New-object -TypeName System.Management.Automation.PSCredential -ArgumentList $uname, $passw
Invoke-Command -FilePath "C:\test_scripts\fetchquery.ps1" -Authentication default -Credential $mycred -computername localhost
答案 0 :(得分:0)
创建凭据对象:
$username = 'domain\username'
$Password = 'password' | ConvertTo-SecureString -Force -AsPlainText
$credential = New-Object System.Management.Automation.PsCredential($username, $Password)
在您的代码中,您在localhost上执行它,所以启动Powershell会话使用已保存的凭据:
Start-Process powershell -argumentlist '-executionpolicy','bypass','-file',"C:\test_scripts\fetchquery.ps1"' -Credential $credential
并远程运行脚本(不要使用Localhost)
Invoke-Command -Computername 'Computer' `
-FilePath C:\test_scripts\fetchquery.ps1 -ArgumentList PowerShell `
-Cred $Credential
答案 1 :(得分:0)
试试这个:
Start-Process powershell -WindowStyle 'Hidden' -ArgumentList '-executionpolicy', 'bypass', '-file', $scriptFullName -Credential $C