Check if user is allowed to use the command prompt.
It is indeed not possible to check on enabled/disabled because the opening of the command prompt will always work, it's just any activity in it that's enabled/disabled.
I used Scott's idea of trying something and see if it works. In my situation, I didn't have the permission to make a file in the directory, so I tried something else.
Solution:
A hidden cmd window is started with a simple echo. (ProcesID (=output) is put in a string) => 2 possible scenario's:
1. If cmd is enabled, it will be closed after this command (due to /c).
2. If disabled, it will stay open, with the msg "The command prompt is disabled by the administrator", waiting for keypress.
Then, a list of tasks with the PID of this cmd is taken and put into a string with the ShellRun function from bburns.km with the change proposed by Pupa Rebbe on stackoverflow.
1. If the PID is in the list, the cmd exists and thus was not allowed (is hanging). The cmd is killed.
2. If the PID is not in the string, it means the cmd was closed as it should have been and thus is enabled.
Here's the code:
' Check if user is allowed to use the command prompt '
' by starting temporary cmd.exe and see if it hangs. '
Function IsCmdAllowed() As Boolean
Dim TaskId As String, TaskList As String
IsCmdAllowed = False
' Start hidden cmd, echo to have something that doesn't harm, keep Taskid. '
TaskId = Shell("cmd.exe /c timeout /t 5 /nobreak echo ""hihi""", vbHide)
' not sure if waiting 0,5 sec is really necessary. '
Application.Wait (Now + TimeValue("0:00:01") / 2)
' See if cmd still exists (check PID) and put data of cmd in string. '
TaskList = ShellRun("Tasklist /fi ""PID eq " & TaskId & """ /nh")
' If The TaskId of cmd is in the string, it still exists'
If Not TaskId = "" And _
InStr(TaskList, TaskId) Then
' Kill the temporary cmd '
Shell ("TASKKILL /PID " & TaskId)
Else
IsCmdAllowed = True
End If
End Function
'Run a shell command, returning the output as a string: see bburns.km. '
Public Function ShellRun(sCmd As String) As String
Dim oShell As Object, oExec As Object
Set oShell = CreateObject("WScript.Shell")
'run command
Set oExec = oShell.Exec(sCmd)
ShellRun = oExec.StdOut.ReadAll
End Function
gpresultcan query the restrictions placed on a system. If something is not restricted, gpresult will not show it. Its up to you to translate this into something you can work with. For example, get the output into a string, then search it for a keyword. – LPChip May 04 '18 at 10:52cmd /cwork ifcmdis disabled? If not, then generate a random string (e.g.,vu7zwyd4), runcmd /c copy nul C:\temp\vu7zwyd4, and check whether the file was created (and delete it if it was). – Scott - Слава Україні May 05 '18 at 21:34