Repository navigation
how to pass PASSession information into a Scriptblock? #614
gardnerrowe
started this conversation in
General
Replies: 3 comments
|
Good news is there's already a pair of commands built for exactly this:
That's the piece you were missing, but it only works cleanly if the object is passed as a live reference, not serialized and rebuilt on the other side. a fix could be to parallelize in a way that stays in the same process, using Both run on separate threads rather than separate processes, so the session object keeps its type: # Authenticate once in the main runspace
$token = Get-Credential
New-PASSession -Credential $token -BaseURI https://cyberark.example.com
# Grab a portable snapshot of the authenticated session
$pasSession = Get-PASSession
$usernames = 'user1', 'user2', 'user3'
$usernames | ForEach-Object -Parallel {
Import-Module psPAS
# Re-hydrate the session in this thread's runspace
Use-PASSession -Session $using:pasSession
Get-PASUser -UserName $_
} -ThrottleLimit 5Or with $jobs = foreach ($name in $usernames) {
Start-ThreadJob -ScriptBlock {
param($Session, $UserName)
Import-Module psPAS
Use-PASSession -Session $Session
Get-PASUser -UserName $UserName
} -ArgumentList $pasSession, $name
}
$jobs | Wait-Job | Receive-Job |
0 replies
|
Hi Pete,
Thank you for the response! I had forgotten that I had asked the question.
It has been a minute since I was working on this, but I am really looking forward to getting back on it.
BTW, Love the tools!
Very helpful and super easy to use. Sometimes, I could use a little more detail in the examples, but I figure out most of it. Overall, I really like the work you have done.
I would describe my scripting experience as more than a novice, but less than a developer. I like the word “cobbler”; I learn a few things here and there, borrow a snippet or two and debug them to understand them.
Sometimes I need a little jump start. AI is helping me get over the hump on several things.
Respectfully,
Gardner Rowe
Principal Appl Sys Analyst
IT Engineering
***@***.******@***.***>
T 713-123-4567
From: Pete Maan ***@***.***>
Sent: Wednesday, August 5, 2026 5:08 PM
To: pspete/psPAS ***@***.***>
Cc: Gardner Rowe ***@***.***>; Author ***@***.***>
Subject: [EXTERNAL] Re: [pspete/psPAS] how to pass PASSession information into a Scriptblock? (Discussion #614)
SLOW DOWN! - EXTERNAL SENDER: ***@***.******@***.***>
Be suspicious of tone, urgency, and formatting. Do not click/open links or attachments on a mobile device. Wait until you are at a computer to confirm you are absolutely certain it is a trusted source.
If you are at all uncertain use the Report Phish button and our Cybersecurity team will investigate.
Good news is there's already a pair of commands built for exactly this: Get-PASSession and Use-PASSession:
* Get-PASSession hands you a snapshot copy of the module's internal state (auth token, base URI, etc.) as a psPAS.CyberArk.Vault.Session object.
* Use-PASSession takes that object back in and re-seeds the module's internal session variable, wherever it's called from.
That's the piece you were missing, but it only works cleanly if the object is passed as a live reference, not serialized and rebuilt on the other side.
a fix could be to parallelize in a way that stays in the same process, using ForEach-Object -Parallel (PS7+) or Start-ThreadJob.
Both run on separate threads rather than separate processes, so the session object keeps its type:
# Authenticate once in the main runspace
$token = Get-Credential
New-PASSession -Credential $token -BaseURI https://cyberark.example.com
# Grab a portable snapshot of the authenticated session
$pasSession = Get-PASSession
$usernames = 'user1', 'user2', 'user3'
$usernames | ForEach-Object -Parallel {
Import-Module psPAS
# Re-hydrate the session in this thread's runspace
Use-PASSession -Session $using:pasSession
Get-PASUser -UserName $_
} -ThrottleLimit 5
Or with Start-ThreadJob:
$jobs = foreach ($name in $usernames) {
Start-ThreadJob -ScriptBlock {
param($Session, $UserName)
Import-Module psPAS
Use-PASSession -Session $Session
Get-PASUser -UserName $UserName
} -ArgumentList $pasSession, $name
}
$jobs | Wait-Job | Receive-Job
—
Reply to this email directly, view it on GitHub<https://urldefense.com/v3/__https:/github.com/pspete/psPAS/discussions/614?email_source=notifications&email_token=AWXXGTHF7UEU7TKBD6TOLUL5IOV4LA5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZZGEZDOMZXUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVRTG633UMVZF6Y3MNFRWW*discussioncomment-17912737__;Iw!!PfbeBCCAmug!g0tAaCdIbIcjFsm2n8OPAcQgAoe6nrEk5X2GJZpyf45-yTPG5UZhRNZfzREkOE0K7K-xumAcs5vNN36fhqcYppvh$>, or unsubscribe<https://urldefense.com/v3/__https:/github.com/notifications/unsubscribe-auth/AWXXGTAHG4CQBV6R2ZZVDED5IOV4LAVCNFSNUABHKJSXA33TNF2G64TZHM4TKOJZGU3DMNZ3IRUXGY3VONZWS33OHMYTANBSHA2TKMVBOYBA__;!!PfbeBCCAmug!g0tAaCdIbIcjFsm2n8OPAcQgAoe6nrEk5X2GJZpyf45-yTPG5UZhRNZfzREkOE0K7K-xumAcs5vNN36fhhs6QF45$>.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS<https://urldefense.com/v3/__https:/github.com/notifications/mobile/ios/AWXXGTGYEKKZNEBA4MMIEFD5IOV4LA5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZZGEZDOMZXUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVJTG633UMVZF62LPOM__;!!PfbeBCCAmug!g0tAaCdIbIcjFsm2n8OPAcQgAoe6nrEk5X2GJZpyf45-yTPG5UZhRNZfzREkOE0K7K-xumAcs5vNN36fhhxvSfpw$> and Android<https://urldefense.com/v3/__https:/github.com/notifications/mobile/android/AWXXGTEVZKUIJSEK232P4635IOV4LA5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZZGEZDOMZXUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVZTG633UMVZF6YLOMRZG62LE__;!!PfbeBCCAmug!g0tAaCdIbIcjFsm2n8OPAcQgAoe6nrEk5X2GJZpyf45-yTPG5UZhRNZfzREkOE0K7K-xumAcs5vNN36fhr1njTIO$>. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.******@***.***>>
The information contained in this e-mail message may be privileged, confidential, and/or protected from disclosure. This e-mail message may contain protected health information (PHI); dissemination of PHI should comply with applicable federal and state laws. If you are not the intended recipient, or an authorized representative of the intended recipient, any further review, disclosure, use, dissemination, distribution, or copying of this message or any attachment (or the information contained therein) is strictly prohibited. If you think that you have received this e-mail message in error, please notify the sender by return e-mail and delete all references to it and its contents from your systems.
|
0 replies
|
all examples have been updated for the latest release, available online here: https://pspas.pspete.dev/commands/ |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I have connected to my URI and have a session defined.
I have several lookups I want to do to initialize the script (e.g., Get-PASUser and Get-PASSafe)
I was hoping that separating them into PowerShell jobs would speed up the script.
I want to call a PowerShell job using a script block. But I can't figure out how to pass the PASSession information.
I have tried two different methods...
I define my sciptblock like this...
...and make this call:
$job_pas_Users = Start-Job -Name $jobName -ScriptBlock $scriptBlock -ArgumentList $session, $jobNameI get this error:
Invalid URI: The hostname could not be parsed. Run New-PASSessionI have tried a couple other things like calling Use-PASSession $session
I modified my scriptblock like this...
...and make the same call:
$job_pas_Users = Start-Job -Name $jobName -ScriptBlock $scriptBlock -ArgumentList $session, $jobNameand I get this error:
All reactions