Expanding Windows Disk

This is more of a process question than anything else. I am working on a contract to automate some tasks in vSphere (this is a powershell question, bear with me). One particular script I am working on right now allows an admin to feed in a list of servers, and gather information on the hard drives on both the Windows and VMware side of things. So it uses a mixture of Get-CimInstance on a couple of hives (Win32_DiskDrive, Win32_DiskPartition and Win32_LogicalDisk) as well as the built in PowerCLI Get-HardDisk object to show the admin the Drive Letter, Label, Size, Name and VMDK path for each disk on each VM. Then, depending on a couple of factors, the admin can choose to expand the vDisk in vSphere for some or all of the servers. All of this is working just fine.

Typically, after the expansion of the vDisk I would use code such as this to expand the disk in Windows…

    foreach ($server in $targetServers) {
        $opt = New-CimSessionOption -Protocol DCOM
        $cim = New-CimSession -ComputerName $server.Name -Credential $creds -SessionOption $opt -Verbose:$false
        $session = New-PSSession -ComputerName $server.Name -Credential $creds -Verbose:$false

        $MaxSize = Invoke-Command -Session $session -ScriptBlock {
            (Get-PartitionSupportedSize -DriveLetter c).sizeMax
        } -Verbose:$false

        Invoke-Command -Session $session -ScriptBlock {
            Resize-Partition -DriveLetter c -Size $MaxSize

…however this particular customer has turned off PS Remoting on all servers going forward. It still works on some, but will soon enough not work on any.

So my question is what other (preferably native) PS ways can I do the same thing? For some silly reason the customer is just fine with me doing it through PSExec, but I would prefer not to. Is it possible to start the WinRM service remotely, enable Remoting, then disable remotely when done? I have played around with it a bit but have had no joy thus far.

You could always use PsExec just to enable/disable PSRemoting, if you wanted to. :slight_smile:

Yes, as mentioned, I can do that (although it is silly they allow it). Looking for any suggestions on alternatives, keeping that as my last choice.

If you can’t use a CIM session, why not just use WMI directly?

Something like:

$drives = Get-WmiObject -Namespace "root\Microsoft\Windows\Storage" -Class 'MSFT_Partition' -ComputerName ''

Can you still use wsman (-computername or new-cimsession) or dcom (-computername?)

Thanks for the suggestion, Curtis Smith. I did try it that way, like so:

$drives = Get-WmiObject -Namespace "root\Microsoft\Windows\Storage" -Class 'MSFT_Partition' -ComputerName 'myServ01'
    foreach ($drive in $drives) {
       if ($drive.DriveLetter -eq "D") {
           $myDrive = $drive | 
               Select DriveLetter, @{n="FreeSpace";e={[math]::Round($drive.Size/1GB,2)}}
           try {
           } catch {
               Write-Host "Could not Resize"
           $myDrive | Out-GridView -Wait

Doing it this way I can report on the size of the drive just fine, but it does not perform the expansion. No failure or error code though.

js, as wsman relies on the WinRM service (the whole crux of my problem being this service is disabled) that is not an option. I will have to look into dcom more - I have used it for setting permissions, but never to something like this.

Using DCOM with the CIM cmdlets reverts the behaviour to a more WMI cmdlet like experience at least as far as remote access is concerned. If WinRM is disabled then you can’t use the default WSMAN for a CIM session and have to use DCOM.

BUT you’re not actually using the CIM session you create you’re trying to create a remoting session and using that.

Something like this should work

foreach ($server in $targetServers) {
        $opt = New-CimSessionOption -Protocol DCOM
        $cim = New-CimSession -ComputerName $server.Name -Credential $creds -SessionOption $opt -Verbose:$false
        $MaxSize = Get-PartitionSupportedSize -DriveLetter c  -CimSession $cim | 
        select -ExpandProperty SizeMax
        Resize-Partition -DriveLetter c -Size $MaxSize -CimSession $cim

Note that the -Size parameter on Resize-Partition expects a uint64 (unsigned 64bit integer). Check that’s what you get from your first call to Get-PartitionSupportedSize

I’d also add a line to remove the CIM session once you’ve finished with it

Thanks, I will definitely try this out.