First time poster so bear with me I am slowly moving over to powershell but still in the early stages understanding it.
I have a batch script that removes msi/exe programs based on the 25th character of each line in a text file (uninstall-database.txt), I decided it was time to rewrite it but there’s one part of it I am struggling with no matter what I do I can’t seem to figure it out.
:: search for msi/exe uninstall strings and uninstall silently
for /f "tokens=* delims=[]{}" %%u in (uninstall-database.txt) do (
set applist=%%u
call :uninstallprocess
)
goto uninstalldone
:uninstallprocess
set firstchar=%applist:~0,1%
if %firstchar%==# goto commented
set appname=%applist:~0,25%
set firststringchar=%applist:~25,1%
:commented
exit /b
if %firststringchar%=={ goto msiuninstall
if %firststringchar%==[ goto exeuninstall
:msiuninstall
set msistring=%applist:~25,38%
msiexec /x%msistring% /qn /norestart
exit /b
:exeuninstall
set exestring=%applist:~26,-1%
%exestring%
exit /b
:uninstalldone
Format of the uninstall-database.txt…
Comments have # at the beginning
25 chars for app name and then an msi string like this {C18FD2DC-D1FE-338B-A046-E83D5BF19C6E}
25 chars for app name and then an exe string like this [“C:\windows\SysWOW64\Adobe\Shockwave 12\uninstaller.exe”]
Well, this forum isn’t really about writing a script for you or to migrate your old batch scripts, but we’re happy to try and answer questions! Did you have a specific question about Powershell code you wrote you wanted to start with?
I would recommend for you to start learning the basics of Powershell. I am sure as you progress you will come along commands and methods to replace what you have in your batch script.
Here you can find some great sources to start with:
The rest of it I have managed to change over all ok except this part and it seems like it’s to complicated for me to just jump into so I don’t know where to start and nothing I find on the web seems to fit with what I am doing. I currently only have this…
I guess I need help with the whole checking the 25th character instead of the first, I just need someone to point me in the right direction or give me an example of how I would do this.
I am not really following your effort here.
Are you checking the whole 25 char string or just char 25?
Your snippet above shows you checking the beginning of the string.
The are a few ways to parse a string whether form a CSV, variable, etc. For complicated strings, RegEx is often the most prudent approach, but if you are not familiar and patient, RegEx can be frustrating.
Of course you can use the approach you started with, just be sure you fully look into string matching approaches that are possible.
OK, as postanote already mentioned there are several approaches possible. One could be something like this:
$InputData = ‘25 chars for app name and then an msi string like this {C18FD2DC-D1FE-338B-A046-E83D5BF19C6E}’,
‘25 chars for app name and then an exe string like this [“C:\windows\SysWOW64\Adobe\Shockwave 12\uninstaller.exe”]’,
‘25 chars for app name and then an exe string like this [“C:\windows\SysWOW64\Adobe\Shockwave 13\uninstaller.exe”]’,
‘25 chars for app name and then an msi string like this {C18FD2DC-D1FE-338B-A046-E83D5BF29C6E}’,
‘25 chars for app name and then an msi string like this {C18FD2DC-D1FE-338B-A046-E83D5BF39C6E}’,
‘25 chars for app name and then an exe string like this [“C:\windows\SysWOW64\Adobe\Shockwave 14\uninstaller.exe”]’,
‘25 chars for app name and then an exe string like this [“C:\windows\SysWOW64\Adobe\Shockwave 15\uninstaller.exe”]’