Page 1 of 1

invoke-command behavior

Posted: Fri Nov 02, 2018 11:18 am
by Domtar
hi all,

I use a scriptblock to connect to a computer, get some info and store that info into a variable, some basic stuff

Code: Select all

function Get-BitlockerEncryptionStatus ($DeviceName)
{
	$scriptBlock = {
		Get-BitlockerVolume -MountPoint "C:"
	}
	$Script:BitlockerInfos = Invoke-Command -ComputerName $DeviceName -ScriptBlock $scriptBlock
	
	return $BitlockerInfos
	
}

Get-BitlockerEncryptionStatus ($env:COMPUTERNAME)
if I run this small script, it returns the value I need and displays it to the screen, like the following;

ComputerName: xxxxxxx

VolumeType Mount CapacityGB VolumeStatus Encryption KeyProtector AutoUnlock Protection
Point Percentage Enabled Status
---------- ----- ---------- ------------ ---------- ------------ ---------- ----------
OperatingSystem C: 237.69 FullyDecrypted 0 {} Off



now, I insert this same function into a PSF file and call it like this;

$FF = Get-BitlockerEncryptionStatus $ComputerName

but the FF variable contains only C:, which is my MountPoint.

I honestly do not understand why the function's behavior differs when called as is or into some GUI we have.

Any hint/advise/idea that could help?

Thanks!

Re: invoke-command behavior

Posted: Fri Nov 02, 2018 2:06 pm
by jvierra
You don't need a function. Just use this:

Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock {Get-BitlockerVolume -MountPoint C:}

Re: invoke-command behavior

Posted: Mon Nov 05, 2018 6:19 am
by Domtar
Same result.

I honestly cannot understand why my variable returnedValue doesn't contain what should be returned by the Invoke-Command cmdlet. All I see when I display it is: C:

$returnedValue = Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock {Get-BitlockerVolume -MountPoint C:}

I believe this happens because what is returned is "deserialized". If so, how can I manage this? My PS skills aren't that sharp yet.

thanks!

Re: invoke-command behavior

Posted: Mon Nov 05, 2018 2:12 pm
by jvierra
Did you use my example. Your original code has issues of syntax and design.

When calling a function it is not necessary to add parens around an argument.
The use of "return" in a function is not necessary in PowerShell.
THe code you are trying to build does not require a separate function. Just call the Invoke and use the results.

Re: invoke-command behavior

Posted: Mon Nov 05, 2018 5:13 pm
by jvierra
Works fine for me on any system

Code: Select all

PS D:\scripts> Invoke-Command -ScriptBlock {Get-BitlockerVolume  c:} -ComputerName alpha -

   ComputerName: ALPHA

VolumeType      Mount CapacityGB VolumeStatus           Encryption KeyProtector              AutoUnlock Protection PSComputerName
                Point                                   Percentage                           Enabled    Status
----------      ----- ---------- ------------           ---------- ------------              ---------- ---------- --------------
OperatingSystem C:        237.24 FullyDecrypted         0          {}                                   Off        alpha


PS D:\scripts>
[\code]