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

Using Start-Job with credentials

Post by Shelltastic »

I am looking for some assistance using the Start-Job cmdlet. I am trying to run a background job to query some data from my Exchange environment. I need to build the connection to Exchange first, I currently use the following code to do so...

Code: Select all

$usercred = Get-Credential
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ServerName.domain.com/PowerShell/ -Credential $usercred -Authentication Kerberos
Import-Module (Import-PSSession $s -AllowClobber) -DisableNameChecking
This code will not work inside the scriptblock for Start-Job since it's a different thread and will not allow me to interact with it. What methods could I look at to go about connecting to my Exchange environment so I can query data inside the Start-Job scriptblock?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Using Start-Job with credentials

Post by davidc »

[TOPIC MOVED TO WINDOWS POWERSHELL FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
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 »

You would have to run all of your code as a job. Why use a job? The connection only takes 10 seconds even on a slow computer.

Your scriptblock is also not written correctly which may cause many weird issues.

Code: Select all

$usercred = Get-Credential
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ServerName.domain.com/PowerShell/ -Credential $usercred -Authentication Kerberos
Import-PSSession $s -AllowClobber
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 »

I would also suggest this for ease of management and simplicity:

Code: Select all


$splat = @{
    ConfigurationName = 'Microsoft.Exchange'
    ConnectionUri = 'http://ServerName.domain.com/PowerShell/'
    Credential = 'domain\userid'
    AllowClobber = $true
}
$s = New-PSSession @splat
Import-PSSession $s 
Credentials are automatically prompted.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Using Start-Job with credentials

Post by Shelltastic »

jvierra wrote: Thu Apr 11, 2019 1:12 pm You would have to run all of your code as a job. Why use a job? The connection only takes 10 seconds even on a slow computer.

Your scriptblock is also not written correctly which may cause many weird issues.

Code: Select all

$usercred = Get-Credential
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ServerName.domain.com/PowerShell/ -Credential $usercred -Authentication Kerberos
Import-PSSession $s -AllowClobber
I'm aware that the connection runs rather quickly, that is not the part I am worried about, and it is not written incorrectly, I run it a dozen times a day, you may chose to modify it, but it runs with the desired result as per my configuration requires.

I am trying to query large amounts of data in my Exchange environment, that is the code I use to build the connection from my local PC, I can't just run the Exchange cmdlet's in the Start-Job script block because the Exchange module needs to be loaded, which gets pulled down upon making the connection. My issue is, I can't just run the connection code I posted above in the Start-Job script block because I need it to prompt for my credentials so I can enter them. So I am trying to figure out the best approach to this here, which is the help I am asking for.
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 »

You can pass the credentials to the script block easily enough.
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 »

Since you are already in a domain and using Kerberos you can very easily just do this.

Code: Select all


$sb = {
    $splat = @{
        ConfigurationName = 'Microsoft.Exchange'
        ConnectionUri = 'http://ServerName.domain.com/PowerShell/'
        AllowClobber = $true
    }
    $s = New-PSSession @splat
    Import-PSSession $s
    # other code
}
Start-Job -ScriptBlock $sb -Credential domain\userid
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Using Start-Job with credentials

Post by Shelltastic »

jvierra wrote: Thu Apr 11, 2019 4:00 pm Since you are already in a domain and using Kerberos you can very easily just do this.

Code: Select all


$sb = {
    $splat = @{
        ConfigurationName = 'Microsoft.Exchange'
        ConnectionUri = 'http://ServerName.domain.com/PowerShell/'
        AllowClobber = $true
    }
    $s = New-PSSession @splat
    Import-PSSession $s
    # other code
}
Start-Job -ScriptBlock $sb -Credential domain\userid
Ok, thanks for the help on that one. I actually prompt the user for their credentials at the start of my program. Is there any way I could take those credentials already entered and just pass them through here? In your example above, the user is prompted. It wouldn't scale very well if I had to make the user enter their credentials every time I try to run a job.
User avatar
Shelltastic
Posts: 65
Last visit: Mon Feb 19, 2024 11:31 am

Re: Using Start-Job with credentials

Post by Shelltastic »

Here is an example of what I have...

Code: Select all

	$sb = {
		$splat = @{
			ConfigurationName = 'Microsoft.Exchange'
			ConnectionUri	  = 'http://ServerName.doamin.com/PowerShell/'
			AllowClobber	  = $true
		}
		$s = New-PSSession @splat -Credential $global:usercred -Authentication Kerberos
		Import-PSSession $s
		
		(Get-MailboxDatabase | where { $_.MasterServerOrAvailabilityGroup -eq "DAG1" }).count
	}
	
	Start-Job -Name Job1 -ArgumentList $global:usercred -ScriptBlock $sb
	
	$Job1Results = Receive-Job 'Job1'
The $global:usercred variable looks like this...

Code: Select all

$global:usercred = Get-Credential
$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ServerName.domain.com/PowerShell/ -Credential $global:usercred -Authentication Kerberos
Import-Module (Import-PSSession $s -AllowClobber) -DisableNameChecking
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 »

Just pass the credential object as the job credential.

Code: Select all

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

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

$Job1Results = Receive-Job 'Job1'
You do not need to use the credential in the job if the job is running with the correct credential. You don't need to specify the authentication as that will be automatic.
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