VMware Cloud Community
sachingaikwad
Enthusiast
Enthusiast
Jump to solution

Invoke-VMScript - run command prompt/powershell in foreground

My requirement is to register a VM as a Appium node. For this to happen, I am running the below command but the powershell is running in background due to which I am not able to see the logs. I was able to confirm that the process is running by netstat -ano| findstr :4723.

Is there a way to run the powershell in foreground? Tried setting false to `$startinfo.CreateNoWindow" but still the process is running in background. Here is the command which I am using

$ScriptBlock = @"
function Elevate-Process {
param ([string]`$exe = `$(Throw `"Please provide the name and path of an executable`"),[string]`$arguments)
`$startinfo = new-object System.Diagnostics.ProcessStartInfo
`$startinfo.FileName = `$exe
`$startinfo.Arguments = `$arguments
`$startinfo.verb = `"RunAs`"
`$startinfo.UseShellExecute = `"False`"
`$startinfo.CreateNoWindow = `"False`"
`$process = [System.Diagnostics.Process]::Start(`$startinfo)
}
Elevate-Process -Exe powershell.exe -Arguments `"appium --nodeconfig $vm_home\gridConfigJSON\AppiumNodeWindows.json -p 4723`"
"@

ECHO $ScriptBlock

Invoke-VMScript -VM $TARGET_VM -ScriptText $ScriptBlock -GuestUser $vm_user -GuestPassword $vm_pass

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not sure why you copy the script with Copy-VMGuestFile.
Try something like this

$TARGET_VM = 'vmName'

$vm_user='user'

$vm_pass = 'pass'


$code = @'

`$sAction = @{

   Execute = 'Powershell.exe'

   Argument = '-noexit -windowstyle normal -command appium --nodeconfig $vm_home\AcceptanceTestFramework\src\main\resources\gridConfigJSON\AppiumNodeWindows.json -p 4723'

}

`$action = New-ScheduledTaskAction @sAction

`$trigger = New-JobTrigger -Once -At '$((Get-Date).AddSeconds(10))'

`$principal = New-ScheduledTaskPrincipal -UserID $vm_user -LogonType ServiceAccount -RunLevel Highest

`$sTask = @{

   Action = `$action

   Trigger = `$trigger

   Principal = `$principal

   TaskName = 'Run Appium node'

   Description = 'Register Appium node to Hub'

}

Register-ScheduledTask @sTask

'@


$sInvoke = @{

    ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)

    ScriptType = 'Powershell'

    VM = $TARGET_VM

    GuestUser = $vm_user

    GuestPassword = $vm_pass

    Verbose = $true

}

Invoke-VMScript @sInvoke


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

No, that is not possible with Invoke-VMScript afaik.

What you could try is to create a Scheduled Task with the option "Run only when user is logged on".
You can use Invoke-VMScript to create a Scheduled Task on a station.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sachingaikwad
Enthusiast
Enthusiast
Jump to solution

Thanks LucD​. Can we run scheduled task on-demand? I tried this script by referring your answer Re: scheduling_powershell script_task scheduler but getting errors. Please suggest.

$vm_home="C:\Users\XRAY"

$action = "New-ScheduledTaskAction -Execute `"powershell.exe`" -Argument `"appium --nodeconfig $vm_home\AcceptanceTestFramework\src\main\resources\gridConfigJSON\AppiumNodeWindows.json -p 4723`""

$trigger = "New-JobTrigger -Once"

$sTask = @{

   Action = $action

   Trigger = $trigger

   User = $vm_user

   Password = $vm_pass

   TaskName = 'Run Appium node'

   Description = 'Register Appium node to Hub'

   RunLevel = 'Highest'

}

$script = "Register-ScheduledTask @sTask"

Invoke-VMScript -VM $TARGET_VM -ScriptText $script -GuestUser $vm_user -GuestPassword $vm_pass

Output:

|  Register-ScheduledTask : Cannot validate argument on parameter 'TaskName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You're setting up the hash table on the calling side, while you are calling the Scehduled Task cmdlet on the target Guest OS.

The set up of the hash table should also happen inside the Guest OS (as part of the script you run via Invoke-VMScript)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
sachingaikwad
Enthusiast
Enthusiast
Jump to solution

Based on the suggestion, this is what I did.

Created RegisterAppiumNodeTask.ps1 file with below contents:

$vm_user="user"

$vm_pass="pass"

$vm_home="C:\Users\XRAY"

{

$sAction = @{

   Execute = 'Powershell.exe'

   Argument = '-command appium --nodeconfig $vm_home\AcceptanceTestFramework\src\main\resources\gridConfigJSON\AppiumNodeWindows.json -p 4723'

}

$action = New-ScheduledTaskAction @sAction

$trigger = New-JobTrigger -Once

$sTask = @{

   Action = $action

   Trigger = $trigger

   User = $vm_user

   Password = $vm_pass

   TaskName = 'Run Appium node'

   Description = 'Register Appium node to Hub'

   RunLevel = 'Highest'

}

Register-ScheduledTask @sTask

}

Then copied it to VM and executed Invoke-VMScript command

Copy-VMGuestfile -Source "/Users/sgaikwad/RegisterAppiumNodeTask.ps1" -destination $vm_home  -VM -LocalToGuest $TARGET_VM -GuestUser $vm_user -GuestPassword $vm_pass -force -ErrorAction SilentlyContinue

$script = "$vm_home\RegisterAppiumNodeTask.ps1"

Invoke-VMScript -ScriptText $script -VM $TARGET_VM -GuestUser $vm_user -GuestPassword $vm_pass -ErrorAction SilentlyContinue

As part of the ScriptOutput, whole contents of RegisterAppiumNodeTask.ps1 is getting printed and Powershell window is not opened and the process is also not created. netstat -ano| findstr :4723 gives no processes.

Please suggest.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure why you copy the script with Copy-VMGuestFile.
Try something like this

$TARGET_VM = 'vmName'

$vm_user='user'

$vm_pass = 'pass'


$code = @'

`$sAction = @{

   Execute = 'Powershell.exe'

   Argument = '-noexit -windowstyle normal -command appium --nodeconfig $vm_home\AcceptanceTestFramework\src\main\resources\gridConfigJSON\AppiumNodeWindows.json -p 4723'

}

`$action = New-ScheduledTaskAction @sAction

`$trigger = New-JobTrigger -Once -At '$((Get-Date).AddSeconds(10))'

`$principal = New-ScheduledTaskPrincipal -UserID $vm_user -LogonType ServiceAccount -RunLevel Highest

`$sTask = @{

   Action = `$action

   Trigger = `$trigger

   Principal = `$principal

   TaskName = 'Run Appium node'

   Description = 'Register Appium node to Hub'

}

Register-ScheduledTask @sTask

'@


$sInvoke = @{

    ScriptText = $ExecutionContext.InvokeCommand.ExpandString($code)

    ScriptType = 'Powershell'

    VM = $TARGET_VM

    GuestUser = $vm_user

    GuestPassword = $vm_pass

    Verbose = $true

}

Invoke-VMScript @sInvoke


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

sachingaikwad
Enthusiast
Enthusiast
Jump to solution

Thank you so much LucD

Reply
0 Kudos