if-then batch to map drives - batch-file

I am trying to get a couple scripts to work with each other, but I am not entirely familiar with the if-then commands, I am using wizapp and I have my info ready to go, but I don't know how to map a specific location based on the output of wizapp, as a for instance
if %siteid%=="0"
How do I map that to a drive, I have 10 different drives that have to be mapped using that
info, and I am lost, siteid will obviously be different in each if then statement?

This is relatively easy to do. I will provide manual instructions as it is extremely useful to learn and will improve your coding skills.
C:\windows\system32> net view
Server Name Remark
---------------------------------------------------------------------------------
\\PC1
\\PC2
\\PC3
\\PC4
\\PC5
\\PC6
\\PC7
\\PC8
\\PC9
\\SERVER
The command completed successfully.
C:\windows\system32> net view \\PC1
Shared resources at \\PC1
Share name Type Used as Comment
-------------------------------------------------------------------------------
SharedDocs Disk
The command completed successfully.
C:\windows\system32> net use ( Drive letter A-Z ) \\PC1\SharedDocs
The command completed successfully.
Now open up My Computer and youll see that PC1 is registered as a drive onto your computer.

Related

why won't my AutoCAD accoreconsole open files or execute scripts?

I am running Vanilla AutoCAD 2017
The accoreconsole.exe will start but and show the example Screen and will appears to run commands, although i am unaware of a way to open a file directly from core console so i cant really do anything with it.
Methods attempted
1. Script Pro 2.0:
From https://knowledge.autodesk.com/support/autocad/downloads/caas/downloads/content/autodesk-customization-conversion-tools.html
Script pro executes scripts successfully when using AutoCAd But then Fails when i switch to Core Console
It generates a log file that reads: Error while reading log file for C:\Users\Documents\TEST\ARCH01_FIRST FLOOR PLAN - AREA C.dwg
2.Auto Lisp
From AutoCAD using an Auto Lisp command i found on House of BIM. The command works and i tested several scripts which i first tested using the Run Script Button in AutoCad and they worked, but not when i used core console.
This generated a Temp file called accc34642 which reads m_kernelList still has 1 entry
/i core console flashes and then disappears
/I core console opens a new file from the Qnew Template file path
This seems to happen no matter whats after that in the lisp statement.
Then OPEN command in core console doesn't return a prompt or an error, it just returns twice and does nothing.
Typing in a file path generates an error
3.Widows batch files
I have only used one of these in the past successfully but i checked the ones i found a couple different websites and still no luck on getting the scripts to execute.
I also tried all this from different File Paths
The main idea of AutoCAD Console is to process 1 file and exit. That's also important to avoid memory fragmentation and errors when you process multiple files in a single instance of accoreconsole.exe
So, as already mentioned, consider a workflow where each instance process a single file and then exit. You may even consider multiple instances of accoreconsole to process multiple files at once.
Wed0,
to get started with working with accoreconsole, here are a couple links that will help you:
1) http://adndevblog.typepad.com/autocad/2012/04/getting-started-with-accoreconsole.html
2) http://through-the-interface.typepad.com/through_the_interface/2012/02/the-autocad-2013-core-console.html
for the second article, Kean goes into quite a bit of detail, but I don't especially like his approach with using .bat files. It is much nicer to use a programming language like C#.
As for opening files, it actually is possible!
I recently discovered a VERY fast method of processing dwg files using accorconsole (or AutoCAD if that is what you prefer). For example, running a search and replace operation of dbtext and mtext on a batch of dwg files runs at around 15 drawings/second on a single process. Setting up 5 or 6 in parallel will process close to 5000 drawings/minute! I thought I would share this with the community as this discovery has been a game-changer for my company when it comes to processing large batches of dwg files.
For this you have to use the .NET API(or C++, etc). I did all the code for processing in C#, and the only 2 lines in the lisp script file is to NETLOAD my dll and call the command that does the processing.
Turns out, you can work on multiple dwg files in the same instance of accoreconsole. The idea here is to just load the dwg database without actually opening the drawing (takes ~70ms), manipulate the database, then save it. For the i/ switch, you can use any dummy drawing file as it will have no effect on your batch. Then in C# in your command method, you would do something like this:
string[] dwgFiles = Directory.GetFiles("C:\\MyDWGFiles");
foreach(string drawingFilePath in dwgFiles)
{
using(Database database = new Database(false, true))
{
database.ReadDwgFile(drawingFilePath, FileShare.ReadWrite,
true String.Empty);
using(Transaction transaction =
database.TransactionManager.StartTransaction())
{
//DO STUFF HERE
transaction.Commit();
database.SaveAs(drawingFilePath, DwgVersion.Current);
}
}
}
Let me know if you have any questions or need more detail,
I don't believe it is possible to open files from within core console. I am relatively certain core console has to be initialized with the path as an argument of the drawing you wish it to open. Once it is open you can then do work with that file. So, you would essentially have to open a single instance of core console for each file you want to do work on.
Hope that helps.
The best way as drive AutoCAD commands as dwg files is use the concept mencioned by "Autodesk Knowledge Network": Out-of-Process .NET
a. https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-NET/files/GUID-C8C65D7A-EC3A-42D8-BF02-4B13C2EA1A4B-htm.html),
and this alternative is supported by Autodesk; the accoreconsole is not.
With this features, you may execute AutoCAD software, open Projects, batch iterates on files and manipulate data. Recently, I developed one plugin that has two requirements:
b. be executed without user-intervention and,
c. interates on all Dwg files to execute a process per-file;
Like link above, I instantiated AutoCAD and send commands like a lisp sintaxe to call the customized command (as "("CUSTOM_CMD", "PARAMS1", "PARAMS2" ...)" );
My custom command was like this:
[CommandMethod("CUSTOM_CMD")]
public static void CustomCmd
{
//stuff iterate on DWGs
}
3.Comments: It's importante to use Thread Safe Collection (for example ConcurrentBag) to memory Dwg Collection to ensure non interference between Next Moving contiguous Dwgs iteraction's running.

Remove drive letters via batch or vbs scripting

I am searching for a little batch or vbs script that does the following:
Find the drives in a list of valid drive letters, e.g. ['c','d','e','f'], that have a specific drive name, e.g. 'BackupDrive'
Remove the drive letters of the found drives so that they are no more displayed in Windows Explorer
Any suggestion is very appreciated.
You can do this using the command line tool "diskpart". As stated in the official technet documentation if you know the volume name you can remove it with:
select volume <volume number>
remove letter=<Letter>
You can automate this either using a script file (as documented here) with the exact commands, or by calling the exe with objShell.Exec on the shell object in vbscript and manipulating the stdin and stdout accordingly. An example for that can be found here. In your case this would probably be the better approach because you could do a "list volume" there and then parse the result for description and label and act accordingly. Afaik this is sadly the only way to get to the volume number, because it is not present in wmi or somewhere easier queryable.
Please keep in mind that diskpart is a VERY powerful tool, that can wipe whole partitions, so use it with caution.
Also note:
You cannot remove the drive letters on system, boot, or paging
volumes. In addition, you cannot remove the drive letter for an OEM
partition, any GPT partition with an unrecognized GUID, or any of the
special, non-data, GPT partitions such as the EFI system partition.

How to shorten this script to make it fit in GPO GUI?

Huy Guys
How do you shorten this script to make it fit in a GPO GUI or make a transform file for it? We changed the share name to remove the spaces
Should read like this now. Its critical:
msiexec /i "\ho-dlpendpoint\DLPAgent\32\AgentInstall.msi
msiexec /i "\ho-dlpendpoint\DLP Agent\32\AgentInstall.msi"/q INSTALLDIR="C:\Program Files\Manufacturer\Endpoint Agent\" ENDPOINTSERVER="ho-dlpendpoint.ppsdmn.co.za:8000" KEY="BD075B59564B27304D69C67FA44561363CE94CBB54F3436EC74CF0E62F02382D62C95D7D182258103697520846B25444:43AEB4D39CF0EFD836F49DB518F4B7037C1AF151" UNINSTALLPASSWORDKEY="98B3085301D33FE35B59A038D85CFB0446028522" SERVICENAME="EDPA" WATCHDOGNAME="WDP" ARPSYSTEMCOMPONENT="1" /L*v %SystemDrive%\installAgent.log
Below is all the explinations of the different parameters:
msiexec /i "\ho-dlpendpoint\DLP Agent\32\AgentInstall.msi"
this is a fileshare accessable to all users - we donot object to first copying the .msi to the local machine, then executing it there e.g.
msiexec /i c:\AgentInstall.msi
we are open to suggestions, recomendations and best practice here
/q
quiet install - required
INSTALLDIR="C:\Program Files\Manufacturer\Endpoint Agent\"
this is that default that Symantec uses - please leave as is for ongoing continuity
ENDPOINTSERVER="ho-dlpendpoint.ppsdmn.co.za:8000"
this is our endpoint server and the communications port, this cannot be changed
KEY="BD075B59564B27304D69C67FA44561363CE94CBB54F3436EC74CF0E62F02382D62C95D7D182258103697520846B25444:43AEB4D39CF0EFD836F49DB518F4B7037C1AF151"
the cert, cannot be changed
UNINSTALLPASSWORDKEY="98B3085301D33FE35B59A038D85CFB0446028522"
our designated uninstall password cert key, this must not be changed
if you would like to test the install/uninstall process do not specify an uninstall password; rather use this for test purposes:
UNINSTALLPASSWORDKEY=""
SERVICENAME="EDPA"
the default service name, we never change this
WATCHDOGNAME="WDP"
the watchdog for the EDPA service to detect tampering with the EDPA service, we never change this
ARPSYSTEMCOMPONENT="1"
we use this do the agent does not appear in the remove program files - we never change this
/L*v %SystemDrive%\installAgent.log
we write the install process to this log file for problem determination
Thanks Guys
There a lot of things in there that never change, so it's not clear from your question why you can't:
Put them in the MSI file permanently. For example ARPSYSTEMCOMPONENT and maybe some others.
Make a transform. I'm not sure why you're asking how because that should be out there for you to find out, but basically you'd make a copy of that MSI file and use something like Orca to add most of those items to the Property table, as many as necessary. Then use Orca's Transform->Generate transform to get your .mst file, then use that .mst file in the command line instead of the property list.

Making Bootable USB From DVD using Command line

Is there any way to create a boot-able USB from a windows DVD via command line?
It is accepted using third party application which accepts command line arguments.
I searched internet but did not found any way.
You can do this using the program diskpart which is included in windows.
You need the following steps:
SELECT DISK <DRIVE NUMBER>
CLEAN
CREATE PARTITION PRIMARY
SELECT PARTITION 1
ACTIVE
FORMAT FS=NTFS QUICK
ASSIGN
EXIT
save them in a txt file. Find out the drive number using "LIST DISK"
You can then call diskpart with the /s parameter and give the saved textfile as a script.
Afterwards you can copy the content of the dvd over.
Now as to really do this programmatically:
The main difficulty would be selecting the correct drive number in case it may vary.
The main problem however is that this is a pretty dangerous operation. If you select the wrong disk (as stated those drives are selected by number not even by their drive letter) instead of your usb drive you might format a partition of your harddrive. I would personally not really fully automate the task but rather do it by hand.

How to refactor a Windows batch script littered with GOTOs?

I have to maintain a batch script of about 3500 lines littered with GOTO. Seems that the original "developer" hasn't heard of this famous paper and modular programming.
What the script does?
The script deals with the (silent) installation/uninstallation/reinstallation of several programs using different options. It could be split in several files that deal with each program in part. The problem is that if you're trying to take a part in another file that part will still GOTO another section that needs to be in the original script.
Refactoring?
Normally you wouldn't do a refactoring without having automated tests (so you can be sure you didn't break anything), but I don't know how to do it. There's no testing framework for that.
Partial Solution
I have come up with a partial "solution" that is some kind of adaptation of characterization tests (from Working Effectively with Legacy Code by Michael Feathers) and approval tests:
- create another script: test.py that replaces all commands (like copy or msiexec) with echo,
- redirect the output to a text file (good.txt),
- change the original batch script,
- run the test.py script again and save the output to another text file (current.txt),
- diff good.txt and current.txt -> if there are no differences then I didn't break anything, but if they are different I need to check if I broke something.
Problem with partial solution
How can I capture and replace all the commands? I could make a list of commands to replace, but there are also a lot of string concatenations to get the name and path of the program to be installed.
CMD level capture/hook?
Is there any way I can hook into the command line interpreter (CMD.exe) so I can replace on the fly all the calls to installers with echo?
Other suggestions?
Do I approach the problem in the wrong way? Can I do it better somehow? Do you have some advice I could use?
You could replace all COPY, DEL or CALL with %COPY%, %DEL% ,...
So you can use the same file for production and also for the tests.
#echo off
if not defined UNITTEST (
set "COPY=COPY"
set "DEL=DEL"
set "CALL=CALL"
)
%COPY% src dest
%DEL% somefile.txt
%CALL% installer.exe
And from your unittest.bat, you could start it via
#echo off
set "COPY=>>trace.log ECHO COPY"
set "DEL=>>trace.log ECHO DEL"
set "CALL=>>trace.log CALL ECHO "
del trace.log
set "unittest=Active"
call production.bat
fc good.txt trace.log
I'm not an expert in Batch, but I have done my fair share of it. With that said, I can offer a few tips.
Forget trying to do it all at once. Batch is very hard to debug. Echoing out to a log file helps a lot, but it will not capture everything you need if something goes wrong.
Work on breaking out the exe and msiexec calls into self-contained scripts. It is much easier to test the small script for the functionality you desire. Once you have that working, it is simple to call that script from the "Master" script.
Establish a good protocol for passing args to, and return codes from the smaller scripts. If there are common settings needed to be used for all the scripts consider using a central settings file.
GOTOs are not the devil, unless they pass control all over the place. Normally there are two good reasons that I know of to use GOTO’s.
Skip past a block of code that does not need to run.
To SET values into variables. Note there is a bug that can prevent variables from having their value set from within an 'IF' statement block. That little bug caused a big headache for me at one time.
Calls to a label might be better option at times.
Depending on how far back the legacy support is required, consider using Powershell when possible. The power and debugging capabilities of Powershell far out way the benefits of simple scripting of Batch. Which at 3500 lines simplicity has already been lost. You are already looking at Python, so maybe that could be used instead.
If you need a break point, use Pause. ECHO all the settings you need to examine right before the pause. This is as close to a break point I have found for batch.
Echo the command you intend to run to a log file and actually run it.
Write small verification scripts to be used independently or with the “Master” script to confirm you are getting the results you are expecting.
Use the right tool for the job. I like to use EditPadPro, RegexBuddy, and BeyondCompare for batch editing and comparing differences. There free tools that can be used too NotePad++ and Windiff. Making many edits in a file of that size is best handled by a good editor. IE inserting an echo at the beginning of a line that calls a cmd.exe.
Remember it is scripting not programming. While there is a lot of overlap of the two, the same exact approach to a problem may not be viable between the two.
Always make a backup copy of the scripts as a whole before mucking around. A fallback position is greatly appreciated when there is one small bug that you can’t find.
If it ain't broke... well you wouldn't be working on it if everything was working just fine.
Always test changes. And when you are done test it again. After that have someone else test it.
Just my .02. I’m sure someone else can chime in with more advanced advice. My knowledge on Batch has been acquired from the school of hard knocks, supplemented by ss64.com

Resources