Start-Job control set and PowerCLI

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Airbag
Posts: 2
Last visit: Thu Jul 06, 2017 12:13 am

Start-Job control set and PowerCLI

Post by Airbag »

Hello everyone, hoping i can get some help on a strange issue i've been having.

As part of a script which includes some PowerCLI commands i need to connect to our vCenter server, since this takes a few moments i tried putting it into the "Start Job" Control Set, see code below.
  1. $buttonStartJob_Click = {
  2.    
  3.     $Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $User, $Password # <-- These variables are declared from textboxes earlier in the script
  4.     try
  5.     {
  6.         Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction Stop
  7.     }
  8.     Catch
  9.     {
  10.         Import-Module VMware.VimAutomation.Core
  11.     }
  12.    
  13.     $buttonStartJob.Enabled = $false   
  14.    
  15.     #Create a New Job using the Job Tracker
  16.     Add-JobTracker -Name "JobName" `
  17.     -JobScript {
  18.         #--------------------------------------------------
  19.         #TODO: Set a script block
  20.         #Important: Do not access form controls from this script block.
  21.    
  22.         Param($Script:Credentials)#Pass any arguments using the ArgumentList parameter
  23.         Connect-VIServer -Server <server> -Credential  $Credentials
  24.  
  25.         #--------------------------------------------------
  26.     }`
  27.     -CompletedScript {
  28.         Param ($Job)
  29.         #$results = Receive-Job -Job $Job
  30.         #Enable the Button
  31.         $buttonStartJob.ImageIndex = -1
  32.         $buttonStartJob.Enabled = $true
  33.         Get-VM <VM Name>    # <--- Just to test if i'm connected or not.
  34.     }`
  35.     -UpdateScript {
  36.         Param($Job)
  37.         #$results = Receive-Job -Job $Job -Keep
  38.         #Animate the Button
  39.         if($buttonStartJob.ImageList -ne $null)
  40.         {
  41.             if($buttonStartJob.ImageIndex -lt $buttonStartJob.ImageList.Images.Count - 1)
  42.             {
  43.                 $buttonStartJob.ImageIndex += 1
  44.             }
  45.             else
  46.             {
  47.                 $buttonStartJob.ImageIndex = 0     
  48.             }
  49.         }
  50.     }`
  51.     -ArgumentList $Script:Credentials
  52. }
Now, no matter what i try i always get an error that i'm not connected to a vCenter server, i even tried hardcoding my username and password with the -User and -Password parameters instead but i get the same result. The strange thing is if i put the Connect-VIServer inside the -CompletedScript block then i connect just fine, but thats no good...

I'm guessing it's a scoping issue but as i said i just can't fiugre this one out,
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Start-Job control set and PowerCLI

Post by jvierra »

You cannot do this. The connection will be created in an isolated session which will be terminated when the job completes.

Some things you just have to wait for.
User avatar
Airbag
Posts: 2
Last visit: Thu Jul 06, 2017 12:13 am

Re: Start-Job control set and PowerCLI

Post by Airbag »

I suspected as much, and there is no other work-around to keep the form from freezing while connecting then?
This topic is 6 years and 8 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked