Error with Powershell remote Job

Hi All,
I am trying to execute a remote PS session to an exchange 2010 Server.

$session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$($script:ConfigFile.Settings.ExchangeServerSettings.ServerName)/Powershell
$job = Invoke-Command -Session $session -ScriptBlock {
$Databases = Get-MailboxDatabase | Where-Object { $_.Name -notlike "Archive*" }
$DBCount = $Databases.Count
$SelectDatbase = Get-Random -Minimum 1 -Maximum $DBCount
enable-mailbox -Identity args[0] -Database ($Databases[$SelectDatbase]) -force
} -ArgumentList $txtUserName.Text -AsJob

however once i execute the code i get the following errors. any help would be appreciated

The term 'Where-Object' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Where-Object:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : petmulser009.peters.local

The term 'Get-Random' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
    + CategoryInfo          : ObjectNotFound: (Get-Random:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
    + PSComputerName        : petmulser009.peters.local

Cannot index into a null array.
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
    + PSComputerName        : petmulser009.peters.local

TIA

I haven’t seen the issue myself but the following may be relevant:

https://blogs.msdn.microsoft.com/dvespa/2011/07/20/where-or-where-has-my-where-object-gone/

Others might have more hands on experience with this, but this is a start.

Thanks @John Carney. Will have a read through. its definitely the same issue I am facing. in the interim i have created a function as below

Function New-ExchangeMailbox
{
	Param
	(
		[String]$MailboxUser,
		[String]$DC
	)
	add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
	$Databases = Get-MailboxDatabase | Where-Object { $_.Name -notlike "Archive*" }
	$DBCount = $Databases.Count
	
	
	$SelectDatbase = Get-Random -Minimum 1 -Maximum $DBCount
	
	enable-mailbox -Identity $MailboxUser -DomainController $DC -Database ($Databases[$SelectDatbase]) 
}

$job = Start-Job -Name Exchange -ScriptBlock ${Function:New-ExchangeMailbox} -ArgumentList $txtUserName.Text, $($script:ConfigFile.Settings.DomainControllerSettings.DCServerName)
Receive-Job $job.ID
Wait-Job -Id $job.ID | Out-Null

the main drawback with the above method is exchange management tools needs to be loaded on the machine that the script runs.

Thanks again.

Hi,
Just an update on my progress with the above issue. I believe i found a work around thanks to the article that john directed me to. in case anyone in the future stumbles upon this issue this could be a possible solution

create a remote PS Remote session to the exchange server

$Script:session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EXCHANGESERVER.FQDN/Powershell

i then run my first PS remote job to get all Exchange servers mailbox databases. the invoke-command is run as a job so i can use receive-job to get the outback and save it to a variable

if ($Script:DatabaseInfo -eq $null)
{
$DatabaseJob = Invoke-Command -Session $Script:session -ScriptBlock {get-mailboxdatabase} -AsJob
Wait-Job -Id $DatabaseJob.ID -Timeout 120 | Out-Null
$Script:DatabaseInfo = Receive-Job $DatabaseJob
} 
$ExchangeDatbase = New-ExchangeMailbox -Database $Script:DatabaseInfo

A function is then called New-ExchangeMailbox to sort exchange databases. The below function gets all mailbox databases and filters out the Archive databases. then returns the value back which is saved to $ExchangeDatbase

Function New-ExchangeMailbox
{
	try
	{
		Param
		(
			[String]$Databases
		)
		
		$DatabaseFilter = $Databases | Select-Object -ExpandProperty Name | Where-Object{ $_ -notlike "Archive*" }
		$DBCount = $DatabaseFilter.Count

		$SelectDatbase = Get-Random -Minimum 1 -Maximum $DBCount
		return ($DatabaseFilter[$SelectDatbase])
	}
	
	Catch
	{
		$Script:ErrorLog += "$($_.Exception.Message) -- ($(Get-date).DateTime) `n"
	}
}

Finally a second PS remote connection to create the mailbox with the selected database

$MailboxCreatejob = Invoke-Command -Session $Script:session -ScriptBlock { enable-mailbox -Identity $args[0] -Database $args[1] } -ArgumentList $txtUserName.Text, $ExchangeDatbase  -AsJob

volla ! problem solved. its a bit more code than i would have liked but seem to work like a charm.

Thanks Again

Hi,
Just an update on my progress with the above issue. I believe i found a work around thanks to the article that john directed me to. in case anyone in the future stumbles upon this issue this could be a possible solution

create a remote PS Remote session to the exchange server

$Script:session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://EXCHANGESERVER.FQDN/Powershell

i then run my first PS remote job to get all Exchange servers mailbox databases. the invoke-command is run as a job so i can use receive-job to get the outback and save it to a variable

if ($Script:DatabaseInfo -eq $null)
{
$DatabaseJob = Invoke-Command -Session $Script:session -ScriptBlock {get-mailboxdatabase} -AsJob
Wait-Job -Id $DatabaseJob.ID -Timeout 120 | Out-Null
$Script:DatabaseInfo = Receive-Job $DatabaseJob
} 
$ExchangeDatbase = New-ExchangeMailbox -Database $Script:DatabaseInfo

A function is then called New-ExchangeMailbox to sort exchange databases. The below function gets all mailbox databases and filters out the Archive databases. then returns the value back which is saved to $ExchangeDatbase

Function New-ExchangeMailbox
{
	try
	{
		Param
		(
			[String]$Databases
		)
		
		$DatabaseFilter = $Databases | Select-Object -ExpandProperty Name | Where-Object{ $_ -notlike "Archive*" }
		$DBCount = $DatabaseFilter.Count

		$SelectDatbase = Get-Random -Minimum 1 -Maximum $DBCount
		$script:$ExchangeDatabase ($DatabaseFilter[$SelectDatbase])
	}
	
	Catch
	{
		$Script:ErrorLog += "$($_.Exception.Message) -- ($(Get-date).DateTime) `n"
	}
}

Finally a second PS remote connection to create the mailbox with the selected database

$MailboxCreatejob = Invoke-Command -Session $Script:session -ScriptBlock { enable-mailbox -Identity $args[0] -Database $args[1] } -ArgumentList $txtUserName.Text, $script:$ExchangeDatabase -AsJob

volla ! problem solved. its a bit more code than i would have liked but seem to work like a charm.

Thanks Again