Q. I'm trying to use methods of an object within a workflow but they are not working. Why?
A. Typically objects have a number of methods available that are accessed as <object>.<method>, e.g. $string.ToUpper(). If you try to use this within a workflow you will receive an error that method invocation is not supported in a workflow. This is because objects in a workflow are deserialized and therefore lose their methods. The solution is to perform any object methods within an InlineScript block. An example would be:
Workflow RestrictionCheck
{
$msgtest = "Hello"
$msgtest = InlineScript {($using:msgtest).ToUpper()}
Write-Output $msgtest
}
RestrictionCheck
Note that to access any variables within the InlineScript they must be prefixed with $using: since the InlineScript actually creates a separate PowerShell process, runs the code and returns the objects and therefore ordinarily would not have access to the objects in the parent PowerShell process.
Each week, John Savill answers all of your toughest tech questions about the worlds of Windows Server, Azure, and beyond. Read his past IT advice here, and email your questions to john@savilltech.com.