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.
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

invoke-command behavior

Post 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!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: invoke-command behavior

Post by jvierra »

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

Invoke-Command -ComputerName $env:COMPUTERNAME -ScriptBlock {Get-BitlockerVolume -MountPoint C:}
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: invoke-command behavior

Post 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!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: invoke-command behavior

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: invoke-command behavior

Post 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]
This topic is 5 years and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked