Powershell | Imagination is power

Hi guys,

Previously created post to transfer the file into VM got many likes and comments but I think you will love this way of working with powershell.

You can use this method in many use cases. My use case was-

- transfer a script(Powershell script) into around 1500 VMs
- run that script through task scheduler and it should run in all those 1500 VMs

Sounds crazy!? :D

Now, you all are the master for first point and today I am going to share the magic behind second point. For me it was hardly few hours job and I couldn't imagine doing it without powershell like login 1500 VMs one by one and create task scheduler in all those VMs. OMG! Anyone gonna mad doing it. isn' it?

Not only task scheduler, you can use above idea in many areas. For example, in case of Virus attack. It used to happen once, twice or more in a year depending upon the security architecture of your company. So, now what operations teams do in such emergency kind of situation. Operations teams extend their days and nights and patch all the servers by spending their hours in office. isn't it?

Now, we can use this method, to transfer those security patches in multiple machines at the same time and quickly and run those security patches without login the machines to install it. Apart from this, you can also verify whether that has been installed or not without login in any virtual machine.

There can be many more use cases where you can use my idea of working. I am expecting mere comment on my blog if you really like it.

Let's see then, how you can do it-

You need to pay attention to understand what is going on...


In above example, I was having 3 scripts. One is which I ran and two others which I transferred inside VM. Out of these 2, one was my mainscript having all the commands and instructions to do a task and second was to create a task scheduler to run my mainscript.

It could be confusing for you guys when you first try to understand it but scope of such type of task is huge. Feel free to comment out if you need any help of mine.


Script to run-

#Start here

clear

Connect-VIServer vcneter.corp.local

Write-Host "Enter the requested info please" -ForegroundColor Cyan
Function Collectdata{
$source = "C:\vcnotes\temp"
$dest = Read-host "enter the destination folder path here"
$VM = Read-Host "Enter the VM Name"
$user = Read-Host "Enter the username"
$pass = Read-Host "enter password" -AsSecureString
Write-Host "Thanks to provide all the required info. Tell me the desired action" -ForegroundColor Green
DRSCTransfer
}
Collectdata
Function DRSCTransfer {
echo "Press 1 to transfer the file"
echo "Press 2 to create task scheduler"
$choice = read-host "Enter your choice here "
if ($choice -eq 1){transfer}
if ($choice -eq 2){runscr}

}

Function transfer {
Get-Item "$source " | Copy-VMGuestFile -force -Destination "$dest" -VM $VM -LocalToGuest -GuestUser $user -GuestPassword $pass

}

Function runscr{

    $Chpass = @"
    C:\temp\ScheduleTaskCreate.ps1
"@

#below command will create task scheduler as per above .ps1 script which I placed in C:\temp

Invoke-VMScript -VM $VM -ScriptText $Chpass -GuestUser "$user" -GuestPassword "$pass" -ScriptType Powershell

}

#End here

To create task scheduler
#Start here
$action = New-ScheduledTaskAction -Execute 'C:\temp\vcnotes\mainscript'
$trigger =  New-ScheduledTaskTrigger -Daily -At 04:50PM
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "vcnotes.test" -Description "Demo"
#End here

#Mainscript

It depends upon your requirement that what you want to run inside a VM.






Thank you,
vCloudNotes





0 Comments:

Post a Comment