Archive for the ‘ Programming ’ Category

VBS Clean Printer list

If your looking to delete all the printers via script this is your code.
You can change the SELECT to pick certain printers.

This .vbs can be deployed using GPO as a user login script.


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where ServerName = '\\\\servername'",,48)
For Each objPrinter in colInstalledPrinters
objPrinter.Delete_
Next

All Printers


("SELECT * FROM Win32_Printer")

All printers where they are offline


("SELECT * FROM Win32_Printer WHERE ExtendedPrinterStatus = 2")

 

 

For more select statements please compile them using the information below.

http://msdn.microsoft.com/en-gb/library/windows/desktop/aa394363(v=vs.85).aspx

http://www.scriptinternals.com/new/us/support/Internal/WMI_Win32_Printer.htm

or search for “WMI Win32_Printer class”

Another useful tool is WMI Code Creator.
This will show you the values of current WMI records and build your select.

Print Friendly, PDF & Email

Check Windows 7 Activation Using VBS

So i had some issues trying to use windows activation and wanted to be able to report on the OS status using BGInfo.

My below script can be referenced  inside BGInfo.

I might be worth downloading WMI Code Creator v1.0 This can be used to show what other information is ready for the picking.

Editing the below script will be able to report on any of the records WMI can throw your way.

 

Copy and paste the following code into a new .vbs file


Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From SoftwareLicensingProduct")
Coltest = int(0)
IsWinActivated = True

For Each objitem In colItems
IF int(objitem.GracePeriodRemaining) = 0 Then Coltest = True else Coltest = False
IF Coltest = False then IsWinActivated = False
Next

IF IsWinActivated = False then
call echo "Failed"
ELSE
call echo "Successful"
END IF

Print Friendly, PDF & Email

Ubuntu 10.4 VPNC Cisco Connection

  • Install vpnc Client
    sudo apt-get install vpnc
  • sudo create file in “/etc/vpnc/” called “*something*.conf” <- creating config file

edit file and place information


IPSec gateway domain.co.uk
IPSec ID GroupID
IPSec secret GroupPassword
Xauth username VPNUsername
Xauth password VPNPassword

  • sudo vpnc-connect *something* <- e.g if file was work.conf then sudo vpnc-connect work
  • sudo vpnc-disconnect <- Disconnect from VPN

Not needed, but SCRIPT login/logout:

  • create file on desktop called “VPN.sh
  • place below script in file and amend connection string.

#!/bin/bash
echo “You Sure you want to connect to
VPN? Press ENTER”
read var_1
sudo vpnc-connect *something*
echo -n “Please ENTER again to disconnect”
read var_2
clear
sudo vpnc-disconnect
echo “VPN has been disconnected”
read var_3

  • Run terminal & navigate to location of VPN.sh
  • Type “chmod 777 VPN.sh” <- Make script executable
  • click “VPN.sh” from desktop and select run in terminal

Links I used when doing this.

  • http://www.ax697.org/writing-a-basic-ubuntu-script-200786.html
  • http://www.longren.org/2007/05/17/how-to-cisco-vpn-client-on-ubuntu-704-feisty-fawn/
  • http://www.gentoo.org/doc/en/vpnc-howto.xml
  • http://ubuntuforums.org/showthread.php?t=16434
Print Friendly, PDF & Email

ASP randomize number

ASP, set randomize


<%
randomize
StoreNumber = int(rnd*100)+1
%>

  • result is a number between 1 and 100
Print Friendly, PDF & Email

Check Registry Settings

Paste the following text into a .vbs file and edit as required.

The code allows you to check registry settings


On Error Resume Next

sResults = ""

Set oWShell = CreateObject("Wscript.Shell")
Call GetAppVersion("Internet Explorer", "HKLM\SOFTWARE\Microsoft\Internet Explorer\Version")
Call  GetAppVersion("Java",  "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{26A24AE4-039D-4CA4-87B4-2F83216016FF}\DisplayVersion")

msgbox sResults

Set oWShell = Nothing

Sub GetAppVersion(sApplication, sRegPath)
On Error Resume Next

sTemp = oWShell.RegRead(sRegPath)
If Err.Number <> 0 Then
sResults = sResults & "Version not found for " & sApplication & "." & vblf
Err.Clear
Else
sResults = sResults & sApplication & " Version: " & sTemp & vblf
End If
End Sub

Print Friendly, PDF & Email

ASP Select description from large string

You can use below code to select the first n of chars from a string.

This can also be done from an ASP string. (not tested but the command is still left.) – Select LEFT(ColumName,1) from TableName where id = ‘100’ ?? Anyone care to confirm?


<%
String1="MyText"
response.write(LEFT(String1,3))
%>

  • Result is MyT
Print Friendly, PDF & Email

Replace vbCrLf with <br>

When you create a textara and then save it to a DB, When you go to use it later on the output page, you will find

that if you used the return key when you wrote your textarea is now on one line. It seems to have foggtten the returns, unless you put it back into a textara.

When in a textarea the return key must be key “vbCrLF” but when in html must be “<br>”. Use this code to switch between the two.

  • replace(inputtext,lookfor,replease with)

<%
Replace(StoredVAR,vbCrLf,"<br>")
%>

Print Friendly, PDF & Email

Make form input safe from SQL Injection

  • Add the following code to a global Function page or the header page

<%
Function clean(inputtext)
dim badChars
clean = inputtext
badChars = array("select", "drop", ";", "--", "insert", "delete", "xp_", "=", "'", ":")
for i = 0 to uBound(badChars)
clean = replace(clean , badChars(i), "")
next
end Function
%>

  • The following code is a static use of the Function

<%
RawText = "asdasd--ad;ddelete=,qw:"
CleanText = clean(RawText)
response.write(RawText & "<br>" & CleanText)
%>

  • output text will have none of the banned chars.

Print Friendly, PDF & Email

Send emails via objMessage Function (ASP)

This code I have used to send emails from pages like contact.asp

  • Config for Function (incude in global config file or header)

<%
ConfigMailActive = "true"
ConfigMailUsername = "mail"
ConfigMailPassword = "password"
configMailServer = "mail.server"
ConfigMailPort = cint("25")
%>

  • Function (Include in global function file or header)

<%
Function SendEmail(FEmailTo,FEmailFrom,FEmailSub,FEmailMsg)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = FEmailSub
objMessage.From = FEmailFrom
objMessage.To = FEmailTo
objMessage.htmlBody = FEmailMsg

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 1

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = configMailServer

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoencoded

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = ConfigMailUsername

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = ConfigMailPassword

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = ConfigMailPort

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

IF ConfigMailActive="true" then
objMessage.Send
End IF
set objMessage = nothing
End Function
%>

  • Place at point of email

<%
StoreETo = "email@address.co.uk"
StoreEFrom = "email@address.co.uk"
StoreESubject = "Subject of email"
StoreEmsg = "Message String"
call SendEmail(StoreETo,StoreEFrom,StoreESubject,StoreEmsg)
%>

Print Friendly, PDF & Email

List every ServerVariable stored by the webserver

Normaly added to the bottom of your ASP code (footer.asp) for testing only.


<%
For Each var In Request.ServerVariables
Response.Write var & " = " & Request.ServerVariables(var) & "<br>"
Next
%>

Output is as follows

<span style="font-size: x-small;">ALL_HTTP:<span style="font-size: x-small;">HTTP_ACCEPT<span style="font-size: x-small;">:*/* HTTP_ACCEPT_LANGUAGE:en-gb HTTP_CONNECTION:Keep-Alive HTTP_HOST:mydomain.co.uk HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) HTTP_COOKIE:ASPSESSIONIDAQSATTBS=BLADACMABJAGMKJPDFPPBONA HTTP_ACCEPT_ENCODING:gzip, deflate
ALL_RAW:Accept: */* Accept-Language: en-gb Connection: Keep-Alive Host: mydomain.co.uk User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) Cookie: ASPSESSIONIDAQSATTBS=BLADACMABJAGMKJPDFPPBONA Accept-Encoding: gzip, deflate
APPL_MD_PATH:/LM/W3SVC/1/ROOT
APPL_PHYSICAL_PATH:c:\inetpub\wwwroot\
AUTH_PASSWORD:
AUTH_TYPE:
AUTH_USER:
CERT_COOKIE:
CERT_FLAGS:
CERT_ISSUER:
CERT_KEYSIZE:
CERT_SECRETKEYSIZE:
CERT_SERIALNUMBER:
CERT_SERVER_ISSUER:
CERT_SERVER_SUBJECT:
CERT_SUBJECT:
CONTENT_LENGTH:0
CONTENT_TYPE:
GATEWAY_INTERFACE:CGI/1.1
HTTPS:off
HTTPS_KEYSIZE:
HTTPS_SECRETKEYSIZE:
HTTPS_SERVER_ISSUER:
HTTPS_SERVER_SUBJECT:
INSTANCE_ID:1
INSTANCE_META_PATH:/LM/W3SVC/1
LOCAL_ADDR:127.0.0.1
LOGON_USER:
PATH_INFO:/index.asp
PATH_TRANSLATED:c:\inetpub\wwwroot\index.asp
QUERY_STRING:
REMOTE_ADDR:127.0.0.1
REMOTE_HOST:127.0.0.1
REMOTE_USER:
REQUEST_METHOD:GET
SCRIPT_NAME:/index.asp
SERVER_NAME:mydomain.co.uk
SERVER_PORT:80
SERVER_PORT_SECURE:0
SERVER_PROTOCOL:HTTP/1.1
SERVER_SOFTWARE:Microsoft-IIS/5.1
URL:/index.asp
HTTP_ACCEPT:*/*
HTTP_ACCEPT_LANGUAGE:en-gb
HTTP_CONNECTION:Keep-Alive
HTTP_HOST:mydomain.co.uk
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
HTTP_COOKIE:ASPSESSIONIDAQSATTBS=BLADACMABJAGMKJPDFPPBONA
HTTP_ACCEPT_ENCODING:gzip, deflate
Print Friendly, PDF & Email