Reputation: 13
I have a problem with a script. I check for firmware type and after that I format the hard drive based on that. The problem is that I get an error when it runs the command
New-Partition -DiskNumber 0 -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' $partsize_param -DriveLetter C
It says
a positional parameter cannot be found that accept argument '-UseMaxiumSize' + categoryinfo : invalidargument: (:) [New-Partition], ParameterBindingException + fullyqualifiederror: PositionalParameterNotFound, New-Partition
The funny thing is that the command runs just fine if I'm using -UseMaximumSize
instead of $partsize_param
Can someone point what is the error that I'm making?
function clean_install_hdd () {
Switch (Get-BiosType) {
1 {$firmwaremode='Legacy BIOS'}
2 {$firmwaremode='UEFI Mode'}
Default {$firmwaremode='Unknown'}
}
Get-Disk
$PartitionSize = Read-Host "Partition size - How many GB or max to use all available space"
if ("$PartitionSize" -eq "max") {
$partsize_param = '-UseMaximumSize'
} else {
$partsize_param = '-Size ' + $PartitionSize
}
if ("$firmwaremode" -eq "Legacy BIOS") {
Clear-Disk 0 -RemoveData -RemoveOEM -Confirm:$false; Initialize-Disk 0 -PartitionStyle MBR -Confirm:$false
New-Partition -DiskNumber 0 -$partsize_param -DriveLetter C -IsActive | Format-Volume -FileSystem NTFS -NewFileSystemLabel Windows -ShortFileNameSupport $False -Confirm:$false
}
if ("$firmwaremode" -eq "UEFI Mode") {
Clear-Disk 0 -RemoveData -RemoveOEM -Confirm:$false; Initialize-Disk 0 -PartitionStyle GPT -Confirm:$false
$systemPart = New-Partition -DiskNumber 0 -GptType '{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}' -Size 100MB -DriveLetter S
& format.com "$($systemPart.DriveLetter):" /FS:FAT32 /Q /Y | Out-Null
New-Partition -DiskNumber 0 -GptType '{e3c9e316-0b5c-4db8-817d-f92df00215ae}' -Size 128MB
Write-Host $partsize_param
New-Partition -DiskNumber 0 -GptType '{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}' $partsize_param -DriveLetter C | Format-Volume -FileSystem NTFS -NewFileSystemLabel Windows -ShortFileNameSupport $False -Confirm:$false
}
}
Upvotes: 1
Views: 7618
Reputation: 174845
You can't substitute a parameter name with a string literal, like so:
$p = "-ParamName"
Get-Stuff $p
The parser will interpret the string "-ParamName" as an argument to a positional parameter. Since no positional parameters can be found, the error is thrown.
The proper way to handle dynamic parameter arguments like in your case, is to use splatting:
$params = @{}
if ("$PartitionSize" -eq "max") {
$params['UseMaximumSize'] = $true
} else {
$params['Size'] = $PartitionSize
}
New-Partition @params
You can put all the named parameter arguments into the hashtable, or you can splat just a partial set of parameters:
if($firmwaremode -eq "Legacy BIOS") {
New-Partition -IsActive @params
} else {
New-Partition @params
}
Upvotes: 2
Reputation: 28194
You can't (easily) use a variable as a parameter name. This is a good situation to use variable splatting. This lets you easily build a dynamic set of parameters in a hash table.
function clean_install_hdd () {
Switch (Get-BiosType) {
1 {$firmwaremode='Legacy BIOS'}
2 {$firmwaremode='UEFI Mode'}
Default {$firmwaremode='Unknown'}
}
Get-Disk
$PartitionSize = Read-Host "Partition size - How many GB or max to use all available space"
$Params = @{
DiskNumber = 0
DriveLetter = "C"
};
if ($PartitionSize -eq "max") {
$Params.Add("UseMaximumSize",$true);
} else {
$Params.Add("Size", $PartitionSize);
}
if ("$firmwaremode" -eq "Legacy BIOS") {
Clear-Disk 0 -RemoveData -RemoveOEM -Confirm:$false; Initialize-Disk 0 -PartitionStyle MBR -Confirm:$false;
$Params.Add("IsActive",$true);
New-Partition @Params | Format-Volume -FileSystem NTFS -NewFileSystemLabel Windows -ShortFileNameSupport $False -Confirm:$false
}
if ("$firmwaremode" -eq "UEFI Mode") {
Clear-Disk 0 -RemoveData -RemoveOEM -Confirm:$false; Initialize-Disk 0 -PartitionStyle GPT -Confirm:$false
$systemPart = New-Partition -DiskNumber 0 -GptType '{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}' -Size 100MB -DriveLetter S
& format.com "$($systemPart.DriveLetter):" /FS:FAT32 /Q /Y | Out-Null
New-Partition -DiskNumber 0 -GptType '{e3c9e316-0b5c-4db8-817d-f92df00215ae}' -Size 128MB
Write-Host $partsize_param
$Params.Add("GptType","{ebd0a0a2-b9e5-4433-87c0-68b6b72699c7}");
New-Partition @Params | Format-Volume -FileSystem NTFS -NewFileSystemLabel Windows -ShortFileNameSupport $False -Confirm:$false
}
}
Upvotes: 3