Tuesday, July 9, 2013

Qvod Cache !mv files to rmvb ( Qvod快播(安卓)下载文件(!mv)合并方法)

If you have been using Andriod Qvod player, you will be a little bit frustrated if you want to move the downloaded file from your phone to a computer or other media player.

Qvod is a very fantastic application in Andriod which allows you to download Peer to Peer files very easily.

While there are a lot of Qvod cacher conversion program in the internet, you do not know if they are malware or contains virus.

I written this simple batch file in dos format, the key thing it is doing is to extract the number in the middle of the cache file name, expand that with leading zeros so that the system can sort the file names accordingly.

-----------Start of qvod.bat--------------
ren *.!mv *.

for /f "tokens=1,2 delims=_ " %%i in ('dir/b') do (


setlocal ENABLEDELAYEDEXPANSION


set ttt=000000000%%j
set ttt=!ttt:~-5!
ren %%i_%%j !ttt!.rmv
endlocal

)
)

setlocal ENABLEDELAYEDEXPANSION
set tttt=copy/b
for /f %%u in ('dir/b *.rmv') do (
set tttt=!tttt! %%u +
)

%tttt:~0,-1% result.rmvb

endlocal
-----------End of qvod.bat--------------

View PowerCli Script: Automate linked clone master (base image) OS update and recompose

I believe this is one of the nightmare of View Administrators. When you have a lot of View Pool, the effort to maintain the master image is going to be tough.

While you can use ThinApp to simplify the application management, there are some occassion you cannot use ThinApp, you may have to manually update OS patches or application in the master image and do recompose to keep your link clone updated.

This process is tedious and I have two scripts to automate the OS patches update.
  1. Script to trigger Windows Update automatically on all master (or base image) that need update.
  2. Script to create a new snapshot, delete the old snapshots except the last snapshot and trigger recompose of the link clone pool.
The first script should be run first and let it run for a few hours to ensure all installation and reboot are completed before we go to step 2 to generate snapshot and link clone.

Script to trigger Windows Update automatically on all master (or base image) that need update.

The first script (OSupdate.ps1) is create to run the windows update script on all VM required.

Please visit my previous post and save that files (update.bat and wua.vbs) in the same directory as OSupdate.ps1

Prerequisite

To run the OSupdate.ps1 script, we have the following assumption.
  • All VM to be updated by the script has the same username and password with local administrator right. If there are a few different username/password, you could group those VMs with same username/password.
  • During the creation of those master image, the Windows update agent should be updated to at least 7.4.7600.226 and XP should be at least SP3.
  • All VMs should be able to access Windows Update server in Internet or the agent should be configured to pointing to any internal WSUS server.
  • All VMs are in the same folder
Basically, this script will be go through all VM in the folder specified, copy the update.bat and wua.vbs to c:\ and then run the update.bat. If the VM is powered off, it will be powered on by the script automatically.

Since the windows update are different every month, it may take a very long time for your script to download and install all patches, therefore, please make sure you have enough time to run this before you do the link clone recompose.

Here's the OSupdate.ps1 script, there are a few parameters here..
  1. Vcenter - that's the Vcenter address
  2. Folder - that's the folder contains all VMs to be updated.
  3. Admin - That's the username with local admin right on the VM operating system.
  4. Password - That's the password of the account with local admin right on the VM operating system. 
Usage:

./OSupdate.ps1 -vcenter VCENTER.DOMAIN.COM -folder "VMFolders" -admin Administrator -password Password


------------Start of OSupdate.ps1--------------

param ($vcenter, $folder, $admin, $password)

connect-viserver -server $vcenter
$VM = Get-Folder $folder | Get-VM

foreach ($v in $VM) {

if ($v.powerstate -eq "PoweredOff") {
start-VM -VM $v
start-sleep -s 180

}

Copy-VMGuestFile -Source update.bat -Destination c:\ -VM $v -LocalToGuest -GuestUser $admin -GuestPassword $password

Copy-VMGuestFile -Source wua.vbs -Destination c:\ -VM $v -LocalToGuest -GuestUser $admin -GuestPassword $password

Invoke-VMScript "c:\update.bat" -vm $v -GuestUser $admin -GuestPassword $password -ScriptType "bat"

}

--------------End of OSupdate.ps1--------------


 Script to recompose Link clone pool

The second script is to recompose the link clone pool.

Here's the parameter of the script
  1. pool_id - the ID of the link clone pool
  2. parentvm - the name of the VM of the master image
  3. delay - the number of minutes delay you wants the recompose to happen (this allows you to run this script for all VMs at the same time but the actual recompose can happens at different time to avoid IOPS storm)
  4. vcenteraddy - the full address of the Vcenter
  5. folder - the VM folder name of the master image

Usage:

./recompose.ps1 -pool_id "VM1" -vcenter VCENTER.DOMAIN.COM -folder "VMFolders" -parentvm "VMDesktop" -delay 10


------------Start of recompose.ps1-------------


param ($pool_id, $parentvm, $delay, $vcenteraddy, $folder) 

#User set variables

#Connect to vcenter and set some other variables
Connect-VIServer $vcenteraddy
Get-Datacenter | set-Variable -name datacenter

write-host $datacenter
$VM = Get-Folder $folder | Get-VM -Name $parentvm

$p = $parentvm
$Snap = Get-Snapshot -VM $VM -WarningAction SilentlyContinue | Sort-Object Created
$count = 0
foreach ($s in $Snap){
$count = $count + 1
Write-host $count
Write-host $s.name
}


foreach ($s in $Snap){
if ($count -gt 1) {
$k = " Removing " + $s.name
Write-host $k
Write-host $count
Remove-Snapshot -Snapshot $s -confirm:$false

}
$count = $count - 1
Write-Host $count

}

$p = get-date.Year + get-date.Month + get-date.Day + get-date.Hour + get-date.minute + get-date.second

Write-Host $p
Get-VM $VM | New-Snapshot -Name $p
Write-Host "New Snapshot created"


$snapshots = Get-Snapshot -VM $VM -WarningAction SilentlyContinue | select VM, Name, @{Name="Age";Expression={((Get-Date)-$_.Created).Hours}} | Sort-Object Age
$parentsnapshotpath = ""
foreach ($s in $snapshots){
$parentsnapshotpath = "/" + $s.Name + $parentsnapshotpath
}
write-host $parentsnapshotpath

get-composerdomain | Get-DesktopVM -pool_id $pool_id | Send-LinkedCloneRecompose -schedule ((Get-Date).AddMinutes($delay)) -parentvmpath /$datacenter/vm/$folder/$parentvm -parentsnapshotpath $parentsnapshotpath -forceLogoff:$true -stopOnError:$true

update-automaticlinkedclonepool -pool_id $pool_id -parentSnapshotPath $parentsnapshotpath
Write-Host "Pool recomposed sucessfully."


-------------End of Recompose.ps1-------------


Here's some of my reference
http://terenceluk.blogspot.hk/2012/03/updating-a-vmware-view-50-automatic.html

Tuesday, July 2, 2013

View 5.1 PowerCLI powershell script to automate the Link Clone creation

Here's a script we used to create hundred of VMWare View link clone pool for our VDI project. While the user interface of View Administrator is very good for creating link clone, it becomes a nightmare when you need to create 100+ VM pools

This script basically do the following:-

  1. Remove all old the snapshot of the Base Image except the latest snapshot
  2. Create a new snapshot on the Base Image for the link clone. The name of the snapshot is the current date and time.
  3. Create a new link clone pool using the latest snapshot (just created) using the following name as parameters
    • $cluster - Cluster
    • $pool_id - Pool ID 
    • $displayname - Display Name
    • $datastore - The Datastore for storing the Link Clone
    • $minimumcount - Minimum number of VMs
    • $maximumcount - Maximum number of VMs
    • $vcenteraddy - The full path of the Vcenter Server
    • $folder - the folder of the base image (we use the same location to keep the Link clone folder)
    • $ou - the Organizational unit in the Active Directory
    • $entitlementName - the group name in Active Directory that are entitled to this group
    • $parentvm - the VM name of the base image
  4. There are a few policy has been set by default and you could modify that in the first section of the script.
    • $headroomCount = 2 
    • $refreshpolicytype = "Never"
    • $deletepolicy = "DeleteOnUse" 
    • $powerpolicy = "AlwaysOn" 
    • $autologofftime = 540 
    • $IsUserResetAllowed = $true
    • $nameprefix = $pool_id + "{n:fixed=2}"
  5. Pre-requisite for this script to work for you
    • You are happy to have the LC folders under the same folder of your Base Image are contained.
    • All base image should be turned off and do ipconfig /release before doing so.
    • You should have planned all the parameters e.g datastore, clusters,...etc on a spreadsheet for all the pools, then you can use a simple formula in Excel to create the script.
  6. Here's an example of using this script (assume this script is saved as LC.ps1)
./LC.ps1 -cluster "ClusterA" -pool_id "VMD-1" -displayname "VM testing pool 1" -datastore "Datastore1" -minimumcount 2 -maximumcount 5 -vcenteraddy "vcenter.server.com" -folder "Pilot" -OU "OU=VDI" -entitlementName "VMUsers Group" -parentvm "VMDesktop"


Here's the detail of the script. Enjoy!!!

------Start of the scripts--------



param ($cluster, $pool_id, $displayname, $datastore, $minimumcount, $maximumcount, $vcenteraddy, $folder, $OU, $entitlementName, $parentvm)  




#Connect to vcenter and set some other variables
Connect-VIServer $vcenteraddy
Get-Datacenter | set-Variable -name datacenter

$headroomCount = 2 
$refreshpolicytype = "Never" 
$deletepolicy = "DeleteOnUse" 
$powerpolicy = "AlwaysOn" 
$autologofftime = 540 
$IsUserResetAllowed = $true 
$nameprefix = $pool_id + "{n:fixed=2}"

set-variable -name vmfolderpath -value /$datacenter/vm/$folder


$VM = Get-Folder $folder | Get-VM -Name $parentvm

$p = get-date.Year + get-date.Month + get-date.Day + get-date.Hour + get-date.minute + get-date.second 

Get-Snapshot -VM $VM -WarningAction SilentlyContinue | select Id, Created | Sort-Object Created -descending
$Snap = Get-Snapshot -VM $VM -WarningAction SilentlyContinue | Sort-Object Created -descending

$count = 0

foreach ($s in $Snap){

if ($count -gt 0) {

Write-host "Removing Snapshot"
Remove-Snapshot -Snapshot $s -confirm:$false

}
$count = $count - 1
Write-Host $count


}

Write-Host $p
Get-VM $VM | New-Snapshot -Name $p
Write-Host "New Snapshot created"


$snapshots = Get-Snapshot -VM $VM -WarningAction SilentlyContinue | select VM, Name, @{Name="Age";Expression={((Get-Date)-$_.Created).Hours}} | Sort-Object Age 

$parentsnapshotpath = ""

foreach ($s in $snapshots){

$parentsnapshotpath = "/" + $s.Name + $parentsnapshotpath

}

write-host $parentsnapshotpath




get-composerdomain | add-automaticlinkedclonepool -pool_id $pool_id -displayname $displayname -nameprefix $nameprefix -vmfolderpath $vmfolderpath -parentvmpath /$datacenter/vm/$folder/$parentvm -resourcepoolpath /$datacenter/host/$cluster/Resources -parentsnapshotpath $parentsnapshotpath -datastorespecs "[Conservative,OS,data]/$datacenter/host/$cluster/$datastore" -persistence Nonpersistent -organizationalUnit $OU -minimumcount $minimumcount -maximumcount $maximumcount -headroomCount $headroomCount -refreshpolicytype $refreshpolicytype -deletepolicy $deletepolicy -powerpolicy $powerpolicy -autologofftime $autologofftime -IsUserResetAllowed $IsUserResetAllowed 

Write-Host "Pool created sucessfully.  Now I will entitle it."

#Add-PoolEntitlement 
$entitlement =  Get-User -name $entitlementName 

Get-Pool -pool_id $pool_id | Add-PoolEntitlement -sid $entitlement.sid

Write-Host "Entitled and building VMs."

Write-Host "THE END"

---------end of the script-----------