Remote Powershell, invoke-command and Variables

 

Hi

I’m trying the following after I have startet a new “Import-PSSession $Session”
<pre class=“lang:ps decode:true”>$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

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

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.

The example I gave used PowerShell as process name

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

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: (:slight_smile: [Get-MailboxStatistics], ParameterBindingValidationException    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Get-MailboxStatistics    + PSComputerName       : 4twexbe1

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

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