Using Start-Job with credentials

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 4 years and 11 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
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Using Start-Job with credentials

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using Start-Job with credentials

Post by jvierra »

Remove AllowClobber. I just copied you parameters without thinking. "AllowClobber" is a parameter on Import-PsSession.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using Start-Job with credentials

Post 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
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Using Start-Job with credentials

Post 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)
			
		}
	}

This topic is 4 years and 11 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