Passing parameters in logon script in GPO failed - windows

As you see in my GPO settings
I have a VBScript and call it as a logon script via GPO in domain controller. The script is running at a logon but the parameters are not passed. Any ideas why not?
On technet they say
In Script Parameters, type the parameters you want to use as you would type them on the command line. For example, if your script included parameters called //logo (display banner) and //I (interactive mode), type: //logo //I.
So it should be possible the way I do it, right?
Because AnsgarWiechers asked:
I check if the script is running by
Set objFsoLog=CreateObject("Scripting.FileSystemObject")
outFileLog="C:\ScriptLog.txt"
Set objFileLog = objFsoLog.CreateTextFile(outFileLog,True)
objFileLog.Write "script wurde wenigstens ausgeführt" & vbCrLf
objFileLog.Close
at the beginning od the script.
And I check the parameters by this:
On Error Resume Next
Err.Clear
MsgBox WScript.Arguments(0)
If Err.Number <> 0 Then
MsgBox Err.Description
End If
On Error Resume Next
Err.Clear
MsgBox WScript.Arguments(1)
If Err.Number <> 0 Then
MsgBox Err.Description
End If
gpresult /h gives the following output for my GPO:
But the script is saved in the logon scripts:

Related

Self-elevating VBS from mapped network drive not working

Here is my sample test code:
On Error Resume Next
Dim localServiceKey : localServiceKey = CreateObject("WScript.Shell").RegRead("HKEY_USERS\s-1-5-19\")
'On error, elevate the script
If Err.Number <> 0 Then
CreateObject("Shell.Application").ShellExecute WScript.FullName, """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
Err.Clear
WScript.Quit()
End If
...INSERT CODE BELOW...
This self-elevating VBS script file works properly if I run it from a local drive or from a network drive accessed using UNC (ie: \\server\folder).
But if I run it from a mapped drive (ie: Z:\folder), I receive an error after the UAC prompt stating:
Can not find script file...
Is there a work around for allowing VBS script file to run from a mapped network drive?
NOTE: I'm testing this code on Windows 10 x64.
Thanks for any advice.

Restart a vbs script if it crashes

I'm trying to make a vb script that will restart another vb script if it crashes.
I have searched, and searched but all I get is how to restart a program and since a vb script is a background process it doesn't work when you search in Win32_Process.
Here is my code
set Service = GetObject ("winmgmts:")
set Shell = WScript.CreateObject("WScript.Shell")
sEXEName = "Test_To_Block.vbs"
while true
bRunning = false
for each Process in Service.InstancesOf ("Win32_Process")
if Process.Name = sEXEName then
bRunning=true
msgbox("I am active")
End If
next
if bRunning=False then
msgbox("I am not active.")
Shell.Run sEXEName
end if
WScript.Sleep(100)
wend
The problem is that it never see's the file running and just opens hundreds of "Test_To_Stop.vbs"'s which resolves in me having to restart the computer.
In my opinion what should be changed is where the code is looking for.
for each Process in Service.InstancesOf ("Win32_Process")
Instead of looking in "Win32_Process" you need to look in wherever background process' run.
I am new to coding so sorry if this is a simple question.
Thank you in advance.
Regards,
A Viper
The below code restarts itself via WshShell.Exec() method and trace state of the running script via .Status property of returned object:
If Not WScript.Arguments.Named.Exists("task") Then
Do
With CreateObject("WScript.Shell").Exec("""" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""")
Do While .Status = 0
WScript.Sleep 1
Loop
End With
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Another way is to use .Run() method with third parameter set to True to wait until launched process terminated:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Or even simplier:
If Not WScript.Arguments.Named.Exists("task") Then
Do
CreateObject("WScript.Shell").Run """" & WScript.ScriptFullName & """ ""/task""", 1, True
Loop
End If
MsgBox "This script will be restarted immediately after termination"
Probably because the name of the running process is 'wscript.exe' and not 'Test_To_Block.vbs'. You may be able to use the hack mentioned on this page to change the name of the process:
If you're running the scripts locally and running some regular scripts, one
common hack is just to copy and rename wscript.exe to a particular name,
such as "MyScript1.exe". Then run the script with a shortcut as
"...\MyScript1.exe MyScript1.vbs". Then the process will show up as
MyScript1.exe.
Then you can use sEXEName = "MyScript1.exe"
Note: instead of using Shell.run sExeName use Shell.run "Test_To_Block.vbs"

No parameter in GPO logon script passed?

Is it possible to run secedit in logon script ?
I wrote a script to add some COM and DCOM settings. To run it by doubble click or via cmd.exe works fine.
But it is not working as a logon script.
MsgBox is also not working at logon ......
Is it because secedit is not working at the start or because I run it via my domain controller ?
EDIT:
secedit isn´t the problem. I added some MsgBoxes to control the script at a logon and it is starting.
But the parameter I added to the GPO in the domain controler are not there. I made this :
On Error Resume Next
Err.Clear
MsgBox WScript.Arguments(0)
If Err.Number <> 0 Then
MsgBox Err.Description
End If
On Error Resume Next
Err.Clear
MsgBox WScript.Arguments(1)
If Err.Number <> 0 Then
MsgBox Err.Description
End If
And the output is "out of range" twice.
So why am I not able to set these two parameters in the GPO like this?

What is the equivalent of a Batch ECHO Command using VBScript?

We currently use Windows Batch (DOS) command files to control our process flow. To display messages to the Console, we would use the ECHO command. These messages would show up in our Scheduler software, which used to be Tivoli and now is CA WA Workstation\ ESP.
I would like to start using VBS files instead of CMD\BAT files and am trying to figure out how to do the equivalent of an ECHO to the console.
When I try to use either the WScript.Echo command or write to Standard Out, the messages are displayed in dialog boxes for both and they require the OK button to be pushed to continue. Not surprisingly, when I run unattended though a scheduler, the job hits one of these commands and just hangs since there is no one to OK the messagebox.
SET FS = CreateObject("Scripting.FileSystemObject")
SET StdOut = FS.GetStandardStream(1)
StdOut.Write("Test 1")
WScript.echo("Test 2")
I realize I could write the messages to a Log file using the Scripting object, but this could fail if an invalid path is provided or because of insufficient permissions. Besides, being able to see feedback write within the Scheduler is awfully convenient.
How do I write to the Console using VBScript? I’ve seen other posts here that suggest that the above methods which didn't work for the reason describe above were the way to do it.
wscript.echo is the correct command - but to output to console rather than dialogue you need to run the script with cscript instead of wscript.
You can resolve this by
running your script from command line like so:
cscript myscript.vbs
changing the default file association (or creating a new file extension and association for those scripts you want to run with cscript).
change the engine via the script host option (i.e. as per http://support.microsoft.com/kb/245254)
cscript //h:cscript //s
Or you can add a few lines to the start of your script to force it to switch "engine" from wscript to cscript - see http://www.robvanderwoude.com/vbstech_engine_force.php (copied below):
RunMeAsCScript
'do whatever you want; anything after the above line you can gaurentee you'll be in cscript
Sub RunMeAsCScript()
Dim strArgs, strCmd, strEngine, i, objDebug, wshShell
Set wshShell = CreateObject( "WScript.Shell" )
strEngine = UCase( Right( WScript.FullName, 12 ) )
If strEngine <> "\CSCRIPT.EXE" Then
' Recreate the list of command line arguments
strArgs = ""
If WScript.Arguments.Count > 0 Then
For i = 0 To WScript.Arguments.Count
strArgs = strArgs & " " & QuoteIt(WScript.Arguments(i))
Next
End If
' Create the complete command line to rerun this script in CSCRIPT
strCmd = "CSCRIPT.EXE //NoLogo """ & WScript.ScriptFullName & """" & strArgs
' Rerun the script in CSCRIPT
Set objDebug = wshShell.Exec( strCmd )
' Wait until the script exits
Do While objDebug.Status = 0
WScript.Sleep 100
Loop
' Exit with CSCRIPT's return code
WScript.Quit objDebug.ExitCode
End If
End Sub
'per Tomasz Gandor's comment, this will ensure parameters in quotes are covered:
function QuoteIt(strTemp)
if instr(strTemp," ") then
strTemp = """" & replace(strTemp,"""","""""") & """"
end if
QuoteIt = strTemp
end function

How to output a list of arguments passed to a VBScript script?

I have the following VBScript code:
Dim returnVal
returnVal = "You did not pass me 4 arguments"
args = WScript.Arguments.Count
If args = 4 Then
returnVal = "The arguements you passed me are " & WScript.Arguments.Item(0) & " " & WScript.Arguments.Item(1) & " " & WScript.Arguments.Item(2) & " " & WScript.Arguments.Item(3)
end if
All I want is the ability to print "returnVal" so that if I typed:
test.vbs 1 2 3 4
It would return:
The arguments you passed me are 1 2 3 4
How can I do this?
To output to the command console window you can do this using:
WScript.Echo returnVal
or
WScript.StdOut.WriteLine returnVal
But you must use the CScript host for this to work, for example:
cscript.exe myscript.vbs
WScript is the GUI host and so has no knowledge of the standard input/output/error/aux streams. Trying to do WScript.StdOut.WriteLine will result in the following error dialogue:
---------------------------
Windows Script Host
---------------------------
Script: d:\myscript.vbs
Line: 12
Char: 1
Error: The handle is invalid.
Code: 80070006
Source: (null)
---------------------------
OK
---------------------------
In a CScript.exe script you can still pop up GUI message dialogues using:
Msgbox "Hello World!"
Using WScript.Echo in a WScript host will display the message in a popup dialogue instead of printing to the command line window.
For more information see:
Write Method (Windows Script Host)
For more information on the differences between WScript and CScript and how to switch between them:
Sesame Script Stop and Go (MS TechNet)
The difference between Cscript and Wscript is that Cscript is the
command-line version of the script host and Wscript is the graphical
version. This difference isn’t really noticeable unless your script
uses the Wscript.Echo command.
If you're not after the Message Box suggested by #heximal, you should use StdOut:
WScript.StdOut.Write(returnVal)
Important: This requires CScript to be the host executable.
To change the default script host, use
cscript //h:cscript //s

Resources