Remote Powershell, invoke-command and Variables

This topic contains 5 replies, has 2 voices, and was last updated by  nOrphf 4 years, 3 months ago.

  • Author
    Posts
  • #6683

    nOrphf
    Participant

     

    Hi

    I'm trying the following after I have startet a new  "Import-PSSession $Session"

    $sb = {(Get-MailboxStatistics -Identity $Mailbox.Alias).TotalItemSize.value.ToBytes()}
    [int]$MailboxSize = Invoke-Command -Session $session -ScriptBlock $sb

    But I get the error that Identity is empty.

    How can I pass the content of the variable with the invoke command?
    Or another way of putting it, what am I doing wrong?

    Regards Lars Mortensen

  • #6694

    Richard Siddaway
    Moderator

    use the argumentlist parameter on invoke-command

    $sess = New-PSSession -ComputerName $env:COMPUTERNAME
    Invoke-Command -Session $sess -ScriptBlock {param ($procname) Get-Process -Name $procname} -ArgumentList powerShell

  • #6695

    nOrphf
    Participant

    Hi

    Thank you for the answer, but I can't seam to make it work.

    My code is now:
    $sb = {param ($Mailbox) (Get-MailboxStatistics -Identity $Mailbox.Alias).TotalItemSize.value.ToBytes()}
    [int]$MailboxSize = Invoke-Command -Session $session -ScriptBlock $sb -ArgumentList powerShell

    Regards Lars.

  • #6696

    Richard Siddaway
    Moderator

    The example I gave used PowerShell as process name

    In your case you need to give a mailbox identifier – a name or an alias

  • #6969

    nOrphf
    Participant

    Hi

    Im not sure what you mean.

    I do supply a mailbox identifier: $Mailbox.Alias

    PS M:\SkyDriveBackup\SkyDrive\BackupScripts> echo $Mailbox.Alias
    DiscoverySearchMailbox{D919BA05-46A6-415f-80AD-7E09334BB852}

    PS M:\SkyDriveBackup\SkyDrive\BackupScripts> $sb = {param ($Mailbox) (Get-MailboxStatistics -Identity $Mailbox.Alias).TotalItemSize.value.ToBytes()}

    PS M:\SkyDriveBackup\SkyDrive\BackupScripts> [int]$MailboxSize = Invoke-Command -Session $session -ScriptBlock $sb -ArgumentList powerShell
    Cannot bind argument to parameter 'Identity' because it is null.     + CategoryInfo          : InvalidData: (:) [Get-MailboxStatistics], ParameterBindingValidationException     + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Get-MailboxStatistics     + PSComputerName        : 4twexbe1

    PS M:\SkyDriveBackup\SkyDrive\BackupScripts> echo  $MailboxSize
    0

  • #6970

    nOrphf
    Participant

    Found the error now, this Works:

    $sb = {param ($MailAlias) (Get-MailboxStatistics -Identity $($MailAlias)).TotalItemSize.value.ToBytes()}
    [int]$MailboxSize = Invoke-Command -Session $session -ScriptBlock $sb -ArgumentList $Mailbox.Alias

    Thanks

     

    Regards Lars

You must be logged in to reply to this topic.