0

我需要能够以不同的用户身份远程(通过 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
4

2 回答 2

2

创建凭证对象:

$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 

并远程运行脚本(不要使用本地主机)

Invoke-Command -Computername 'Computer' `
-FilePath C:\test_scripts\fetchquery.ps1 -ArgumentList PowerShell `
-Cred $Credential
于 2015-09-09T09:02:16.073 回答
-1

尝试这个:

Start-Process powershell -WindowStyle 'Hidden' -ArgumentList '-executionpolicy', 'bypass', '-file', $scriptFullName -Credential $C
于 2016-08-16T17:17:55.323 回答