mrdon
December 21, 2020, 11:31pm
1
Hi
I am trying to create multiple PSSession to exchange management shell. I can’t figure out how to get this done. The code below is
foreach ($Server in $Serverlist) {
$UserCredential = Get-Credential
$MySession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$Server.globalnet.lcl/PowerShell/ -Authentication Kerberos -Credential $UserCredential
Import-PSSession $MySession
Get-MailboxDatabaseCopyStatus | Select-Object Name, Status, activationpreference | Format-Table -AutoSize
}
Get-PSSession | Remove-PSSession
Hello,
Can you please elaborate? What the goal?
Are you connecting to all servers using same credentials?
Thank you.
foreach ($Server in $Serverlist) {
$uri = "http://$Server.globalnet.lcl/PowerShell/"
$UserCredential = Get-Credential
$MySession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $uri -Authentication Kerberos -Credential $UserCredential
Invoke-Command -Session $MySession -ScriptBlock {
Get-MailboxDatabaseCopyStatus | Select-Object Name, Status, activationpreference}
}
Get-PSSession | Remove-PSSession
Try to use Invoke-Command with Session parameter.
You can try something similar to this:
$s = New-PSSession -ComputerName Server02 -Credential Domain01\User01
Invoke-Command -Session $s -ScriptBlock {Get-MaboxDatabaseSatus}
Or this:
$parameters = @{
ComputerName = "Server01", "Server02", "TST-0143", "localhost"
ConfigurationName = 'MySession.PowerShell'
ScriptBlock = { Get-MaboxDatabaseSatus }
}
Invoke-Command @parameters
Hope that helps.
mrdon
December 22, 2020, 4:49am
5
Oh, thanks. It’s functioning.
I tried to add the line below and received error.
Error: the term format table is not recognized as the name of a cmdlet
Invoke-Command -Session $MySession -ScriptBlock {
Get-MailboxDatabaseCopyStatus | Select-Object Name, Status, activationpreference | Format-Table -Autosize}
}
You should Format-Table outside of the Invoke-Command as it’s a formatting command. The data needs to be serialized and sent over and I’m fairly certain that breaks with the Format-* commands.
Invoke-Command -Session $MySession -ScriptBlock {
Get-MailboxDatabaseCopyStatus | Select-Object Name, Status, activationpreference}
} | Format-Table -Autosize
mrdon
December 22, 2020, 1:43pm
7
Thanks. It is working now.