invoke-command behavior

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
Domtar
Posts: 63
Joined: Sun Mar 03, 2013 12:42 pm

invoke-command behavior

Post by Domtar » Fri Nov 02, 2018 11:18 am

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!

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: invoke-command behavior

Post by jvierra » Fri Nov 02, 2018 2:06 pm

You don't need a function. Just use this:

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

User avatar
Domtar
Posts: 63
Joined: Sun Mar 03, 2013 12:42 pm

Re: invoke-command behavior

Post by Domtar » Mon Nov 05, 2018 6:19 am

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!

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: invoke-command behavior

Post by jvierra » Mon Nov 05, 2018 2:12 pm

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.

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: invoke-command behavior

Post by jvierra » Mon Nov 05, 2018 5:13 pm

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]

Locked