Jump to content

My 1st Script - 1 problem & Streamlining needed


Recommended Posts

Hi all,

First of all, not sure whether to post this here or example scripts as although it works it does have a problem

This is my first script I have made with AutoIT, its very basic but does some deployment tasks to make my life easier.

Script:

; Deployment Script
; This script requires full Administrative rights
#requireadmin
; Do not display Autoit tray icon
;#notrayicon
; Gathering information
   ;Getting username to add to local admin
$username = InputBox( "Input", "Enter the persons name who will use this computer in the format SURNAME, FORENAME: " )
If $username = @error = 0 or StringLen($Username) < 1 Then
    MsgBox(0, "Error", "No data entered or Cancel was pressed", 1)
Exit
Else
; Get the MAC Address of the computer
Send ("{LWin}")
sleep (1000)
Send ("cmd.exe")
Send ("{ENTER}")
WinWait ("C:\Windows\system32\cmd.exe")
Send ("ipconfig /All")
Send ("{ENTER}")
; Check if Corp. IS&T have been contacted
$yesnobox = MsgBox(4, "Check!", "Have you registered the MAC# with Corporate IS&T for ACS (Authentication)")
If $yesnobox = 7 Then
Exit
Else
  
; Disable Autotuning on network card
Send ("{LWin}")
sleep (1000)
Send ("cmd.exe")
Send ("{ENTER}")
WinWait ("C:\Windows\system32\cmd.exe")
Send ("{CTRLUP}{SHIFTUP}")
Send ("cls")
Send ("{ENTER}")
Send ("netsh interface tcp set global autotuning=disabled")
Send ("{ENTER}")
Send ("Exit")
Send ("{ENTER}")
EndIf
    
; Locate USB Drive & assigned drive letter, then copy files.
$var = DriveGetDrive( "removable" )
If NOT @error Then
For $i = 1 to $var[0]
   If $var[$i] = "a:" Then ContinueLoop
$usbdrive = $var[$i]    

sleep (3000)
filecopy($usbdrive & "\7_Deployment\Shortcuts\*.*", "C:\Users\Public\Desktop", 1)
    Else
   MsgBox(4112, "Error", "USB Drive not found")
   Exit
   Next
EndIf  

; Delete Infoworks File from Public Desktop
FileDelete ("C:\Users\Public\Desktop\InfoWorks 4.url")
; Copy Citrix file to default users - stops WCSError on boot
If Not FileExists ("C:\Temp") Then
   DirCreate ("C:\Temp")
   filecopy($usbdrive & "\7_Deployment\Citrix Fix\APPSRV.INI", "C:\Users\Default\AppData\Roaming\ICAClient\", 1)
Else
   filecopy($usbdrive & "\7_Deployment\Citrix Fix\APPSRV.INI", "C:\Users\Default\AppData\Roaming\ICAClient\", 1)
EndIf
; Add user to local admins
Send ("{LWin}")
sleep (1000)
Send ("mmc.exe lusrmgr.msc")
Send ("{ENTER}")
WinWaitActive ("lusrmgr")
Send ("{TAB}")
Send ("{DOWN}")
Send ("{ENTER}")
Send ("{DOWN}")
Send ("{TAB}")
Send ("{ENTER}")
WinWaitActive ("Administrators Properties")
Send ("!d")
WinWaitActive ("Select Users")
Send(($username) & ";user1" & ";user2")
Send ("{ENTER}")
If WinActive ("Local Users and Groups") Then
   Sleep (5000)
Else
WinWaitActive ("Administrators Properties")
Send ("{ENTER}")
Endif
Endif
Exit

2 Questions:

1, Why doesn't this part work? If the Computer has a floppy disk, the script still tries to read it even though I have specified no A:\ When it tries to read A:\ the files do not copy from the usb stick & then the script continues to add users to admins.

; Locate USB Drive & assigned drive letter, then copy files.
$var = DriveGetDrive( "removable" )
If NOT @error Then
For $i = 1 to $var[0]
   If $var[$i] = "a:" Then ContinueLoop
$usbdrive = $var[$i]

2, Can you suggest any improvements to the code, as I've said this is my first one and I've never tried scripting before.

Link to comment
Share on other sites

; Deployment Script
#RequireAdmin ; script requires admin rights
#NoTrayIcon ; do not display autoit tray icon
#include <Constants.au3>

; Gather information
$hUser = InputBox("Input", "Enter the persons name who will use this computer in the format USERNAME, FORENAME: ")
If $hUser = @error Or $hUser = 0 Or StringLen($hUser) < 1 Then
    MsgBox(0, "Error", "No data entered or cancel was pressed", 5) ; changed timeout to 5 seconds
    Exit
Else
    ; Read and format MAC address
    $hRun = Run(@ComSpec & " /c getmac /fo csv /nh", '', @SW_HIDE, $STDOUT_CHILD) ; get mac address
    While ProcessExists($hRun)
        Sleep(100) ; wait for window to close
    WEnd
    $sMAC = StringLeft(StringReplace(StringReplace(StringStripWS(StdoutRead($hRun), 8), "-", ""), """", ""), 12) ; format MAC address
    ; Check if Corp. IS&T has been contacted
    $hYesNo = MsgBox(4, "Check!", "Have you registered the MAC# with Corporate IS&T for ACS (Authentication)")
    If $hYesNo = 7 Then
        Exit
    Else
        RunWait(@ComSpec & " /c netsh interface tcp set global autotuning=disabled") ; disable autotuning on network card
        ; Locate USB Drive & assigned driver letter, then copy files
        $aVar = DriveGetDrive("removable")
        If Not @error Then
            For $i = 1 to UBound($aVar) - 1
                If $aVar[$i] = "a:" Then ContinueLoop
                $sUsb = $aVar[$i]
                Sleep(3000)
                FileCopy($sUsb & "7_DeploymentShortcuts*.*", "C:UsersPublicDesktop", 1)
            Next
        Else
            MsgBox(4112, "Error", "USB Drive not found")
            Exit
        EndIf
        FileDelete ("C:UsersPublicDesktopInfoWorks 4.url") ; delete infoworks file from public desktop
        If Not FileExists("C:Temp") Then DirCreate("C:Temp") ; create directory if it doesnt exist
        FileCopy($sUsb & "7_DeploymentCitrix FixAPPSRV.INI", "C:UsersDefaultAppDataRoamingICAClient", 1) ; copy citrix file to default users - stops WCSError on boot
        ; code for adding user as an administrator. not sure what you are doing here and why, so letting you figure out a better way to work that code.
        ; the above code should help you understand how to make your code more efficient
    EndIf
EndIf

I have not tested it, but it passes syntax checks.

I'm not sure why you are getting the machines MAC addres since your code doesn't seem to do anything with it. But you can use my code I've found that works quite well to get a machines MAC address. I store it in the variable $sMAC

Edited by kor
Link to comment
Share on other sites

Regarding the MAC address, its just to have it on screen if the person deploying hasn't already contacted the department to add it to network.

Then change

$sMAC = StringLeft(StringReplace(StringReplace(StringStripWS(StdoutRead($hRun), 8), "-", ""), """", ""), 12) ; format MAC address
    ; Check if Corp. IS&T has been contacted
    $hYesNo = MsgBox(4, "Check!", "Have you registered the MAC# with Corporate IS&T for ACS (Authentication)")

To

$sMAC = StringLeft(StringReplace(StringStripWS(StdoutRead($hRun), 8), """", ""), 17) ; format MAC address
    ; Check if Corp. IS&T has been contacted
    $hYesNo = MsgBox(4, "Check!", "Have you registered the MAC " & $sMAC & " with Corporate IS&T for ACS (Authentication)")
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...