Tag Archives: Where-Object

Quicktip: Use a Csv file to splat parameters into a function

I recently received a question on Reddit that asked if it is possible to use a csv file to create a hash table that can be used for splatting parameters into a function. The goal of this exercise is to be able to specify a csv file and to pass on the proper arguments to a function.

For example there is a csv file with the following contents:

SplatCsv

So based on this the New-Server function should be called as such:

1
New-Server -Templare 2012R2 -Spec DomainJoinSpec -Datastore Production2 -Cpus 1

In order to achieve this, the first step would be to remove the empty fields from the equasion as such:

1
2
$Csv = Import-Csv -Path Parameters.csv
$Csv.psobject.Properties | Where-Object {$_.Value}

This will import the csv and only display the colums in the csv that actually contain data. To ensure this data is stored in a hash table we can use the ForEach-Object. This cmdlet can be used to loop through the remaining results and to create a hashtable:

1
2
3
4
5
6
$Csv = Import-Csv c:\temp\params.csv
$Csv.psobject.Properties | Where-Object {$_.Value} | ForEach-Object -Begin {
$SplatParams = @{}
} -Process {
$SplatParams[$_.Name] = $_.Value
}

Now that the hash table has been created this can be used for splatting into the New-Server function:

1
New-Server @SplatParams

By combining the ForEach-Object cmdlet and the PSObject property that is piped into Where-Object it is possible to construct a custom hashtable that can be used for splatting. This can provide an easy alternative for non-technical users to provide parameters and arguments into a PowerShell function.

Share

Quicktip: Get-PSDrive does not filter provider with Where-Object

When attempting to filter the results of Get-PSDrive you might notice that filtering by using Where-Object for the Provider property does not work as expected. For example the following code will not yield the expected results and will in fact not return any drives:

Get-PSDRive | Where-Object {$_.Provider -eq 'FileSystem'}

So let’s have a look at the reason for this, if we take a look at the Provider property of the output and what it contains:

PS C:\Users\JaapBrasser (Get-PSDrive).Provider.GetType()
 
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
 
PS C:\Users\JaapBrasser (Get-PSDrive).Provider | Get-Member -MemberType Property
 
 TypeName: System.Management.Automation.ProviderInfo
 
Name MemberType Definition
---- ---------- ----------
Capabilities Property System.Management.Automation.Provider.ProviderCapabil...
Description Property string Description {get;set;}
Drives Property System.Collections.ObjectModel.Collection[System.Management...
HelpFile Property string HelpFile {get;}
Home Property string Home {get;set;}
ImplementingType Property type ImplementingType {get;}
Module Property psmoduleinfo Module {get;}
ModuleName Property string ModuleName {get;}
Name Property string Name {get;}
PSSnapIn Property System.Management.Automation.PSSnapInInfo PSSnapIn {get;}

So in order to correctly use Where-Object here, the comparison should be made to Provider.Name property instead, here is what the successful Where-Object filter script looks like:

Get-PSDRive | Where-Object {$_.Provider.Name -eq 'FileSystem'}

An easier and more correct form of only listing the FileSystem drives on your system would be by using the -PSProvider parameter of the Get-PSDrive cmdlet. By using this we can avoid using the pipeline and simplify the code even further:

Get-PSDRive -PSProvider FileSystem
Share