locked
Script is running good when alone. NOT running or failed when its code part of another script. RRS feed

  • Question

  • Hello friends,

    One of my script (Mounting_VHD)with pop-up option Yes/No option is working fine when executed alone. When I put the same code in another script which is also running good is failing when I added the code of Yes/No script option, I am not able to mount the VHD. Can anyone help? Thanks in advance.

    Mounting_VHS script content:

    =======================================================================

    param([switch]$Elevated)

    function Test-Admin {
      $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
      $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }

    if ((Test-Admin) -eq $false)  {
        if ($elevated) 
        {
            # tried to elevate, did not work, aborting
        } 
        else {
            Start-Process powershell.exe -windowstyle "Hidden" -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }

    exit
    }

    function MountingVHD 
    {
    import-module HyperV
    Mount-VHD -Path "H:\Win_VM\win10.vhd"
    Start-Sleep -s 6
    start K:\Users\xyz\Documents\Downloading\Downloaded\Edrive
    }

    $a = new-object -comobject wscript.shell
    $intAnswer = $a.popup("Do you want to Mount the VHD disk?", 0,"Mount Disk",4)
    If ($intAnswer -eq 6) { MountingVHD } 
    Start-Sleep -s 120
    #stop-process -Id $PID

    =========================================================================

    Here is the script that NOT working when the above added:

    =====================================================================

    param([switch]$Elevated)

    function Test-Admin {
      $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
      $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
    }

    if ((Test-Admin) -eq $false)  {
        if ($elevated) 
        {
            # tried to elevate, did not work, aborting
        } 
        else {
            Start-Process powershell.exe -windowstyle "Hidden" -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }

    exit
    }
    #Start-Process powershell -Verb runAs
    import-module HyperV
    Shutdown-VM Win10 -Confirm:$False 

    Start-Sleep -s 60

    Stop-Process  -processname vmconnect

    function MountingVHD 
    {
    import-module HyperV
    Mount-VHD -Path "H:\Win_VM\win10.vhd"
    Start-Sleep -s 10
    start K:\Users\xyz\Documents\Downloading\Downloaded\Edrive
    }

    $a = new-object -comobject wscript.shell
    $intAnswer = $a.popup("Do you want to Mount the VHD disk?", 0,"Mount Disk",4)
    If ($intAnswer -eq 6) { MountingVHD } 
    #stop-process -Id $PID

    ============================================================================

    • Moved by Bill_Stewart Thursday, January 25, 2018 10:11 PM This is not "fix/debug/rewrite my script for me" forum
    Thursday, November 9, 2017 2:20 PM