我需要能够以不同的用户身份远程运行PowerShell脚本(通过Jenkins)。因为它将作为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
发布于 2015-09-09 17:02:16
创建凭据对象:
$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
发布于 2016-08-17 01:17:55
试试这个:
Start-Process powershell -WindowStyle 'Hidden' -ArgumentList '-executionpolicy', 'bypass', '-file', $scriptFullName -Credential $C
https://stackoverflow.com/questions/32470348
复制相似问题