1
votes

How do I create a single char variable in PowerShell?

I am using the following script to format new virtual disks added to our Windows 2016 servers. The problem is, Microsoft does not give any examples of what it wants for the -DriveLetter option. I can't figure out what it wants. A single character, I get that, but why is "s" wrong?

$driveletter = "S"
Get-Disk | Where-Object partitionstyle -eq raw |
    Initialize-Disk -PartitionStyle GPT -PassThru |
    New-Partition -AssignDriveLetter -DriveLetter $driveletter -UseMaximumSize |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "App" -Confirm:$false

The error I repeatedly get is:

New-Partition : Invalid Parameter Activity ID: {6fab380c-c0df-4f8d-bdca-f0fed3250238} At line:1 char:22 + ... -Number 2 | New-Partition -AssignDriveLetter -DriveLetter $drivelette ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (StorageWMI:ROOT/Microsoft/Windows/Storage/MSFT_Disk) [New-Partition], CimException + FullyQualifiedErrorId : StorageWMI 5,New-Partition

Once the drive is online I keep trying with:

New-Partition -DiskNumber 2 -AssignDriveLetter -DriveLetter $driveletter -UseMaximumSize |
    Format-Volume -FileSystem NTFS -NewFileSystemLabel "App" -Confirm:$false

To no avail.

Microsoft's documentation on this command says it wants a Char and gives no information on how to give it that.

1
Just as a side-note, I haven't had the best luck with the disk-/drive-oriented cmdlets in Windows 10 powershell and often fall back to diskpart. Can you post your full exception message? If you're trying to pass an explicit char, you can typecast your string: $driveletter = [char]'S'Maximilian Burszley
One problem here is that you are using -DriveLetter and -AssignDriveLetter together. -AssignDriveLetter will automatically assign a drive letter from the first available letter in the pool.AdminOfThings
Updated the error in the post. I really prefer to use powershell if I can because it's easier to integrate with CloudBolt. I don't think diskpart would work for that.Nathan McKaskle
@AdminOfThings Thanks I'll try removing that part.Nathan McKaskle
@AdminOfThings thanks that did the trick, in addition to the previous comment on how to cast as char.Nathan McKaskle

1 Answers

2
votes

The answer was two fold:

Create the char variable like this:

$driveletter = [char]"S"

And secondly, remove the -AssignDriveLetter option as this is redundant and unnecessary with -DriveLetter.