Batch Scripts

Active X Batch Script

Posted by robd on August 16, 2012
Batch Scripts / No Comments

Some stupid software needs Active X controls to work, here’s a basic script for that to work:

The following batch file can be configured as a computer startup script to install the components:

@Echo Off
Setlocal

If exist C:\Windows\System32\STUPIDSOFTWARE.dll Goto End

XCOPY /Y "\\FileServer\Software\STUPIDSOFTWARE\STUPIDSOFTWARE.dll" "C:\Windows\System32\"

XCOPY /Y "\\FileServer\Software\STUPIDSOFTWARE\MORESTUPIDSOFTWARE.dll" "C:\Windows\System32\"

REM Does it need registering?
regsvr32.exe /s "C:\Windows\System32STUPIDSOFTWARE.dll"
regsvr32.exe /s " C:\Windows\System32\MORESTUPIDSOFTWARE.dll"

:End
Exit

Tags: , ,

Install an EXE remotely with PSTOOLS

Posted by robd on May 15, 2012
Batch Scripts, pstools / 1 Comment

Recently I needed to install some software on a bunch of machines but being the lazy git that I am I decided I couldn’t be bothered to walk upstairs, run the installer as me and install the software…so instead I created three convoluted batch scripts to do it:

Few things to note: I’m using PS Tools to run the scripts with my domain admin account, this is the same as shift right clicking the software as running as me!

I’m also using PS Tools to specify a list of PC’s!

Script one:

Copies an installation exe file from a server to a PC:

xcopy "\\SERVER\Software\LOnline.exe" "c:\"  /Y /R

Script two:

Looks at a list of PC’s and runs the above script for each PC in the list:

PsExec.exe @c:\Computers.txt -u DOMAIN\USER -p PASSWORD -c  "c:\CopyToPC.bat" > c:\commands1.log

 

Script three:

Looks at a list of PC’s then remotely installs the file LOnline.exe that you just copied to the pc remotely, I’m running the exe interactively when it’s done it pops a message to tell the user its done!!:

psexec.exe @C:\Computers.txt -d -i -u DOMAIN\USERNAME -p PASSWORD c:\LOnline.exe /i

I’m sure there is a much easier way of doing the above in one script but this was a very quick solution I put together!!

Exchange 2010 Distribution Groups

Posted by robd on January 12, 2012
Batch Scripts / 1 Comment
Recently I needed to re-create some distribution groups in a new Exchange 2010 environment from an Exchange 2003 environment
 
Here’s how I did it:
 
Firstly extracted the groups from Active Directory by modifying a script taken the good people over at Migration Wiz (Their Script http://migrationwiz.zendesk.com/entries/20046721-export-active-directory-distribution-lists-to-csv)

 

My Script (basically less information in the csv file) :


'********************************************************************************
' Description: Exports all distribution lists to CSV
'********************************************************************************

dim filename, props
filename = "2003DistributionLists.csv"
props = array("displayName","sAMAccountName",”Mail”,”mailNickname”)

'********************************************************************************
' Helper Functions
'********************************************************************************

function RetrieveUsers()

    dim rootDse, defaultNamingContext, ldapQuery, ldap, conn, rs

    set rootDse = GetObject("LDAP://RootDSE")
    defaultNamingContext = rootDse.Get("defaultNamingContext")
    ldapQuery = "(&(objectClass=group)(mailNickname=*)(mail=*))"
    ldap = "<LDAP://" & defaultNamingContext & ">;" & ldapQuery & ";adspath;subtree"

    set conn = CreateObject("ADODB.Connection")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"
    set RetrieveUsers = conn.Execute(ldap)

end function

function ArrayToString(value)

    dim firstValue, result
    firstValue = true
    result = ""

    for i=0 to UBound(value)
        if not firstValue then
            result = result + ";"
        end if
        result = result + value(i)
        firstValue = false
    next

    ArrayToString = result

end function

function DnToEmailAddress(dn)

    dim ldap, mail

    mail = ""
    ldap = "LDAP://" & dn
    set user = GetObject(ldap)
    on error resume next
    mail = user.Get("mail")
    on error goto 0

    DnToEmailAddress = mail

end function
function Main()

    dim value, fso, shell, users, firstProp, dns, firstDn, memberEmail, emailAddress

    set fso = CreateObject("Scripting.FileSystemObject")
    set csv = fso.CreateTextFile(filename)
    if not err.number = vbEmpty then
        msgbox err.message, 0, "ExportError"
        exit function
    end If

    comma = false
    for each prop in props
        if comma then
            csv.Write(",")
        end if
        csv.Write("""")
        csv.Write(prop)
        csv.Write("""")
        comma = true
    next
    csv.WriteLine(",""memberEmail""")

    set users = RetrieveUsers()
    while not users.EOF
        set user = GetObject(users.Fields(0).Value)

        memberEmail = ""
        firstProp = true
        for each prop in props
            if not firstProp then
                csv.Write(",")
            end if
            csv.Write("""")
            value = ""
            on error resume next
            value = user.Get(prop)
            if IsArray(value) then
                value = ArrayToString(value)
                if "member" = prop then
                    dns = Split(value, ";")
                    firstDn = true
                    for i=0 to UBound(dns)
                        emailAddress = DnToEmailAddress(dns(i))
                        if not "" = emailAddress then
                            if not firstDn then
                                memberEmail = memberEmail + ";"
                            end if
                            memberEmail = memberEmail + emailAddress
                            firstDn = false
                        end if
                    next
                end if
            end if
            csv.Write(value)
            on error goto 0
            csv.Write("""")
            firstProp = false
        next
        csv.WriteLine(",""" & memberEmail & """")

        users.MoveNext
    wend

    csv.Close

    set csv = nothing
    set fso = nothing

    Set shell = CreateObject("WScript.Shell")
    shell.run ("Explorer" & " ." )
end function

Main


You should now have a csv file containing all your information!!

 

Next create a PowerShell command to import the csv file, here’s one I made earlier:
Import-Csv user2.csv |
foreach {New-DistributionGroup -Alias $_.mailnickname -DisplayName $_.displayName
-Type Distribution -Name $_.displayName -PrimarySmtpAddress $_.mail
-OrganizationalUnit "OU=Users,OU=Sites,DC=DOMAIN,DC=local" }

 

Basically the headers from the csv file need to match the sections in the powershell command that start with $_.

 

Once that runs in powershell without issue then you should be good to import users into the groups….which I also have a script for!!

Batch Script to Exe‏

Posted by robd on January 12, 2012
Batch Scripts / No Comments
Recently a user came to me wanting to become an admin on their machine, after the initial “No chance” they explained they “needed” to update some software for a project they were working on…..which got me thinking!!

 

How could I allow a user to run some software with giving them admin …..so after a while I came up with this:

 

Make a batch file that uses PS Tools then wrap the batch file up into an exe so the user can’t get to the admin password!!

 

Here’s how:

 

Download and extract PS Tools from Microsoft http://technet.microsoft.com/en-us/sysinternals/bb896649

 

I normally delete all the tools I don’t use i.e. keep PSEXEC.exe (also it may be an idea to run psexec.exe prior to the script to get rid of the annoying disclaimer screen!

 

Create an administrator for the machine and remember the username and password.

 

In the folder with PS Tools create a batch file to run your desired software as an administrator, could look similar to this:

 

http://technet.microsoft.com/en-us/sysinternals/bb896649

 

@Excho off psexec.exe -u DOMAIN\ADMIN_USER -p ADMIN_PASSWORD -i -d “C:\Program Files\Internet Explorer\iexplore.exe” Exit

 

Save the script and give it a test.  I tested the script with IE as normal users don’t have access to the tools menu whereas Admins do.

 

Now to wrap the batch file:

 

Download this brilliant batch to exe application:
http://www.f2ko.de/programs.php?lang=en&pid=b2e

 

Run it in either 32 bit or 64 bit, here are the options I used
Notice I chose Invisible application, this simply means the user won’t see the script run!
The working directory refers to where ever the exe is placed, for the end user place it somewhere where they have permissions to create and delete files such as “My Documents” etc otherwise once the program has run it wont be able to delete its temporary files (also specified below)!

 

Notice I added PsExec.exe – You must do this or batch script won’t run!

 

I used a logo just to make it look nicer for the end user:

 

Default Options:

Done, give it a test and let me know how you got on!!!!