Passing a [ref] parameter in a remote session in Powershell - function

I have a Powershell question.
I am trying to get a value from a function to a variable, by calling a function with a reference to the variable.
For example:
$var = New.Object System.Object;
Example-Function -OutObject ([ref]$var);
Where the Example-Function is defined like this:
function Example-Function
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true)]
[ref]
$OutObject
)
$SomeValue = ...
#Write some output
#Do something...
$OutObject.Value = $SomeValue;
}
This is working OK. The $var variable gets it's value from the function ($SomeValue).
But, this is not working when the Example-Function is imported into remote session, for example:
$creds = New-Object System.Management.Automation.PSCredential('user','pass')
$session = New-PSSession -ComputerName 'ExampleComputer' -Credential $creds -Authentication CredSSP
Import-PSSession -Session $session -CommandName 'Example-Function' -AllowClobber
$var = New.Object System.Object;
Example-Function -OutObject ([ref]$var);
This code is throwing the following error: Cannot process argument transformation on parameter 'OutObject'. Reference type is expected in argument.
I am assuming that this is becuase the Example-Function is now running on the other computer ('ExampleComputer'), while ([ref]$var) is referencing the variable in memory of the computer running the scripts (my computer).
The reason I don't want to (cannot) use the return statement way is becuase my function is writing some output, and in Powershell, everything that is outputed from a function is returned.
So, my question is, can I get a value from a function that has a lot of output into the variable, when the function is running in the remote session?
If it cannot be done by using the [ref] parameter, is there another way?
Thanks

Okay lets try again:
Invoke-Command returns whatever is run in the remote pipeline. Which means you can do:
$var = Invoke-Command -session $session -command {Example-Function}
Which saves everything in the $var variable. You can then filter the results and get whatever information you need.
And please remember [ref] just makes everything more complicated than it actually is.

Related

variable in function executed by invoke-command not visible

Can someone please help me get the below code to work?
$ab = "1"
function test {
$script:ab = "c"
}
invoke-command -ComputerName localhost ${function:test}
$ab
After running the above function by invoke-command I want to see value "c" for $ab
Note: ${function:test} is an unusual instance of PowerShell's namespace notation and is equivalent to
(Get-Item function:test).ScriptBlock; i.e., it references the body of function test, as a script block.
When you use the -ComputerName parameter, Invoke-Command uses remoting to execute the specified script block - even if the target computer is the same machine (localhost or .).
Remotely executed code runs in a different process and has no access to the caller's variables.
Therefore:
If local execution is the goal, simply omit the -ComputerName argument; then again, in that case you could simply run . ${function:test} or even just test:
$ab = "1"
function test { $script:ab = "c" }
test # shorter equivalent of: Invoke-Command ${function:test}
For remote execution, output the desired new value from the remotely executed script block and assign it to $ab in the caller's scope:
$ab = "1"
function test { "c" } # Note: "c" by itself implicitly *outputs* (returns) "c"
$ab = Invoke-Command -ComputerName localhost ${function:test}

How to call function outside a scriptblock

I have a function that I should be able to call from any place in my powershell script.
The problem is that it doesn't identify the function in a script block.
In the following example I have the function getNumberFive which should return the number 5.
I want to be able to use this function inside a scriptblock when I start a new job and also in the end of the script.
Expected result:
Write the number 15 to the file "C:\tmp\result.txt"
Write to the console: "I like the number 5"
In reality:
Write the number 10 to the file "C:\tmp\result.txt"
Write to the console: "I like the number 5"
I can workaround this issue by defining the same function inside the scriptblock but then I will duplicate the function and this is not a good programming.
Another way is to define:
$func = {
function getNumberFive(){
return 5
}
}
$scriptBlock = {
Function printSum(){
$number = getNumberFive
$newNumber = 10 + $number # => $newNumber should be 15
$newNumber >> "C:\tmp\result.txt"
}
}
Start-Job -ScriptBlock $scriptBlock -InitializationScript $func
But in this case I won't be able to call $five = getNumberFive.
I read number of methods but I didn't understand how exactly to use them:
CALLING A POWERSHELL FUNCTION IN A START-JOB SCRIPT BLOCK WHEN IT’S DEFINED IN THE SAME SCRIPT
How to pass a named function as a parameter (scriptblock)
https://social.technet.microsoft.com/Forums/ie/en-US/485df2df-1577-4770-9db9-a9c5627dd04a/how-to-pass-a-function-to-a-scriptblock?forum=winserverpowershell
PowerShell: Pass function as a parameter
Using Invoke-Command -ScriptBlock on a function with arguments
My script:
function getNumberFive(){
return 5
}
$scriptBlock = {
Function printSum(){
# $number = getNumberFive => DOESN'T WORK
# $number = Invoke-Expression ($(get-command getNumberFive) | Select -ExpandProperty Definition) => DOESN'T WORK AS EXPECTED
# $number = &(${function:getNumberFive}) => DOESN'T WORK AS EXPECTED
# $number = &(Get-Item function:getNumberFive) => DOESN'T WORK AS EXPECTED
$newNumber = 10 + $number # => $newNumber should be 15
$newNumber >> "C:\tmp\result.txt"
}
printSum
}
Start-Job -ScriptBlock $scriptBlock
$five = getNumberFive
Write-Host "I like the number"$five
Get-Job | Wait-Job
Get-Job | Stop-Job
Get-Job | Remove-Job
When you pass a scriptblock to start-job (or invoke-expression) the PowerShell instance that executes that scriptblock only has access to that scriptblock, anything that script block loads, and anything that already exists in the PowerShell instance.
Other parts of your script are not included. (For functions locally defined in your script to be available from other, possibly remote, instances of PowerShell the whole script – not just the scriptblock – and any dependencies would need to be accessible from the other instance.)
You could refactor the code you want in both places into a module which the script block loads as well as the job creating script.
When using jobs you are executing code in another process: like any remote operation the remote executing is a separate environment.
You can do like this:
function getNumberFive(){
return 5
}
$a={
Function printSum($number){
$newNumber = 10 + $number # => $newNumber should be 15
$newNumber >> "D:\result.txt"
}
}
$scriptBlock = {
param($number)
printSum -number $number
}
$five = getNumberFive
Write-Host "I like the number"$five
Start-Job -InitializationScript $a -ScriptBlock $scriptBlock -ArgumentList $five
There is a switch name -InitializationScript in Start-job where you can keep your initialization code like a function. Wrap it in a scriptblock and simply call it from the main scriptblock.
Hope it helps.

How to pass arguments to functions in PowerShell

I have the below PowerShell script
Function Publish
{
Param(
[parameter(Mandatory=$true)]
[String]
$RELEASEDIR,
[parameter(Mandatory=$true)]
[String]
$SERVICENAME,
[parameter(Mandatory=$true)]
[String]
$SERVER
)
Get-ChildItem "$RELEASEDIR\*"
$service = Get-Service -Name $SERVICENAME -Computername $SERVER -ErrorAction SilentlyContinue
$service.Status
}
Publish
How I am executing this:
PS C:\Release\RPCPS> .\RPCPublish.ps1 -RELEASEDIR "C:\Location" -SERVICENAME "value" -SERVER "server"
cmdlet Publish at command pipeline position 1
Supply values for the following parameters:
RELEASEDIR:
Even after passing arguments while executing, the script is expecting it again. What am I doing wrong here?
If you want to execute the script by calling the .ps1 as in your example, there is no need to use a function. Your script should look just like this:
Param(
[parameter(Mandatory=$true)]
[String]
$RELEASEDIR,
[parameter(Mandatory=$true)]
[String]
$SERVICENAME,
[parameter(Mandatory=$true)]
[String]
$SERVER
)
Get-ChildItem "$RELEASEDIR\*"
$service = Get-Service -Name $SERVICENAME -Computername $SERVER -ErrorAction SilentlyContinue
$service.Status
The parameters are passed directly to the script and can be used there.
If, on the other hand, you want to establish a (reusable) function, remove just the last line from your script, which calls the function without parameters (which is why it asks for the mandatory parameters every time).
If you remove the last line, you can call the script without parameters once. After that you have a new function Publish in your current session, which you can then call with
Publish -RELEASEDIR "C:\Release\Batchfile" -SERVICENAME "AmazonSSMAgent" -SERVER "10.0.1.91"
independent of the script file.
Your script is creating a function, "Publish", (lines 1-17) and then calling it with no parameters (line 18). Since you've defined parameters as mandatory (lines 4, 7, 10), failing to supply the parameters when you call the function (line 18) causes PowerShell to request values for the unsupplied parameters.
Supplying parameters to the script file itself does not help; there is no mechanism for "automagically" passing those parameters to anything within the script (you would have to explicitly code the script for that).
As Matt suggested in the comments, dot-source your script after deleting line 18, and then call your function explicitly, passing the parameters (publish -RELEASEDIR "C:\Release\Batchfile" -SERVICENAME "AmazonSSMAgent" -SERVER "10.0.1.91").
As per my understanding your requirement is to run the function, and you have to compile the scripts also in Jenkins.
You can do something like this:
Let's say your script name is RPCPublish.ps1 and the path is D:\Folder.
And I can see your function name is Publish.
So in your case,
powershell -command "& { D:\folder\RPCPublish.ps1; Publish }"
You can pass the parameters after this in the script block.
I used a PowerShell plugin (PowerShell) and executed the same.
. "C:\Release\RPCPS\RPCPublish.ps1"
FUunctionName -RELEASEDIR "C:\bin\Release" -SERVICENAME "Service" -SERVER "$env:SERVER" -DISPLAYNAME "Services Air" -BINPATH "D:\Build\Project.exe" -DESCRIPTION "This service hosts Air service" -DESTINATION "d$\Build\"

powershell add objects via -outvariable +variable in a function

I am running in an imported session window. Not sure if that matters.
I am trying to add a few variable values to an array in a function.
$Session = New-PsSession -ConfigurationName Microsoft.Exchange -ConnectionUri "http://emailserver/powershell/" -Authentication Kerberos
Import-PsSession $Session -allowclobber
Add-PsSnapin Microsoft.SharePoint.PowerShell
$group1 = "Accounting"
$group2 = "HR"
function AddUsersToGroups {
Write-output $Group1 -outvariable +Adgroups
Write-output $Group2 -outvariable +Adgroups
}
When I highlight and run the lines from a Powershell ISE they work fine and create the Variable $AdGroups and the combined data is in there.
But when I run the function all I see is the output and no variable gets created.
PS C:\Windows\system32> AddUserToGroups
Group1
Group2
Kinda Stumped. I tried to create a variable $AdGroups = #() as the first line but it fails also.
Not sure exactly what you are trying to achieve, but the AdGroups variable will be available only in the scope of the AddUsersToGroups function.
One way of solving this would be add the script: scope, like below:
Function AddUsersToGroups{
Write-output $Group1 -outvariable +script:Adgroups
Write-output $Group2 -outvariable +script:Adgroups
}
Now you should be able to access $Adgroups outside after the function is called.

PowerShell function return type not as expected

I have a script that accepts a string parameter :
script-that-takes-string-param.ps1
param(
[Parameter(Mandatory=$true, HelpMessage="path")]
[string]$path,
)
And I have another script that calls the first script :
parent-script.ps1
function CreateDir($dir) {
if (!(Test-Path $dir)) {
mkdir $dir
}
}
function CreatePath($BaseDir, $Environment, $Site, $Domain){
$path = [string]::format("{0}{1}\{2}\{3}", $BaseDir, $Environment, $Site, $Domain)
CreateDir $path
$path
}
$path = CreatePath 'c:\web\' 'qa' 'site1' 'com'
.\script-that-takes-string-param.ps1 -path $path
Running this script throws the exception :
"Cannot process argument transformation on parameter 'path'. Cannot convert value to type System.String"
Casting the parameter doesn't work :
.\script-that-takes-string-param.ps1 -path [string] $path
And casting the function result doesn't work either :
$path = [string] CreatePath 'global' 'site1'
But what is really strange is that if I run parent-script.ps1 twice from the PS command line, the 1st time it throws exceptions, but the 2nd time it executes with no errors.
My best guess would be that your
#do some other stuff with $path
writes something to the standard output, causing the function to return an array that contains said output and the path you expect. Can you send details on what you do in that bit?
Try removing "return". Output that is not saved to a variable is automatically returned. It shouldn't do any difference but it won't hurt to try.
Can you provide a full exception? Without seing the complete exception I get the feeling that the error is caused by something inside your script(ex. a function).
EDIT Your mkdir is causing the problem. When you run it, it returns an object representing the created directory(a DirectoryInfo object if I remember correctly). To fix this, try:
function CreateDir($dir) {
if (!(Test-Path $dir)) {
mkdir $dir | out-null
}
}
or combine them like:
function CreatePath($BaseDir, $Environment, $Site, $Domain){
$path = [string]::format("{0}{1}\{2}\{3}", $BaseDir, $Environment, $Site, $Domain)
if(!(Test-Path $path -PathType Container)) {
New-Item $path -ItemType Directory | Out-Null
}
$path
}