I'm running a script that performs multiple checks on a Windows machine before running a program. I'm running a VBScript script that checks things such as patch status, firewall status, and antivirus software status. After the script runs, the information needs to be output so the end user can fix any issues that arise. This needs to be output in a single message box that shows everything that is not compliant on the end users machine.
Right now after the script runs I'm getting multiple boxes for output, and none of them are displaying the names of what needs to be fixed. I'm just getting the information in quotes in the dialog box.
errors = Array(strSUpdate,strFirewallStatus,strProdState)
Dim errorfix
For Each item In errors
set errorfix = errorfix & item & vbnewline
if (errors = "Off") Then
msgbox "Compliance Check results" & vbNewLine & vbNewLine & _
"The following requires attention" & vbNewLine & errorfix & vbNewLine & _
"Press OK to Continue", 0, "Moka 5 Compliance Check"
Else
msgbox "Compliance Check results" & vbNewLine & vbNewLine & _
"Everything looks good." & vbNewLine & vbNewLine & _
"Press OK to Continue", 0, "Moka 5 Compliance Check"
End if
Next
My suggestion is to avoid arrays altogether and use the much more convenient dictionaries instead.
If nothing else you will create much more readable code that way, but they are also a lot more powerful than arrays.
Option Explicit
Dim errors
Set errors = CreateObject("Scripting.Dictionary")
' fill the dictionary troughout your program. Fill in just the
' actionables and don't insert the stuff that's okay.
errors("Firewall Status") = "Off"
errors("This other thing") = "Missing"
If errors.Count = 0 Then
MsgBox "Compliance Check results" & vbNewLine & vbNewLine & _
"Everything looks good." & vbNewLine & vbNewLine & _
"Press OK to Continue", vbOkOnly, "Moka 5 Compliance Check"
Else
Dim item, toFix
For Each item In errors
toFix = toFix & " - " & item & " is " & errors(item) & vbNewLine
Next
Msgbox "Compliance Check results" & vbNewLine & vbNewLine & _
"The following requires attention:" & vbNewLine & _
toFix & vbNewLine & _
"Press OK to Continue", 0, "Moka 5 Compliance Check"
End If
I think you need to get the return value from the msgbox, i.e., tempresponse = Msgbox(""). The link is here
errors = Array(strSUpdate,strFirewallStatus,strProdState)
Dim errorfix, tempresponse
For Each item In errors
set errorfix = errorfix & item & vbnewline
if (errors = "Off") Then
tempresponse = msgbox ("Compliance Check results" & vbNewLine & vbNewLine & _
"The following requires attention" & vbNewLine & errorfix & vbNewLine & _
"Press OK to Continue", 0, "Moka 5 Compliance Check")
Else
tempresponse = msgbox ("Compliance Check results" & vbNewLine & vbNewLine & _
"Everything looks good." & vbNewLine & vbNewLine & _
"Press OK to Continue", 0, "Moka 5 Compliance Check")
End if
Next
As #Tomalak has said, after each iteration you will need to clear the msgbox. Would a popup work better, as it will clear the message after x seconds.
Related
I'd like to check at fixed time intervals (days) if a specific registry key exceeds a certain value.
If yes, then run a batchfile.
Is this possible (maybe with RegScanner ?) / how ?
thx
This is from the sample code in Help on RegistryValueChangeEvent in WMI
Set wmiServices = GetObject("winmgmts:root/default")
Set wmiSink = WScript.CreateObject( _
"WbemScripting.SWbemSink", "SINK_")
wmiServices.ExecNotificationQueryAsync wmiSink, _
"SELECT * FROM RegistryValueChangeEvent " _
& "WHERE Hive='HKEY_LOCAL_MACHINE' AND " _
& "KeyPath='SOFTWARE\\Microsoft\\WBEM\\sCRIPTING' " _
& "AND ValueName='Default Namespace'"
WScript.Echo "Listening for Registry Value" _
& " Change Events..." & vbCrLf
While(True)
WScript.Sleep 1000
Wend
Sub SINK_OnObjectReady(wmiObject, wmiAsyncContext)
WScript.Echo "Received Registry " _
& "Change Event" & vbCrLf & _
wmiObject.GetObjectText_()
End Sub
I'm trying to update the legal caption on our PCs using a VBScript. So far, I've been able to read values but I can't seem to get it to write any values. I don't get an error when I run the script, it just doesn't change anything. It's the first time I'm doing this and I have limited experience; any insight would be appreciated:
Dim objShell
Dim strMessage, strWelcome, strWinLogon
' Set the string values
strWelcome = "legalnoticecaption"
strMessage = "did this work"
strWinLogon = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"
' Create the Shell object
Set wshShell = CreateObject("WScript.Shell")
'Display string Values
Wscript.Echo "key to update: " & strWelcome
Wscript.Echo "key value to enter: " & strMessage
Wscript.Echo "Existing key value: " & wshShell.RegRead(strWinLogon & strWelcome)
' the crucial command in this script - rewrite the registry
wshShell.RegWrite strWinLogon & strWelcome, strMessage, "REG_SZ"
' Did it work?
Wscript.Echo "new key value: " & wshShell.RegRead(strWinLogon & strWelcome)
set wshShell = Nothing
NOTE: These are testing values at the moment.
Your script seems to be bug-less. However, launched by cscript 28416995.vbs returns next error (where 22 = WshShell.RegWrite line):
28416995.vbs(22, 1) WshShell.RegWrite: Invalid root in registry key "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\legalnoticecaption".
It's not invalid root, it's something like access denied rather because writing to HKLM requires elevated privileges (or run as administrator).
Note:
You should change LegalNoticeText value together with LegalNoticeCaption one.
Under the HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ registry key there both values reside as well. For this case (if a computer is not connected to a domain or with group policy disabled) should work next script.
Run as administrator:
option explicit
On Error Goto 0
Dim wshShell
Dim strResult, strMessage, strWelcome, strWinLogon, strWinLog_2, strWinLTxt
strResult=Wscript.ScriptName
' Set the string values
strWinLTxt = "legalnoticetext"
strWelcome = "legalnoticecaption"
strMessage = "did this work"
strWinLogon = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\"
strWinLog_2 = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"
' Create the Shell object
Set wshShell = CreateObject("WScript.Shell")
'Display string Values
' continue execution if requested registry values not present
On Error Resume Next
strResult = strResult & vbNewLine & "Existing Caption Policies: " _
& wshShell.RegRead(strWinLog_2 & strWelcome)
strResult = strResult & vbNewLine & "Existing Text Policies: " _
& wshShell.RegRead(strWinLog_2 & strWinLTxt)
On Error Goto 0
strResult = strResult & vbNewLine & "Existing Caption Winlogon: " _
& wshShell.RegRead(strWinLogon & strWelcome)
strResult = strResult & vbNewLine & "Existing Text Winlogon: " _
& wshShell.RegRead(strWinLogon & strWinLTxt)
strResult = strResult & vbNewLine
strResult = strResult & vbNewLine & "key to update: " & strWelcome
strResult = strResult & vbNewLine & "key value to enter: " & strMessage
' the crucial command in this script - rewrite the registry
wshShell.RegWrite strWinLogon & strWelcome, strMessage, "REG_SZ"
wshShell.RegWrite strWinLogon & strWinLTxt, UCase( strMessage), "REG_SZ"
' Did it work?
strResult = strResult & vbNewLine
strResult = strResult & vbNewLine _
& "new key Capt. value: " & wshShell.RegRead(strWinLogon & strWelcome)
strResult = strResult & vbNewLine _
& "new key Text value: " & wshShell.RegRead(strWinLogon & strWinLTxt)
Wscript.Echo strResult
set wshShell = Nothing
For me your code run perfect.
For other user that want details over this i recommend this site: http://ss64.com/vb/regread.html and ss64.com/vb/regwrite.html
Both links detail exactly the procedure that you create.
Make sure to add this:
Function RunAsAdmin()
If WScript.Arguments.length = 0 Then
CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & _
WScript.ScriptFullName & """" & " RunAsAdministrator",,"runas", 1
WScript.Quit
End If
End Function
It will run as Admin and if it doesnt work then your key is incorrect.
i am creating a .vbs file that should open access, and inside access a form call "Issue Details", but passing a parameter, meaning that if i have 10 issues in my "Issues" table a vbs file is created for each one and when clicked should open the right record(would be one ID for each record in the table). It is so far opening access and it is opening the form(Issue Details) but it is blank. What am i missing? Help, getting crazy here ... Check code below. The weird thing here is that if i double click it again it will refresh and open the right record without opening anymore windows..
How can i fix that? I dont want to do it twice :)
Public Sub sendMRBmail(mrbid)
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid
End Sub
Private Sub Create_Click()
On Error GoTo Err_Command48_Click
Dim snid As Integer
snid = Me.ID
Dim filename As String
filename = "S:\Quality Control\vbs\QC" & snid & ".vbs"
Dim proc As String
proc = Chr(34) & "sendMRBmail" & Chr(34)
Dim strList As String
strList = "On Error Resume Next" & vbNewLine
strList = strList & "dim accessApp" & vbNewLine
strList = strList & "set accessApp = createObject(" & Chr(34) & "Access.Application" & Chr (34)")" & vbNewLine
strList = strList & "accessApp.OpenCurrentDataBase(" & Chr(34) & "S:\Quality Control\Quality DB\Quality Database.accdb" & Chr(34) & ")" & vbNewLine
strList = strList & "accessApp.Run " & proc & "," & Chr(34) & snid & Chr(34) & vbNewLine
strList = strList & "set accessApp = nothing" & vbNewLine
Open filename For Output As #1
Print #1, strList
Close #1
Err_Command48_Click:
If Err.Number <> 0 Then
MsgBox "Email Error #: " & Err.Number & ", " & "Description: " & Err.Description
Exit Sub
End If
End Sub
Found the problem. Changed instruction below, adding acFormEdit to it and it worked:
DoCmd.OpenForm "Issue Details", , , "[ID] = " & mrbid, acFormEdit
Tried searching for the answer, all were too simple, or didn't match what I need. I have a message box, and I have it displaying information correctly. I have a Yes and No button. I want a timer, so when the timer runs out, it continues. If someone hits yes, it continues down the code, if they hit no, then it returns the user elsewhere. This is what I have, please help.
akey = MsgBox ("Image OS = " & ImageType & vbcrlf & _
"ComputerName = " & ComputerName & vbcrlf & _
"TimeZone = " & TZone & vbcrlf & _
"Ghost Server = " & Server & vbcrlf & _
"Broadcast Method = " & BMethod & vbcrlf & _
"Ghost Session = " & GhostSession _
, vbyesno + vbquestion,VN & " Please Confirm")
You can use Popup instead of Msgbox...
http://ss64.com/vb/popup.html
Set objShell = CreateObject("WScript.Shell")
X = objShell.Popup("You have 3 Seconds to answer", 3, "Test", vbYesNo)
Select Case X
Case vbYes
Msgbox "You pressed YES"
Case vbNo
Msgbox "You pressed NO"
Case Else
MsgBox "You pressed NOTHING"
End Select
Otherwise, you can try to manipulate an HTA or Internet Explorer window to do something similar.
I need to deploy some software through SMS/SCCM and the software requires that an ODBC connection be created in Windows. The connection information I was given requires a user name and password. I have a batch script to import the connection information into the registry, however I don't know how to go about putting the user name and password in. I'd like to script this or put it in some kind of distributable package.
Thanks,
-Mathew
You may use the follow script:
%WINDIR%\System32\odbcconf.exe CONFIGDSN "SQL Server" "DSN=ControltubProducao|Description=ControltubProducao|SERVER=10.23.22.18|Trusted_Connection=No|Database=Controltub"
%WINDIR%\SysWOW64\odbcconf.exe CONFIGDSN "SQL Server" "DSN=ControltubProducao|Description=ControltubProducao|SERVER=10.23.22.18|Trusted_Connection=No|Database=Controltub"
Here are examples of how to directly edit the ODBC Registry settings with VBScript, PowerShell, or Perl.
Here's the script I use (jan. 2015). Replace all <> with your names:
#echo off
cls
echo Configure user Data Sources for <ApplicationName>
echo.
set dsn_name=<OdbcLinkName>
set config_dsn=configdsn "SQL Server" "DSN=%dsn_name%|Server=<SqlServerName>|Database=<DatabaseName>|Trusted_Connection=yes"
%windir%\system32\odbcconf %config_dsn%
%windir%\syswow64\odbcconf %config_dsn%
echo Data Source "%dsn_name%" has been configured.
echo.
echo Done.
echo.
pause
Option Explicit
Dim strDSNName, strDriver, strServer, strLastUser, strRegional, strDatabase
strDSNName = "DSN NAME"
strDriver = "C:\WINDOWS\system32\SQLSRV32.dll"
strServer = "SERVER ADDRESS"
strLastUser = "USERNAME (PLACEHOLDER)"
strRegional = "Yes"
strDatabase = "DB NAME"
If MsgBox("ODBC configuration listed below is going to be added to current user." & vbcrlf & _
"Do you want to continue?" & vbcrlf & _
"DSN Name: " & strDSNName & vbcrlf & _
"Server: " & strServer & vbcrlf & _
"Database: " & strDatabase, vbInformation + vbYesNo, "Confirmation Required") = vbYes Then
Else
MsgBox "Operation cancelled by user.", vbExclamation, "Aborted"
Wscript.Quit
End If
const HKEY_CURRENT_USER = &H80000001
Dim strComputer, objReg, strKeyPath, strValueName, strValue
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources"
strValueName = strDSNName
strValue = "SQL Server"
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
strKeyPath = "SOFTWARE\ODBC\ODBC.INI\" & strDSNName
objReg.CreateKey HKEY_CURRENT_USER,strKeyPath
strKeyPath = "SOFTWARE\ODBC\ODBC.INI\" & strDSNName
strValueName = "Database"
strValue = strDatabase
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
strValueName = "Driver"
strValue = strDriver
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
strValueName = "Server"
strValue = strServer
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
strValueName = "Regional"
strValue = strRegional
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
strValueName = "LastUser"
strValue = strLastUser
objReg.SetStringValue HKEY_CURRENT_USER,strKeyPath,strValueName,strValue
Set objReg = Nothing
If (Err.Number = 0) Then
MsgBox "ODBC added successfully!" & vbcrlf & _
"Details are listed below:" & vbcrlf & _
"DSN Name: " & strDSNName & vbcrlf & _
"Server: " & strServer & vbcrlf & _
"Database: " & strDatabase, vbInformation, "Succeed"
Else
Wscript.Echo "Operation failed. Error = " & Err.Number
End If