51978

Pass a Parameter object (PSCredential) inside a ScriptBlock programmatically in C#

I am trying to run an HPC cmdlet programmatically to change HPC install credential on a remote computer. If run the cmdlet locally, it's pretty straightforward:

Runspace rs = GetPowerShellRunspace(); rs.Open(); Pipeline pipeline = rs.CreatePipeline(); PSCredential credential = new PSCredential(domainAccount, newPassword); Command cmd = new Command("Set-HpcClusterProperty"); cmd.Parameters.Add("InstallCredential", credential); pipeline.Commands.Add(cmd); Collection<PSObject> ret = pipeline.Invoke();

However, if I want to do the same thing with remote PowerShell, I need to run Invoke-Command and pass in the credential to the ScriptBlock inside the Command. How can I do that? It might look something like this, except I need to pass in the credential as an object binded to the InstallCredential parameter inside the ScriptBlock instead of a string:

Pipeline pipeline = rs.CreatePipeline(); PSCredential credential = new PSCredential(domainAccount, newPassword); pipeline.Commands.AddScript(string.Format( CultureInfo.InvariantCulture, "Invoke-Command -ComputerName {0} -ScriptBlock {{ Set-HpcClusterProperty -InstallCredential {1} }}", nodeName, credential)); Collection<PSObject> ret = pipeline.Invoke();

Answer1:

I would continue to use AddCommand for Invoke-Command (instead of AddScript). Add the parameters for Invoke-Command and when you get to Scriptblock parameter, make sure the scriptblock defines a param() block e.g.:

{param($cred) Set-HpcClusterProperty -InstallCredential $cred}

Then add the ArgumentList parameter to the Invoke-Command command and set the value to the credential you have created.

Answer2:

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

I use this, maybe it can help you.

Recommend

  • Invoking exchange activesync cmdlet's from an ASP.NET application
  • PowerShell C# Host - multiple concurrent remote command invocations
  • PowerShell WhereObjectCommand from C#
  • Powershell Get-ExecutionPolicy returns different values
  • Execute a remote generic Powershell script with generic parameters
  • Execute C# interactive powershell scripts
  • Powershell Remote Invoke-Command Start-Process App Immediately Closes After Launch
  • Invoke-Command script block not generating output
  • Change tabs text color in TabLayout to different colors programmatically
  • Why does the compiler find this ambiguous?
  • Pass a Parameter object (PSCredential) inside a ScriptBlock programmatically in C#
  • Powershell commands from C# 'the term is not recognizes as cmdlet'
  • ASP.NET Gridview Paging Problem
  • View Paypal shopping cart contents on my site
  • Timeout a query
  • VSCode change debug shell to bash on windows
  • Error processing multiple files
  • Yii2: Config params vs. const/define
  • Jenkins: How To Build multiple projects from a TFS repository?
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spring security and special characters
  • How do I fake an specific browser client when using Java's Net library?
  • JSON with duplicate key names losing information when parsed
  • Adding custom controls to a full screen movie
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • C# - Getting references of reference
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • reshape alternating columns in less time and using less memory