Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:28:17+00:00 2026-06-15T23:28:17+00:00

I have a PSCredential Object in C# and want to pass it as parameter

  • 0

I have a PSCredential Object in C# and want to pass it as parameter for this PowerShell Script

This is the PSCredential Object

    PSCredential Credential = new PSCredential ( "bla" , blasecurestring)

This is the Script I want to run in C#

powershell.AddScript("$s = New-PSSession -ComputerName '" + serverName + "' -Credential " + Credential);

I couldn’t understand the solution which is offered here Pass a Parameter object (PSCredential) inside a ScriptBlock programmatically in C#

EDIT:
This thing is working

powershell.AddCommand("New-PSSession").AddParameter("ComputerName", serverName).AddParameter("Credential", Credential);

But how can I save the Session Info in a variable? I need them for the following commands:

powershell.AddScript(@"Invoke-Command -Session $s -ScriptBlock {" + cmdlet + "}");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:28:18+00:00Added an answer on June 15, 2026 at 11:28 pm

    I found the solution now. It’s so easy when you know what you do…

    powershell.AddCommand("Set-Variable");
    powershell.AddParameter("Name", "cred");
    powershell.AddParameter("Value", Credential);
    
    powershell.AddScript(@"$s = New-PSSession -ComputerName '" + serverName + "' -Credential $cred");
    powershell.AddScript(@"$a = Invoke-Command -Session $s -ScriptBlock {" + cmdlet + "}");
    powershell.AddScript(@"Remove-PSSession -Session $s");
    powershell.AddScript(@"echo $a");
    

    Where Credential is the C# PSCredential object

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Powershell script that is going to be run through an automation
Let's say I have a PSCrendential object in PowerShell that I created using Get-Credential
have written this little class, which generates a UUID every time an object of
Have have this line of code in my form when I create a new
I have a Powershell script to detect disk space on a network server that
I'm developing a credential provider and credential. So I have this class SampleProvider and
Have converted devise new session from erb to Haml but doens't work, this is
I'm writing a powershell v2 script that I'd like to run against a remote
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have finally got a responsive site working (of a fashion). What I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.