Monthly Archives: August 2015

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.

New script Remove-ScheduledTask in TechNet Script Gallery

One of the things that keeps me busy is maintaining the library of scripts I have made available in the TechNet Script Library. Earlier this week I noticed a request for my Get-ScheduledTask script, a script that gets a list containing the scheduled tasks from either the local machine or a remote system. The request stated if it was possible to create a function that could delete scheduled tasks.

Based on this question I had a look at the Get-ScheduledTask script and decided that it requried an additional property in the custom object that it outputs, the computer name. Using both the computer name and the path of a scheduled task I am able to delete scheduled tasks on both local systems as well as remotely.

Here is an example of Remove-ScheduledTask in combination with Get-ScheduledTask:

Get-ScheduledTask-Remove

.EXAMPLE
.\Get-ScheduledTask.ps1 | Where-Object {$_.State -eq 'Disabled'} | Remove-ScheduledTask -WhatIf

Description
-----------
Get-ScheduledTask will list all the disabled tasks on a system and the Remove-ScheduledTask function will list all the actions that could be taken

For more information or the direct download links of these scripts please refer to the links below. Feel free to leave a comment either here or in the TechNet Script Library.

TechNet Script Gallery
My entries in TechNet Script Gallery
Get-ScheduledTask
Remove-ScheduledTask