ok, here is the final script
Extend VMDK and Windows Drive inside the VM with Parameter of VMname and Driveletter
In my case it was working, just the check Datastore Freespace thing is not done yet
what do you think?
Param(
$DeineVM = (Read-Host "Select VMname"),
$Drive = (Read-Host "Select Drive Letter")+":"
)
$SystemFQFDN = $DeineVM #Subscribe the Published Data of the affected VM
$UserName = 'administrator' # Subscribe your Variable for User with access to VCenter and VM
#getting the Laps pw
$pw = get-adcomputer $SystemFQFDN -Properties * | select @{Label="Name";Expression={$_.name}}, @{Label="OS";Expression={$_.operatingsystem}}, @{Label="Distinguished name";Expression={$_.'distinguishedname'}}, @{Label="Password";Expression={$_.'ms-Mcs-AdmPwd'}} | select Password
$Password = $pw # Subscribe your encrypted Variable for the Password
#VName equals Netbios name here
$VMName = ($SystemFQFDN.Split('.'))[0]
#Make Credentials
$SecureStringPwd = $Password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecureStringPwd
Write-Host " invokeWMI $vmname" -ForegroundColor Green
#$invokescript = " ipconfig /all"
$MyScript = @"
((Get-WmiObject -ClassName 'Win32_LogicalDisk' |
WHERE {`$_.DeviceID -eq "$Drive"}).GetRelated('Win32_DiskPartition')).GetRelated('Win32_DiskDrive') |
Select-Object -Property SCSIBus,SCSITargetId,SerialNumber |
ConvertTo-Csv
"@
$Windrive = Invoke-VMScript -vm $vmname -ScriptType Powershell -ScriptText $myScript -GuestUser $UserName -GuestPassword $pw.password |
Select -ExpandProperty ScriptOutput |
ConvertFrom-Csv -UseCulture
$Windrive
#$outputarray = ($Windrive -split('\n'))
$SCSIBus = $Windrive.SCSIBus
$SCSITargetId =$Windrive.SCSITargetId
$DiskSNfromWMI = $Windrive.SerialNumber
# Match the VMWare Disk with the windows Disk
$vm = Get-VM -Name $VMName
foreach($ctrl in Get-ScsiController -VM $vm)
{
foreach($disk in (Get-HardDisk -VM $vm | where{$_.ExtensionData.ControllerKey -eq $ctrl.Key}))
{
IF (($disk.ExtensionData.UnitNumber -eq $SCSITargetId )-and (($disk.ExtensionData.Backing.Uuid).Replace('-','') -eq $DiskSNfromWMI) )
{
# The below Variables are for the tab Published Data of "Run .Net Script" Activiy
$VMDiskName = $disk.Name
$VMDiskFilename = $disk.Filename
$DiskUID = $disk.Uid
$DiskCapacityGB = $disk.CapacityGB
$DiskFreeSpaceGB = ((Get-VMGuest -VM $vm).Disks | where {$_.Path -eq "$Drive\"}).FreeSpaceGB
$DiskFreeSpaceGB = "{0:N3}" -f ([math]::Round($DiskFreeSpaceGB,3))
}
}
}
$LUN = Get-HardDisk -vm $vm | Where {$_.Uid -eq $DiskUID} | Get-Datastore
$LUNName = $LUN.Name
$FreespaceGB = "{0:N3}" -f ([math]::Round($LUN.FreeSpaceGB,3))
$CapacityGB = "{0:N3}" -f ([math]::Round($LUN.CapacityGB,3))
##################
#Driveletter will be extended with 10GB
$extend10GB = "10"
$newsize = $DiskCapacityGB + $extend10GB
# Extend VMDK
Get-HardDisk -vm $vm | Where {$_.Name -eq "$VMDiskName"} | Set-HardDisk -CapacityGB $newsize -ResizeGuestPartition -Confirm:$false
#rescan
Invoke-VMScript -vm $VM -ScriptText "echo select vol $drive > c:\diskpart.txt && echo rescan >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt" -ScriptType BAT -GuestUser $UserName -GuestPassword $pw.password
#Extend Disc inside
Invoke-VMScript -vm $VM -ScriptText "echo select vol $drive > c:\diskpart.txt && echo extend >> c:\diskpart.txt && diskpart.exe /s c:\diskpart.txt" -ScriptType BAT -GuestUser $UserName -GuestPassword $pw.password