Firstly, I don’t rant (well, I do, but never on forums like this).
However, if / when I do, I am clear to state that it is a rant by fully qualifying it like so.
//begen rant
…yadd, yadda, yadda.
//end rant
Why, because I don’t like abiguity in conversation to knowledge exchnages.
However, unqualified rant is a non-sequitur and it’s not productive for anyone at anytime. The bold was only separate the question for the comment.
Folks using all caps, are ranting and yelling motifs as well. At least that is the take most have on the topic and as such, again, a non-sequitur.
For me, even when folks to the all caps thing, I simply ignore it, as it happens for whatever reason they feel they need to do it. None the less, much as you feel mine was a rant, which it was not, the all caps thing is also not prudent to any productive conversation.
Your second post is far more detailed, and things would have been far more clear with this one had it been your first post. So, all things being equal, all this angst is rather moot.
As to your concern, this…
“if I remove $credential parameter from the script block and use the credentials from a CLIXML it works. but I want to be able to use it as a parmeter”
… is an odd thing for sure and I’ve seen/had this happen (multiple times) with straight calls to MSOL for different operational (flow) steps. Meaning, you set creds at one point but they do not carry to another. The CLIXML (stored creds and call them at each operational request where I’ve gotten the no creds thing happening) approach is what I have defaulted to for sometime because of this oddity. I’ve yet to figure out why the cred loss for inline script efforts. For now, the CLIXML does the job until I decided to dig at this more when I get time.
So, it appares you’ve now run into this ghost as I have, and though you have an admitted workaround, similar to what I am doing, you consider it sub-optimal for your efforts.
So, if it were me, based on what you show now. I’d remove all the connection stuff to it’s own block or function (it’s what I had to do in my use cases), call it once, and execute on the jobs segment . So, using your original shortened code block.
$Credential = Get-Credential -Credential admin@domain.onmicrosoft.com'
$paramNewPSSession = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionUri = 'https://outlook.office365.com/powershell-liveid/'
Credential = $Credential
Authentication = 'Basic'
AllowRedirection = $true
}
$Session = New-PSSession @paramNewPSSession
Import-PSSession $Session -AllowClobber -DisableNameChecking
Get-PSSession
$script = {Get-Mailbox}
$range = 1 .. 3
foreach ($i in $range)
{ Start-Job -Name "job$i" -ScriptBlock {Invoke-Command -session $session {$script}}}
Start-Sleep -Seconds 9
Get-Job | Format-Table -AutoSize
# Results
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.0 tmp_nd3zuojn.jcf {Add-AvailabilityAddressSpace...}
... :
...
Id : 2
Name : job1
ChildJobs : {Job3}
...
PSJobTypeName : BackgroundJob
...
State : Running
...
Id : 4
Name : job2
ChildJobs : {Job5}
...
PSJobTypeName : BackgroundJob
...
State : Running
...
Id : 6
Name : job3
ChildJobs : {Job7}
...
PSJobTypeName : BackgroundJob
...
State : Running
Start-Sleep -Seconds 9
Get-Job | Format-Table -AutoSize
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
2 job1 BackgroundJob Completed True localhost Invoke-Command -session $session {$script}
4 job2 BackgroundJob Completed True localhost Invoke-Command -session $session {$script}
6 job3 BackgroundJob Completed True localhost Invoke-Command -session $session {$script}