Let’s take a look at the following script named md.ps1:
1 2 3 4 5 6 7 8 9 10 11 12 13 | param( [System.String] $ComputerName , [System.String] $Configuration , [System.String] $BuildDefinition ) $session = New -PSSession -ComputerName $ComputerName Invoke -Command -Session $session -ScriptBlock { $latestbuildfolder = Get-ChildItem "C:\procmon\procmonBuilds\$BuildDefinition" | Sort-Object LastWriteTime -Descending | SELECT-Object -First 1 $deploycmdlocation = "\procmon\procmonWebProject\$Configuration\_PublishedWebsites\procmonWebProject_Package\procmonWebProject.zip" $finalstring = $latestbuildfolder .FullName + $deploycmdlocation $finalstring } Remove -PSSession $session |
When I run using below syntax it will not work as expected on a remote computer.
PS> .\md.ps1 “computer1” “Release” “ReleaseBuildConfig”
It will replace $BuildDefinition variable but then it will not list all its child folders. Further more it will not replace $configuration variable with the value, for example, “Release” or “Debug”. As soon as I hard coded them the rest of the command worked just fine. Ok so what was the issue.
It turns out that when you are trying to pass parameters to a script and then invoke a remote powershell session and access variables inside that PSsession then you have to declare them twice in your scripts. Yes Twice. Check the final script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | param( [System.String] $ComputerName , [System.String] $Config , [System.String] $BuildDef ) $session = New -PSSession -ComputerName $ComputerName Invoke -Command -Session $session -ScriptBlock { param( [string] $ComputerName , [string] $Configuration , [string] $BuildDefinition ) $latestbuildfolder = Get-ChildItem "C:\procmon\procmonBuilds\$BuildDefinition" | Sort-Object LastWriteTime -Descending | SELECT-Object -First 1 $deploycmdlocation = "\procmon\procmonWebProject\$Configuration\_PublishedWebsites\procmonWebProject_Package\procmonWebProject.zip" $finalstring = $latestbuildfolder .FullName + $deploycmdlocation $finalstring } -args $ComputerName , $Config , $BuildDef Remove -PSSession $session |
It was painful to figure it out and my co-worker friend helped me figure it out.
No comments:
Post a Comment