Use to convert an existing PowerShell command to splatting
Splatting is a much cleaner and safer way to shorten command lines without needing to use backtick. This function excepts any command as a string or a scriptblock and will convert the existing parameters to a hashtable and output the fully splatted command for you.
ConvertTo-Splatting [[-Command] <string>] [<CommonParameters>]
ConvertTo-Splatting [[-ScriptBlock] <scriptblock>] [<CommonParameters>]The command string you want to convert to using splatting
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
The command scriptblock you want to convert to using splatting
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
$splatme = @'
Set-AzVMExtension -ExtensionName "MicrosoftMonitoringAgent" -ResourceGroupName "rg-xxxx" -VMName "vm-xxxx" -Publisher "Microsoft.EnterpriseCloud.Monitoring" -ExtensionType "MicrosoftMonitoringAgent" -TypeHandlerVersion "1.0" -Settings @{"workspaceId" =
"xxxx" } -ProtectedSettings @{"workspaceKey" = "xxxx"} -Location "uksouth"
'@
ConvertTo-Splatting $splatme$SetAzVMExtensionParam = @{
ExtensionName = "MicrosoftMonitoringAgent"
ResourceGroupName = "rg-xxxx"
VMName = "vm-xxxx"
Publisher = "Microsoft.EnterpriseCloud.Monitoring"
ExtensionType = "MicrosoftMonitoringAgent"
TypeHandlerVersion = "1.0"
Settings = @{ "workspaceId" = "xxxx" }
ProtectedSettings = @{ "workspaceKey" = "xxxx" }
Location = "uksouth"
}
Set-AzVMExtension @SetAzVMExtensionParam
$splatme = {
Copy-Item -Path "test.txt" -Destination "test2.txt" -WhatIf
}
ConvertTo-Splatting $splatme$CopyItemParam = @{
Path = "test.txt"
Destination = "test2.txt"
WhatIf = $true
}
Copy-Item @CopyItemParam
$splatme = {
Get-AzVM `
-ResourceGroupName "ResourceGroup11" `
-Name "VirtualMachine07" `
-Status
}
ConvertTo-Splatting $splatme$GetAzVMParam = @{
ResourceGroupName = "ResourceGroup11"
Name = "VirtualMachine07"
Status = $true
}
Get-AzVM @GetAzVMParam
