Expanding Windows Disk

Welcome Forums General PowerShell Q&A Expanding Windows Disk

This topic contains 8 replies, has 5 voices, and was last updated by

 
Participant
6 months, 3 weeks ago.

  • Author
    Posts
  • #99646

    Participant
    Points: 14
    Rank: Member

    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.

  • #99651

    Participant
    Points: 176
    Helping Hand
    Rank: Participant

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

  • #99654

    Participant
    Points: 14
    Rank: Member

    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.

  • #99663

    Participant
    Points: 0
    Rank: Member

    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 '1.1.1.1'
    $drives[0].Resize(367001600)
    • #99678

      Participant
      Points: 14
      Rank: Member

      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 {
                     $drive.Resize(20971520)
                 } 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.

  • #99664
    js

    Participant
    Points: 219
    Helping Hand
    Rank: Participant

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

    • #99679

      Participant
      Points: 14
      Rank: Member

      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.

  • #99763

    Participant
    Points: 0
    Rank: Member

    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

    • #99892

      Participant
      Points: 14
      Rank: Member

      Thanks, I will definitely try this out.

The topic ‘Expanding Windows Disk’ is closed to new replies.