Powershell hashtables as argument to custom cmdlet in C#


Ive been using a custom class for some time like this in PS (2.0):

import-module .\MyClassLib.dll $task = New-Object MyClassLib.OracleScript -Property @{ Files="MyScript.sql" Database="TEST" User="USER" Password="PASSWORD" } $result = $task.Execute()

And this works just fine.

However i wanted to create a CmdLet in C# to do the work instead. So after creating the cmdlet i thought I could do one of the following:

Invoke-OracleScript @{ Files="Script.sql" Database="db" User="user" Password="password" } Invoke-OracleScript @{ Files="Script.sql"; Database="db"; User="user"; Password="password"; } Invoke-OracleScript -Property @{ Files="Script.sql" Database="db" User="user" Password="password" }

But no luck. :(.

I keep getting errors like:

<ul><li>Cannot find the file System.Collections.Hashtable ( it thinks the entire hashtable is the Files parameter)</li> <li>A parameter cannot be found that matches parameter name 'Property'</li> <li>And a couple more.</li> </ul>

My Class:

[Cmdlet(VerbsLifecycle.Invoke, "OracleScript", ConfirmImpact = ConfirmImpact.High, SupportsShouldProcess = true, SupportsTransactions = false)] public class Invoke_OracleScript : Cmdlet, IOracleScript { [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] public string Files { get; set; } [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)] public string Database { get; set; } ....

If I instead use parameters like this: -Files "" -Database "", it works just fine, but everything has to be on 1 line which is very bad for reading. So the hashtable is really my biggest wish :).

Can anyone explain to me what im missing here? (ParameterSets?, a secret attribute i've been unable to find in the docs, other)

Kind regards


If all you care is to make the cmdlet call to be in multiple lines, you can use backtick (`) to extend the command to the next line:

get-process -Name notepad ` -Computername localhost ` -Verbose

Or, you can create custom object:

$process = new-object psobject $process | add-member -name name -value notepad -type noteproperty $process | add-member -name computername -value localhost -type noteproperty $process | get-process

Or, what you were doing:

$process = new-object psobject -property @{ name="notepad"; computername = "localhost";} $process | get-process

I think hashtable as objects was a feature that was added in v3.0 ( currently in CTP)


You might want to take a look at a feature of PowerShell called Splatting. By default, a Cmdlet can take all its parameters and their values as a hashtable and you can pass that hash in as a param.

Function Add-ThreeNumbers { param ($a,$b,$c) $a + $b + $c } $params = @{a=10; b=15; c = 20} Add-ThreeNumbers @params


  • Publishing ASP.Net Core app to Azure silently fails in command line
  • How to convert string to table or objects in powershell
  • General contract for object comparision : equals() and hashCode()
  • Recursive function not behaving correctly
  • Mixing SQL query and PowerShell send-mailmessage
  • What exactly does PowerShell's -Version parameter do?
  • Passing a hashtable from C# to Powershell
  • How to read data from a text file if the file location is not known in c#?
  • View/Download Pdf Files in React - Router 4
  • string.IsNullOrEmpty() Doesn't Seem to Work on a String within a Class within a Class
  • Many to Many in Linq using Dapper
  • Trying to string.Join an IList
  • Configure Spring's MappingJacksonHttpMessageConverter
  • How can I tell a form not to dispose a particular control when it closes?
  • Hash Code in SQL Server?
  • Can't delete or rename original file after resizing
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • Read text file that is not in the main package in a runnable jar
  • How to revert to previous XCode version?
  • Display images in Django
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Word Open XML Mail Merge
  • MongoError: Incorrect arguments
  • Django rest serializer Breaks when data exists
  • Where to put my custom functions in Wordpress?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Numpy divide by zero. Why?
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j
  • How does Linux kernel interrupt the application?
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS