VMware Cloud Community
mahmn
Enthusiast
Enthusiast
Jump to solution

Accessing an administrative CMD via scripting

Hi

This question is not directly related to PowerCLI and Vmware, however since I am intending to do some powercli scripting, I need that. Also, I hope that similar question exist for developers working with powercli and powershell.

I would like to install AnyDesk remotely on the newly created vm. I have created a Batch file and have put it in C:\ of the new vm (I will call this $RemoteMachine). If I manually login to the remote machine, I have to open a CMD with administrator permission. Now the problem is, when I call that via the powershell from my local machine, it doesn't correctly opens a CMD with admin level, albeit I have enabled the Administrator account and login with that.

The script looks like

$RemoteMachine = "10.1.1.40"
$Username = 'Administrator'
$Password = ' '
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName $RemoteMachine -Credential $Cred -ScriptBlock { c:\installanydesk.bat }

The output is

PS C:\Windows\system32> C:\Users\user\Desktop\anydesk.ps1
Installing AnyDesk...
Access is denied.
+ CategoryInfo : NotSpecified: (Access is denied.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName : 10.1.1.40

ERROR: Input redirection is not supported, exiting the process immediately.
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

 

As you can see, the first line of the output is "Installing AnyDesk..." which is the first line of the batch file. So, the authentication is done correctly. It seems that it hasn't opened a CMD in the administrator mode.

Has anyone faced such an issue? I mean trying to open an elevated CMD using powershell?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

What is defined in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA?
If that is not 0, your session will not start elevated


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

What is defined in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA?
If that is not 0, your session will not start elevated


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

Reply
0 Kudos
mahmn
Enthusiast
Enthusiast
Jump to solution

It is zero on the remote machine.

 

Untitled.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

It looks as if UAC is disbaled.
You can then try to run the code 'elevated'.

There are some options:
 - use the RunAs verb
This Invoke-VMScript example does require the VMware Tools to be installed on the target VM.

$script = ' 
function Elevate-Process  {
param ([string]$exe = $(Throw "Pleave 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" 
$process = [System.Diagnostics.Process]::Start($startinfo)
}

Elevate-Process -Exe powershell.exe -Arguments "-noninteractive -command Get-Process > C:\test.txt"
'

Get-VM MyVM | Invoke-VMScript -ScriptText $script -ScriptType PowerShell

- use PSexec from Sysinternals


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

Reply
0 Kudos
mahmn
Enthusiast
Enthusiast
Jump to solution

I tested EnableLUA  (0 and 1) on a newly created vm to see if I can access the elevated shell and it was fine.

Don't know what was the problem with the machine (in the first post) that didn't work even with EnableLUA=0.

Reply
0 Kudos