Using Start-Job with credentials

Ask your Windows 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.
User avatar
PS_Ross
Posts: 41
Joined: Thu Mar 01, 2018 7:46 am

Re: Using Start-Job with credentials

Post by PS_Ross » Fri Apr 12, 2019 11:21 am

Ok, that is what I tried initially, however it didn't seem to work. When I run the exact code you have above and debug the program, I get errors, in this order...

ERROR: A parameter cannot be found that matches parameter name 'AllowClobber'.
ERROR: Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for the argument, and then try running the command again.
ERROR: The term 'Get-MailboxDatabase' is not recognized as the name of a cmdlet, function, script file, or operable program

The third error is a result of the module not getting pulled down because the connection to Exchange never seems to be made, so disregard that one.

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using Start-Job with credentials

Post by jvierra » Fri Apr 12, 2019 11:37 am

Remove AllowClobber. I just copied you parameters without thinking. "AllowClobber" is a parameter on Import-PsSession.

User avatar
jvierra
Posts: 13687
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using Start-Job with credentials

Post by jvierra » Fri Apr 12, 2019 11:39 am

This is what should work:

Code: Select all

$sb = {
    $splat = @{
        ConfigurationName = 'Microsoft.Exchange'
        ConnectionUri     = 'http://ServerName.doamin.com/PowerShell/'
    }
    $s = New-PSSession @splat
    Import-PSSession $s -AllowClobber
    
    (Get-MailboxDatabase | Where-Object { $_.MasterServerOrAvailabilityGroup -eq "DAG1" }).count
}

Start-Job -Name Job1 -Credential $global:usercred -ScriptBlock $sb

User avatar
PS_Ross
Posts: 41
Joined: Thu Mar 01, 2018 7:46 am

Re: Using Start-Job with credentials

Post by PS_Ross » Tue Apr 16, 2019 5:54 am

I wound up taking a slightly different approach to this and using Powershell Studio's "Button - Start Job" control set. Below is my final code that is working (assume I have all the functions inserted correctly, to long to post them all).

Code: Select all

$Job1 = @{
		Name	  = 'Job1'
		JobScript = {
			Param ($global:usercred)
			
			$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ServerName.domain.com/PowerShell -Credential $global:usercred -Authentication Kerberos
			Import-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
			
			(Get-MailboxDatabase | where { $_.MasterServerOrAvailabilityGroup -eq "USADAG07" }).count			
			for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
			
		}
		ArgumentList = $global:usercred
		CompletedScript = {
			Param ($Job)
			$results = Receive-Job -Job $Job
			$textbox1.Text = $results
		}
		UpdateScript = {
			Param ($Job)
			
		}
	}
The above code works correctly and returns a number, which is just a count of how many databases are in that particular DAG. I do have one question with this approach though, if I were to run multiple commands, and use a variable for each, how would I go about referencing those variables in the CompleteScript block? Something like this...

Code: Select all

$Job1 = @{
		Name	  = 'Job1'
		JobScript = {
			Param ($global:usercred)
			
			$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ServerName.domain.com/PowerShell -Credential $global:usercred -Authentication Kerberos
			Import-Module (Import-PSSession $s -AllowClobber -DisableNameChecking) -DisableNameChecking
			
			$DAG07 = (Get-MailboxDatabase | where { $_.MasterServerOrAvailabilityGroup -eq "USADAG07" }).count
			$DAG08 = (Get-MailboxDatabase | where { $_.MasterServerOrAvailabilityGroup -eq "USADAG08" }).count		
			for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 100 }
			
		}
		ArgumentList = $global:usercred
		CompletedScript = {
			Param ($Job)
			????????????
		}
		UpdateScript = {
			Param ($Job)
			
		}
	}


Locked