Friday, August 31, 2012

HyperV Expand Disk powershell

Expand Disk

# Assumes that there is only 1 'disk' and 1 object
$TVM = get-vm | where-object { $_.vmelementname -like "*wallace1"}
#Shut down server so that we can expand disk
shutdown-vm  $TVM -wait
#locate the disk
$TDisk=$TVM | get-vmdisk | where-object{ $_.drivename -eq "Hard Drive"}
#Expand the disk
Expand-VHD $TDisk.diskpath 25gb -wait
#Power server back online 
$TVM | Start-vm -wait

next step is to set it to loop thru each server, then power down, expand the C: drive only to 45GB.


$TVM = get-vm
$TVD = $TVM | get-vmdisk | where-object{ $_.drivename -eq "Hard Drive" -AND $_.Controllername -eq "IDE Controller 0" -AND $_.DriveLUN -eq 0}
$TVM |Foreach-object {Shutdown-vm -wait}
$TVD |Foreach-object {Expand-VHD $_.diskpath 45GB -wait}


Expand Disk on Windows 2012 (of course the commands change)

get-vm | get-vmharddiskdrive | foreach-object {resize-vhd $_.path -sizebyte 80GB}

till next time.

Thursday, August 23, 2012

Powershell - remove user from list of groups


#
# remove user from list of groups
#
#

get-content "C:\app\lols.txt" | % {get-adgroup $_ | remove-adgroupmember  -Member dgoodlif -Credential $myadmin -confirm y}

Friday, August 3, 2012

String Stuff with Powershell UNC

Striping servername from a UNC path
PS> $path = \\chicken\scrambled

PS >$servername=$path.Substring(2,$path.Substring(2).indexof("\"))

PS>$servername
chicken

2,$path.Substring(2).indexof("\")
=
2 # start cut @ position 2
$path.Substring(2) #then search from 2 position
.indexof("\") # search for \ in postion

My regex query to get server name
$path -match "[^/\\\]\[]+"
$matches.Values