How to run a global PowerShell script from action "Execute PowerShell Script"?

Hi,

I want to know how I can run a PowerShell script that I´ve created as a project global script in FinalBuilder by means of action Execute PowerShell Script?

This is the function I´ve created and that I want to call in the "Script" tab of the above mentioned action:

function Get-ComputerName {
   Get-Content Env:COMPUTERNAME
}

Unfortunately I did not find any answer to this question in the online help.
Can anybody help?

Kind regards,
Marcus

Just use the Run Script action, and call the function as you would any other function. You cannot call project global script functions from external scripts or the Execute Powershell script action.

This works, thank you. Maybe someone could make the online help more clearly / concisely on this topic.

Regards,
Marcus

One thing is still not working. If I want to remotely invoke a project global powershell script, I used the following code in the "Run Script" action on tab "OnExecute"

$DeploymentServer = $FBVariables.GetVariable("DeploymentServer")
Invoke-Command -computerName $DeploymentServer -scriptBlock { Get-ComputerName }

Get-ComputerName is a project global script containing this code block

function Get-ComputerName {
   Get-Content Env:COMPUTERNAME
}

Running this action result in the following error message

The term 'Get-ComputerName' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or
 if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Get-ComputerName:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

If I replace the call to the globally defined function by the function body, the action runs fine.

$DeploymentServer = $FBVariables.GetVariable("DeploymentServer")
Invoke-Command -computerName $DeploymentServer -scriptBlock { Get-Content Env:COMPUTERNAME }

Any ideas why functions placed within project global scripts could not be used in the scriptBlock brackets?

Regards,
Marcus

Hi Marcus

To get this to work in my case, I simply had to replace the curly braces with parentheses. i.e:
Invoke-Command -computerName $DeploymentServer -scriptBlock ( Get-ComputerName )

Could you please try this and confirm whether this works for you?

Regards,
Steve

Hi Stephen,

thank for answering. I tried your suggestion but unfortunately I receive an error message, too. However the error message is a different one:

Invoke-Command : Cannot bind parameter 'ScriptBlock'. Cannot convert the "BNW..." value of type "System.String" to type "System.Management.Automation.ScriptBlock".
At line:10 char:60
+ Invoke-Command -computerName $DeploymentServer -scriptBlock <<<<  ( Get-ComputerName )
    + CategoryInfo          : InvalidArgument: ( [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.InvokeCommandCommand

I must admit that I´m a novice to PowerShell, so I don´t know if the error message has something to do with FinalBuilder.

Kind regards,
Marcus

Hi Marcus

Sorry, I overlooked part of your previous post. The problem is here that the script block argument needs to contain script that can be executed on the target machine. In this case you are passing in a function that is inaccessible to the remote machine, which is causing it to fail.

If you change your command to the following it will work (removing the reference to the global function):
Invoke-Command -computerName $DeploymentServer -scriptBlock { Get-Content Env:COMPUTERNAME }

Global functions are only able to be used within the scope of FinalBuilder script events.

Hope this helps.

Regards,
Steve

Hi Stephen,

I was afraid that you would say that. As you can see from my previous post, this was what I have tried before.
But anyway, thank very much for your help.

Kind regards,
Marcus

Hi Marcus

You can easily work around this by changing your global function to return a ScriptBlock object. So to this you would change your global script to be:


function Get-ComputerName {

   $scriptBlock = [ScriptBlock]::Create("Get-Content Env:COMPUTERNAME")

   return $scriptBlock

}


Then change you Invoke-Command call to be the following:


Invoke-Command -computerName $DeploymentServer -scriptBlock (Get-ComputerName)

This way you are passing section of script to be executed on the remote machine.

Regards,
Steve

Hi Stephen,

works like a charm. Again thank you very much for your help!

Kind regards,
Marcus