cli | New-Harddisk

When I need to add several temp disk into a vm, I had to click many times. So I decided to use PowerCLI to save my time.
 
After I add a vm into the inventory, I run the codes below.
 
============================================================================================
============================================================================================
Add-PSSnapin "Vmware.VimAutomation.Core"

$vi = Connect-VIServer -Server 192.168.123.252 -User root -Password "Password"

$vm = Get-VM -Name "BASE_08R2_SQL08R2_SS_SQL12_AUDIT" -Server $vi

$running = Get-Datastore -Server $vi -Name running
$snapshot = Get-Datastore -Server $vi -Name snapshot

New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $running
New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $snapshot
New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $running
New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $snapshot
New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $running
New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $snapshot
New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $running
New-HardDisk -vm $vm -Persistence IndependentPersistent -CapacityKB 5000000 -StorageFormat Thin -Datastore $snapshot
============================================================================================
============================================================================================
 
 
After i run the script, I run the codes next. ( Install PSCX first.)
 
============================================================================================
============================================================================================
'<?xml version="1.0" encoding="utf-8" ?>
 <Types>   
  <Type>       
   <Name>System.IO.FileSystemInfo</Name>       
   <Members>           
    <ScriptMethod>               
     <Name>CopyGUI</Name>                
     <Script>
      $name= $args[0]                
      #$target = (read-host "Enter Target: ")               
      $(new-object -com shell.application).NameSpace($name).CopyHere($this.fullname)                      
      trap {"Error Copying"; continue }               
     </Script>           
    </ScriptMethod>           
    <ScriptMethod>               
     <Name>Get_Group</Name>                
     <Script>                 
      $this.GetAccessControl().getgroup([System.Security.Principal.ntaccount])               
     </Script>           
    </ScriptMethod>            
    <ScriptMethod>               
     <Name>Get_GroupSID</Name>                
     <Script>                 
      $this.GetAccessControl().getgroup([System.Security.Principal.securityidentifier])                     
     </Script>           
    </ScriptMethod>            
    <ScriptMethod>               
     <Name>Get_Owner</Name>                
     <Script>                 
      $this.GetAccessControl().getOwner([System.Security.Principal.ntaccount])               
     </Script>           
    </ScriptMethod>            
    <ScriptMethod>               
     <Name>Get_OwnerSID</Name>                
     <Script>                 
      $this.GetAccessControl().getOwner([System.Security.Principal.securityidentifier])                     
     </Script>           
    </ScriptMethod>            
    <ScriptMethod>               
     <Name>Set_Owner</Name>                
     <Script>                
      $SP = $args[0]                 
      $a = $this.GetAccessControl()                
      $a.SetOwner($SP)                
      $this.SetAccessControl($a)               
     </Script>           
    </ScriptMethod>            
    <ScriptMethod>               
     <Name>GetWmi</Name>               
     <Script>
      get-WmiObject CIM_DATAFILE -filter "Name = `"$($this.fullname.replace(`"\`",`"\\`"))`""
     </Script>           
    </ScriptMethod>
    <ScriptMethod>               
     <Name>AddFile_Acl</Name>               
     <Script>
      if($this.gettype().name -eq "fileinfo"){
       trap{Break}
       $ar = New-Object System.Security.AccessControl.FileSystemAccessRule($args[0],$args[1],$args[2])
      
       # check if given user is Valid, this will break function if not so.
 
       $Sid = $ar.IdentityReference.Translate([System.Security.Principal.securityidentifier]) 
       $acl = get-acl $this
 
       if($args[3]){"Before`n`n" + $acl.AccessToString}
 
         $acl.SetAccessRule($ar)
         set-acl $this $acl
         
       if($args[4]){"After`n`n" + (get-acl $this).AccessToString}
      } else
      {
       "not file!"
      }
     </Script>           
    </ScriptMethod>          
   </Members>   
  </Type>
 </Types>' > "$pshome\file.system.security.ps1xml"
update-typedata -prependpath $pshome\file.system.security.ps1xml
Import-Module pscx
$SeRestore=new-object Pscx.Interop.TokenPrivilege "SeRestorePrivilege", $true
Set-Privilege $SeRestore
$SeSecurity=new-object Pscx.Interop.TokenPrivilege "SeSecurityPrivilege", $true
Set-Privilege $SeSecurity
$SeOwner=new-object Pscx.Interop.TokenPrivilege "SeTakeOwnershipPrivilege", $true
Set-Privilege $SeOwner
$sid_num = (Get-WmiObject -Query $("SELECT * FROM Win32_Account where Domain = `"" + $(gc env:computername) + "`"") | ?{$_.name -eq "administrator"}).sid
$sid = New-Object System.Security.Principal.SecurityIdentifier([System.Security.Principal.WellKnownSidType]::BuiltinAdministratorsSid, "$sid_num")
New-Item -Path C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ -Name SetupCache -Force -ItemType directory
New-Item -Path "C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\" -Name Log -Force -ItemType directory
New-Item -Path "C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\" -Name Log -Force -ItemType directory
Stop-Service wuauserv
$folderContent_2_delete_normal = @"
C:\Users\Administrator\AppData\Local\Temp
C:\Windows\SoftwareDistribution
c:\windows\temp
"@
$folderContent_2_delete_normal.Replace("`r`n","`r").split("`r") | %{Remove-Item -Path $(((New-Object system.io.directoryinfo $_).fullname -replace "\\$","")+ "\*") -force -Recurse}
$log = @(dir -Path "c:\" -Filter "*.log" -Recurse -Force -ErrorAction silentlycontinue)
$log | %{
    $file = New-Object system.io.fileinfo $_.fullname
    if($file.Exists){
        Remove-Item -Path $file.fullname -Force -Recurse
    }
}
"C:\Windows\Logs\CBS\cbs.log", "C:\Windows\WindowsUpdate.log", "C:\Windows\SoftwareDistribution\DataStore\Logs\edb.log" | %{
    "" | Out-File $_ -Force
}
============================================================================================
============================================================================================
 
At last, I can just mount those disks.^^
 
============================================================================================
============================================================================================
select disk 1
ATTRIBUTES DISK CLEAR READONLY
online disk
CREATE PARTITION PRIMARY
select partition 1
format fs = ntfs quick
assign mount "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\SetupCache"
select disk 2
ATTRIBUTES DISK CLEAR READONLY
online disk
CREATE PARTITION PRIMARY
select partition 1
format fs = ntfs quick
assign mount "C:\Program Files\Microsoft SQL Server\100\Setup Bootstrap\Log"
select disk 3
ATTRIBUTES DISK CLEAR READONLY
online disk
CREATE PARTITION PRIMARY
select partition 1
format fs = ntfs quick
assign mount "C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\Log"
select disk 4
ATTRIBUTES DISK CLEAR READONLY
online disk
CREATE PARTITION PRIMARY
select partition 1
format fs = ntfs quick
assign letter e
select disk 5
ATTRIBUTES DISK CLEAR READONLY
online disk
CREATE PARTITION PRIMARY
select partition 1
format fs = ntfs quick
assign mount "C:\Users\Administrator\AppData\Local\Temp"
select disk 6
ATTRIBUTES DISK CLEAR READONLY
online disk
CREATE PARTITION PRIMARY
select partition 1
format fs = ntfs quick
assign mount "C:\Windows\SoftwareDistribution"
select disk 7
ATTRIBUTES DISK CLEAR READONLY
online disk
CREATE PARTITION PRIMARY
select partition 1
format fs = ntfs quick
assign mount "c:\windows\temp"
============================================================================================
============================================================================================
Comments