我有一个詹金斯(Jenkins)职位,该职位的升职可以运行Powershell脚本。
升级过程操作是Windows PowerShell:
& "\\myserver\mypath\$ENV:BUILD_NUMber\\deployStuff.ps1" -environment "Dev"
脚本的开头:
param (
[Parameter(Mandatory=$true)][string]$Environment
)
Write-Host "Deploy - Environment = $Environment"
$root = (Get-Item $PSScriptRoot)
Write-Host "root = $root"
没有什么非常令人兴奋的。除了当我选中“限制此升级过程的运行位置”并指定我的一个从属设备之外,它可以工作,并且如果我取消选中它,或者指定我在主服务器上运行它,则会得到异常:
Started by user jdege
Building on master in workspace E:\jenkins_home\jobs\Promote Development Branch\workspace
Promoting Promote Development Branch #18
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\KORCOM~1\AppData\Local\Temp\hudson2695983892011285716.ps1'"
Deploy - Environment = Dev
Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At \\myserver\mypath\$ENV:BUILD_NUMber\\deployStuff.ps1:6 char:18
+ $root = (Get-Item <<<< $PSScriptRoot)
+ CategoryInfo : InvalidData: (:) [Get-Item],ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,microsoft.PowerShell.
Commands.GetItemCommand
这为什么在从属设备上起作用,而在主设备上却不起作用?
Mathias建议我尝试$ Myinvocation.PSScriptRoot。
我更改的脚本:
param (
[Parameter(Mandatory=$true)][string]$Environment
)
$root = Get-Item $PSScriptRoot
Write-Host "root - $root"
$root2 = Get-Item $Myinvocation.PSScriptRoot
Write-Host "root2 - $root2"
我的输出:
Started by user jdege
Building on master in workspace E:\jenkins_home\jobs\Promote Development Branch\workspace
Promoting Promote Development Branch #25
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\KORCOM~1\AppData\Local\Temp\hudson1494277122445286804.ps1'"
try PSScriptRoot
Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At \\ch-vmfile1\PublishedBuilds\kt_everything\development\8\deploy_bats\deploy_bats\deployMigrator.
ps1:6 char:17
+ $root = Get-Item <<<< $PSScriptRoot
+ CategoryInfo : InvalidData: (:) [Get-Item],microsoft.PowerShell.
Commands.GetItemCommand
root -
try Myinvocation.PSScriptRoot
root2 -
Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At \\ch-vmfile1\PublishedBuilds\kt_everything\development\8\deploy_bats\deploy_bats\deployMigrator.
ps1:18 char:20
+ $parent = (Get-Item <<<< $PSScriptRoot).parent.parent.FullName
+ CategoryInfo : InvalidData: (:) [Get-Item],microsoft.PowerShell.
Commands.GetItemCommand
Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At \\ch-vmfile1\PublishedBuilds\kt_everything\development\8\deploy_bats\deploy_bats\deployMigrator.
ps1:33 char:29
+ . (Join-Path -Path (Get-Item <<<< $PSScriptRoot) -ChildPath deployFunctions.ps1)
+ CategoryInfo : InvalidData: (:) [Get-Item],microsoft.PowerShell.
Commands.GetItemCommand
换句话说,我看到的与$ PSScriptRoot相同的$ Myinvocation.PSScriptRoot错误。