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

 
Participant
2 days, 4 hours ago.

  • Author
    Posts
  • #128643

    Participant
    Points: 11
    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
    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

    Participant
    Points: 809
    Helping Hand
    Rank: Major Contributor

    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.

You must be logged in to reply to this topic.