Page 2 of 2

Re: Using Start-Job with credentials

Posted: Fri Apr 12, 2019 11:21 am
by Shelltastic
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.

Re: Using Start-Job with credentials

Posted: Fri Apr 12, 2019 11:37 am
by jvierra
Remove AllowClobber. I just copied you parameters without thinking. "AllowClobber" is a parameter on Import-PsSession.

Re: Using Start-Job with credentials

Posted: Fri Apr 12, 2019 11:39 am
by jvierra
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

Re: Using Start-Job with credentials

Posted: Tue Apr 16, 2019 5:54 am
by Shelltastic
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)
			
		}
	}