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.
Using Start-Job with credentials
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.
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.
- Shelltastic
- Posts: 65
- Last visit: Mon Feb 19, 2024 11:31 am
Re: Using Start-Job with credentials
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
- Shelltastic
- Posts: 65
- Last visit: Mon Feb 19, 2024 11:31 am
Re: Using Start-Job with credentials
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).
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
(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)
}
}
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)
}
}