Why process that terminated by script from PowerCli stuck in "suspended" mode - powershell

When I run a script trough PowerCLI after connecting to a VM, I get a strange behavior of some processes, I'm using the "Invoke-VMScript" command that is running an EXE file (compiled in .Net 4.5) that looks for running process and try to kill them.
For some reason some process doesn't get closed, and stuck in "Suspended" mode. When they stuck in this mode even if I tried to remove them from the task manager I get an error "The operation could not be complete, Access is denies."
I'm logged in with the Administrator user
The powershell script that I'm using is:
$executeCommand = "call D:\myCleaningProcess.exe $param1";
Invoke-VMScript -VM $vmName -GuestUser $vmUser -GuestPassword $vmPass -ScriptText $executeCommand
*When running manually the file "EXE" it's works as expected and the process get killed.
Anyone know why I get this strange behavior?

You need to use the -ScriptType Bat parameter when calling EXEs in this manner. See example 3 for reference: https://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FInvoke-VMScript.html

Related

Scheduled Task Powershell Script - Runs OK as user account, but not as SYSTEM

I'm attempting to run a Powershell script as the SYSTEM account as a scheduled task, and the task runs OK, but the script runs incorrectly because it does not load some Citrix Powershell snap-ins that are needed when run as SYSTEM, but runs OK as my user account.
The script does a check to load the Citrix snap-ins, but those do not appear to work when run as SYSTEM.
if ((Get-PSSnapin "Citrix.Common.Commands" -EA silentlycontinue) -eq $null) {
try { Add-PSSnapin Citrix.* -ErrorAction Stop }
catch { write-error "Error Citrix.* Powershell snapin"; Return }
Is there anything special I need to do to get those Snap-ins loaded correctly? I'm calling the script like this, if it matters: powershell.exe -executionpolicy bypass -file C:\path\to\script.ps1.
EDIT: From running (Get-PSSnapin -registered).count as both SYSTEM and my user account, I can see that the snap-ins are loaded correctly, but still can't figure out why the script behaves differently.
OS is Server 2016, version 1607, this is the script: https://gist.github.com/4oo4/85cec464e123d7f2793745e662d6e7ab
This isn't the answer why your specific script doesn't work under the SYSTEM account but explains how you might troubleshoot your (or any other) PowerShell Script under the SYSTEM account.
Give a man a fish, and you feed him for a day. Teach a man to fish,
and you feed him for a lifetime.
The whole thing around this, is that you can actually open a interactive PowerShell command prompt under the SYSTEM account were you probably not aware of.
Run PowerShell as SYSTEM
There are a few ways to start a interactive PowerShell command prompt but probably the easiest one is using PsExec.
Using PsExec
Download
PsTools
Extract PSTools.zip and just copy PsExec into your executable path
Run PowerShell as Administrator (accept the User AccountControl prompt)
In the PowerShell administrator window, give the command: .\PsExec -i -s -d PowerShell
A new PowerShell command window will open:
(Type WhoAmI to confirm the current account)
From here you can troubleshoot the specific script:
Are there any errors when running the specific script?
Does it hang or crash at a specific command?
Are there any differences with running it under a user account?
If it appears that the script runs actually fine in the SYSTEM window, then I would check for any errors in the Task Scheduler:
Select Task Scheduler Local - Task Scheduler Library in the left pane
Select your task in the middle top pane
(Make sure you have Display All Task History in the right pane Enabled)
In the middle bottom pane check the events in the history tab

Check connectivity on virtual machine in vCenter

I am trying to write a small powershell script that checks the health of a newly started server. Here's what I want to do:
Start VM if it's not already started
Wait for Guest Tools to start running
Run the powershell script Test-Connection MyDCServer -Quiet using Invoke-VMScript
Act on whether the check return $true or not.
I have 1. and 2. donem but I am running into trouble on 3. All I get back from Invoke-VMScript is a string presenting the output from the script, when I really want the return value from the script that I invoked.
Is this possible?
I found the solution:
$result = Invoke-VMScript ...
$result.ScriptOutput
$result.ExitCode

Monitor ccmsetup.exe installation status usng powershell

This question is realted to SCCM Client installer ccmsetup.exe. When we run manually ccmsetup.exe; it starts the installation process and exit from command prompt. I cannot monitor the installation process from commandline. It starts recording installation status in ccmsetup.log file. However time to execute completely varies per system.
How can I monitor ccmsetup.exe installation status using powershell.
I am using command as below:
Invoke-Command { C:\Client\ccmsetup.exe /Source:"C:\Client" SMSSITECODE=PPR FSP=Server1.ADDOMAIN.COM}
Thanks & regards,
Kedar
Give a shot to the start-process cmdlet :
start-process C:\Client\ccmsetup.exe -ArgumentList #( '/Source:"C:\Client"' , "SMSSITECODE=PPR" , "FSP=Server1.ADDOMAIN.COM" -wait
And tell me it works, because it works for me :)
NB : the use a simple quotes allows double-quotes to be passed to the process launched.

PowerShell: Starting the CLR Failed with HRESULT 8007000e

I'm getting the following error when running PowerShell scripts on a remote server:
Starting the CLR Failed with HRESULT 8007000e
This is basically how I'm running/calling the scripts:
On the local server I'm running a CMD script that calls a PowerShell script to create a remote session to a remote server. In the PowerShell script I also call a CMD script to run on the remote server like so:
$Script = [scriptblock]::create("cd $BuildPath | cmd.exe /c install.cmd $apptype")
The install.cmd script runs on the remote server and calls a PowerShell script that executes a series of tasks.
powershell ./Install.ps1 -BuildNum %BUILDNUM%
After the tasks are complete, the PowerShell script then calls another PowerShell script to run a separate series of tasks. This is when I hit the above error, when the second PowerShell script is called.
This is how the second PS1 script is called from the first PowerShell script:
powershell "& {. $BinToolsSrc\PostInstallValidation.ps1 -BuildNum $BuildNum -Test 'True'; Run-Validation -App $App -AppLoc $AppLoc -Env $Env:ENV -Site $Site -AppPool $AppPool -Config $Config -EnvConfig $EnvConfig -DllPath $DllPath}"
What usually causes the type of CLR error that I'm getting and how do I resolve it?
NOTE: I do not get this error when I run the install script locally on the remote server.
Thanks in advance!
UPDATE: Installing PowerShell 3 on the remote server seems to have solved the problem as it targets the .NET 4.0 runtime.
I too had the same problem because I have changed some path settings in my VScode unknowingly.
I have changed the settings to command prompt which works fine for me now...(This might not be the best solution though).screenshot

Exiting VMware vSphere PowerCLI command prompt?

Is there a script to input to exit VMware vSphere PowerCLI command prompt after executing a set of script for example created of VM.
MY last line of my .ps1 script is shown below, but the exit does not work, after executing my script, the command prompt is still there, unlike windows command prompt as the exit command works but not in powercli.
New-VM -name $vm -DiskMB 10000 -memoryMB 4000
New-CDDrive -VM $vm -ISOPath $win7 -StartConnected:$true -Confirm:$false
$scsiController = Get-HardDisk -VM $vm | Select -First 1 | Get-ScsiController
Set-ScsiController -ScsiController $scsiController -Type VirtualLsiLogicSAS -Confirm:$false
Start-VM -vm $vm
Exit
My hunch is that this has more to do with powershell and little to do with PowerCLI. I'm also guessing that the window closes if you focus it and press 'Enter'. I ran into this when executing some powershell scripts long ago, but never came across a decent fix until this evening. It seems powershell waits on a Readline() call after executing the script.
The solution: include the flag -InputFormat None in your call to powershell.exe. In your case, I'd include it in the call to the PowerCLI executable, it ought to be passed through.
Resources:
This looks like a known issue from Microsoft's tracker.
These two questions reference the same fix:
Powershell and WiX
Powershell and MSDeploy
Please let me know if this works correctly, I'm not on a system with PowerCLI installed currently.
Good luck!