Need help on Powershell Script RRS feed

  • Question

  • Hi techies,

    I have a Basic knowledge on Powershell scripting. My requirement is something like this: "if i type [Yes] to start the script and [No] exit " how to do this on my script:

    Please guide me how to do this on below script:

    if ((Get-PSSnapin -Name vmware.VimAutomation.core -ErrorAction SilentlyContinue) -eq $null)
    	Write-Host "loading VimAutomation modules loading......." -ForegroundColor	green
    	Add-PSSnapin vmware.VimAutomation.core
    Else {
        Write-Host " Vmware Automation Tools has been loaded already" -ForegroundColor Yellow
    #Variables Provided by User:
    $VIserver =Read-Host "Provide ESXi server or vCenter server Hostname or Ipaddress"
    $VIuser =Read-Host "Provide ESXi server or vCenter Server Hostname username"
    $VIpassword =Read-Host "Provie Esxi or vCenter user password"
    #connect VIserver:
    Write-Host " Connecting to your server $VIserver"
    Connect-VIServer -Server "$VIserver" -User "$VIuser" -Password "$VIpassword"
    #Variables for VMGuest:
    $VMName =Read-Host "Provide Virtaul Machine Name e.g New Virtaul macine" 
    $Source =Read-Host "Provide Source Files localtion e.g D:\test.txt"
    $Destination =Read-Host "Provide Destination Name e.g D:\"
    $GuestUser =Read-Host "Provide Guest user Account e.g administrator"
    $password = Read-Host "Provide Guest user password e.g something"
    #Script for Copying Data:
    Write-Host "Copying in process.....Data trasfering from $Source to $Destination " -ForegroundColor Green
    Copy-VMGuestFile -LocalToGuest -VM $VMName -Source "$Source" -Destination "$Destination" -GuestUser "$GuestUser" -GuestPassword "$password"  -Verbose
    Write-Host "Task Completed............." -ForegroundColor Green

    k sivakumar

    Thursday, July 5, 2012 8:13 AM


  • Put this at the very start of the script:

    if ((Read-Host 'Run script?') -ne 'Yes') { return }

    Grant Ward, a.k.a. Bigteddy

    • Proposed as answer by Yan Li_ Friday, July 6, 2012 2:22 AM
    • Marked as answer by sivaksk Friday, July 6, 2012 5:24 AM
    Thursday, July 5, 2012 1:57 PM