Return value from Invoke-Command

Welcome Forums General PowerShell Q&A Return value from Invoke-Command

This topic contains 1 reply, has 2 voices, and was last updated by

Senior Moderator
8 months, 3 weeks ago.

  • Author
  • #128643

    Topics: 1
    Replies: 1
    Points: 12
    Rank: Member

    I have tried everything I know, and everything the internet has told me to do, including a post on this page.  I cannot seem to get a return value from Invoke-Command.

    #$ADComputers = Get-ADComputer -Filter 'Name -notlike "HCI*"' 
    $ADComputers = Get-ADComputer -Filter 'Name -like "test*"'
    ForEach ($Computer in $ADComputers) {
    $ComputerName = $Computer.Name
    Write-Host "Pinging $ComputerName"
    If (Test-Connection -ComputerName $ComputerName -Count 2 -Quiet) {
    Write-Host "$ComputerName is online" -ForegroundColor Green
    $CountDisks = {
    #[int]$DiskCount = (Get-BitLockerVolume | Where-Object {$_.EncryptionPercentage -gt 0}).Count
    #Write-Output $DiskCount
    #$DiskCount = 1
    Write-Output 1
    $GetKeys = {
    ForEach ($MountPoint in (Get-BitLockerVolume | Where-Object {$_.EncryptionPercentage -gt 0})) {
    (Get-BitLockerVolume -MountPoint $MountPoint).KeyProtector | Where-Object {$_.KeyProtectorType -eq 'RecoveryPassword'} | Select-Object @{Name='ComputerName'; Expression = {$env:COMPUTERNAME}}, KeyProtectorID, RecoveryPassword 
    $DiskCount = Invoke-Command -ComputerName $ComputerName -ScriptBlock $CountDisks #{(Get-BitLockerVolume | Where-Object {$_.EncryptionPercentage -gt 0}).Count}
    If ($DiskCount -gt 0) {
    $RecoveryKeys = Invoke-Command -ComputerName $ComputerName -ScriptBlock $GetKeys
    Else {
    Write-Host "$ComputerName has no encrypted disks."
    Else {
    Write-Host "$ComputerName is offline" -ForegroundColor Yellow

    I WAS trying to get a count of encrypted disks, but it devolved into just trying to get Invoke-Command to return ANYTHING.

  • #128647

    Senior Moderator
    Topics: 8
    Replies: 1009
    Points: 3,281
    Helping Hand
    Rank: Community Hero

    There are two Invoke-Commands, which one doesn't return value ?
    It seems its the second one for me, the second one will return a value only if the expression inside foreach return value.

The topic ‘Return value from Invoke-Command’ is closed to new replies.