hudzon
July 30, 2019, 5:44pm
1
OK I need a little help with this one.
I have PSSession1 and PSSession2
I have a lot of cmdlets so I enter the PSSessions and run my cmdlets from within the sessions
instead of invoke-commands
in PSession1 I have an array
I need to Exit PSSession1 and Enter-PSSession2 and have the array from PSSession1 available in PSSession2
once I exit PSSession1 I lose the array from PSSession1 and therefore cannot add it to the new session.
hudzon
July 30, 2019, 6:19pm
2
Start-Transcript .\Moves.log -Append
Get-Date
$creds = Get-Credential "lab01\admin"
##########################################
# Creating PSSessions
##########################################
$script:EXLAB01 = New-PSSession -ComputerName Server01 -Credential $creds -Authentication Credssp
Invoke-Command -Session $EXLAB01 -ScriptBlock {
Add-PSSnapin -Name *.exchange*
}
$script:EXLAB02 = New-PSSession -ComputerName Server02 -Credential $creds -Authentication Credssp
Invoke-Command -Session $ EXLAB02 -ScriptBlock {
Add-PSSnapin -Name *.exchange*
}
Enter-PSSession $ EXLAB02
$RemoteCredentials = Get-Credential "lab02\admin"
##########################################
# Check for any mailboxes
##########################################
$EXmbxs = get-mailbox -Server Server03 -DomainController DC01.lab02.COM -Credential $RemoteCredentials | Where-Object { $_ .OrganizationalUnit -eq "lab02/Users" -and $_ .Name -ne "scom_mailbox" }
if ( ! $EXmbxs )
{
Stop-Transcript
Exit
}
##########################################
# Setting CustomAttribute13 to "MoveMe"
##########################################
foreach ( $mailbox in $EXmbxs )
{
set-mailbox $mailbox –CustomAttribute13 "MoveMe" -DomainController DC01.lab02.COM -Credential $RemoteCredentials
}
$ReadyToMove = @()
foreach ( $mailbox in $EXmbxs )
{
$Recipient = Get-MailUser $mailbox
$Recipient += $ReadyToMove
}
If ( ! $ReadyToMove )
{
Stop-Transcript
Exit
}
##########################################
# moving any mailboxes ready to move
##########################################
foreach ( $RTMMBX in $ReadyToMove )
{
New-MoveRequest $RTMMBX -RemoteLegacy -TargetDatabase "Mailbox Database" -BadItemLimit 200 -RemoteCredential $RemoteCredentials -RemoteGlobalCatalog DC01.lab02.COM -TargetDeliveryDomain lab01.com -AcceptLargeDataLoss
$MoveRequestStatus = Get-MoveRequest $RTMMBX
do
{
Get-MoveRequest $RTMMBX | Get-MoveRequestStatistics
Start-Sleep 30
}
until ( $MoveRequestStatus .status -eq 'Completed' )
Start-Sleep 15
Get-MoveRequest $RTMMBX | Remove-MoveRequest -Confirm : $false
}
Exit-PSSession
Start-Sleep 10
Enter-PSSession $EXLAB01
##########################################
# moving mailboxes to Exchange 2013
##########################################
#
#
#########################################
# This is where I need the array
# $ReadyToMove from the other session
#########################################
$ReadyToMove
$DB = "newdb"
foreach ( $RTMMBX in $ReadyToMove ){
New-MoveRequest $RTMMBX -TargetDatabase $DB -BadItemLimit 200 -Priority Emergency
}
Stop-Transcript
$Session1 = New-PSSession -ComputerName Server01 -Credential $cred1
$Session2 = New-PSSession -ComputerName Server02 -Credential $cred2
$DataFromSession1 = Invoke-Command -Session $Session1 -ScriptBlock { Get-Service } # or some commands that return some data
Invoke-Command -Session $Session2 -ScriptBlock {
# Here's my data from $Session1
$Using:DataFromSession1
}
hudzon
August 1, 2019, 9:42pm
4
I know I can do it with invoke command from outside of the session
what I am trying to do is to export it from within the session.
If you look at the script I posted you can see I enter the sessions run a bunch of cmdlets and then needs to export that data from the array
to the other session.
I may need to export to a file on a share and then clean it up after I import the data back to the second session.
But was hoping there was an easier or cleaner way.
Please articulate your request clearly
nitbha
August 3, 2019, 12:44pm
6
please give your problem statement more clearly and what is your aim to achieved ?