Log Me In Rescue Service issues - batch-file

I have not been coding for very long but am looking to create a work around with some issues that log me in rescue has, during reboots log me in rescue will frequently lose connection and cause the program to break for lack of better terms, currently the fix for the issues is to delete the service which looks something like LMIRescue_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
So what i would like to do is create a batch that can delete an service starting in LMIRescue_
or if that's not possible to have a workaround where I can find service and the grab the out put and use that to delete
can any one give me an example of how to use batch to find the service and delete it on its own or at least point me in the right direction?
I have tried
#echo off
set /p UserInputPath1= What is the First Service Name?
set /p UserInputPath2= What is the Second Service Name?
net stop %UserInputPath1%
net stop %UserInputPath2%
sc delete %UserInputPath1%
sc delete %UserInputPath2%
this works but it requires user input and takes extra time, and the whole point in making this is to avoid wasting time
if wildcards would work, I could just do this
net stop LMIRescue*
sc delete LMIRescue*
net stop LMIRescue*
sc delete LMIRescue*
is there a way to delete a service using wild cards?
is there any way to delete a service in batch other than sc Delete "ServiceName"
and/or how can i use batch to find and output the name of the service and then use that to delete it.

You could use the sc query command to retrieve the names of all the active services that are currently running on the local machine. The findstr command can be used to filter anything that doesn't meet the given criteria (in this example, only lines starting with SERVICE_NAME: LMIRescue remain). The remaining line can be tokenized with the use of the for /f command to temporarily store the retrieved service name in for-loop variable %%j.
#for /f "tokens=1,*" %%i in ('"sc query | findstr /b /c:"SERVICE_NAME: LMIRescue""') do #(
net stop "%%~j"
sc delete "%%~j"
)

Related

Changing Hostname of PC through Batch Script

Lately I've started working on automating some of my tasks. Since I work with windows machines mostly I decided to learn batch and powershell.
For now I have faced issue with variables in CMD.
The task is extremely simple. I want this script to change the hostname of the computer. The new name should be user input. I want it to find default hostname (whatever it is) and then change it to what I tell it to.
At this moment I was using command:
hostname
set /p future_name="Input what you want Hostname to be: "
wmic computersystem where caption='%computername%' rename %future_name%
shutdown /r /t 5
The tutorials that I keep finding say that the hostname of PC should already be written in the code. However, this script is being used on multiple machines which all bear different names.
I understand that this is a newbie question but I got stuck here.
C:\Windows\system32>wmic computersystem where caption='%computername%' rename 'future_name'
Executing (\\DESKTOP-SMTHSMTH\ROOT\CIMV2:Win32_ComputerSystem.Name="DESKTOP-SMTHSMTH")->rename()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 87;
};
Press any key to continue...
P.S.
I tried running the script with another variable which will be me just retyping the hostname displayed above but it doesn't work still.
hostname
set /p my_name_is="Please retype my current name provided above: "
set /p future_name="Input what you want Hostname to be: "
wmic computersystem where caption='%my_name_is%' rename %future_name%
:: shutdown /r /t 5
PAUSE
It also returns the same as before. Any help will be appreciated!
Using the Where clause methodology you've shown in your question code, as far as I'm aware, is flawed. You cannot use the Create verb with the Where clause.
You should therefore, given a valid name and length, use the Call verb with the Rename method instead.
#Set "FutureName=%COMPUTERNAME%"
#Set /P "FutureName=Input what you want your new name to be>"
#"%__AppDir__%wbem\WMIC.exe" ComputerSystem Where "Name='%COMPUTERNAME%'" Call Rename "%FutureName%"
#"%__AppDir__%wbem\WMIC.exe" OS Call Reboot

NET USE multiple servers copy files

I want to copy specific file from one machine to 5 other machines, so I have server list file contains the ip addresses of each machine like:
10.10.1.3
10.10.1.4
10.10.1.5
10.10.1.6
10.10.1.7
in my batch file:
SET File=C:\Files\servers.txt
SET User=user
SET Password=pass
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
FOR /F %%A IN (%File%) DO (
START /WAIT NET USE b: \\%%A\C$\Temp /user:%User% %Password%
COPY C:\Logs\L1.log b:\L1.log /Y
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
)
the problem is in the first server I get error message The system cannot find the drive specified but for the others servers everything works great.
I think it's something with the NET USE of course maybe the map network is deleted before finish copy?
Is there any way in a batch file to loop some servers and for each one of the servers open map network copy files wait till copy is completed and move on to next server?
EDIT:
I have an update for this problem:
the source machine and the target machine are both in different domains.
I have a user define as admin in both of the machines.
The machines knows each other (I can open the target folder in the source machine like \server\C$\temp and I can paste anything I want there)
I tried to copy files without using net use and just copy from C:\file.log \server\c$\temp\file.log for each server (I have 5) and for 3 servers it worked and the other two I had an error: Logon failure: unknown user name or bad password
FOR /F %%A IN (%File%) DO (
COPY C:\temp\file.log \\server\c$\temp\file.log /Y
)
What can be the problem?
Please help?
Thank you in advanced.
The best way would be if the user/password is the same on every machine (credientals shared throughout the domain)
In that case, no need for NET USE, just copy to UNC path directly
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\C$\Temp\L1.log
I understood you have a problem on 2 machines out of 5. You cannot access C$ directly if you are not administrator of the machines.
Check:
domain (echo %USERDOMAIN% on the remote machine): All machines must be on the same domain for it to work
whether you are administrator or not on the remote machine: you cannot mount drives with the C$ trick if you are not administrator.
There is an alternate solution for you if this doesn't work out:
Share C:\TEMP (read/write) as the same name ex: sharedtemp on the 5 machines (or ask an admin to do that)
Then adapt the script like this:
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\sharedtemp\L1.log
Even if you are not an administrator, you'll be able to access the share that way.
When using START command, you need to either specify new window name, or leave it blank in double quotes, if you want it to run in the same open CMD window. However, you don't need to use START in this particular case. Try adding timeout for the 1st or all server connections in the list:
IF EXIST b:\null NET USE b: /DELETE
:: more code here
NET USE b: \\%%A\C$\Temp /user:%User% %Password%
if "%%A"=="10.10.1.3" timeout 5 >nul

%Computername% Up to a certain character for naming files

So we are running a backup script for my job and until now they just had the script pull the computer name and use that as the folder it creates. The problem that we are encountering is that when we go to then use the restore script it becomes useless.
Our computer names are for example SMITHT#-Year-A.
But I would like the batch script to not use the whole name because often this is used when we change the year of the computer. And because sometimes there are numbers after a persons name there are variying lengths so most of the knowledge I have on deciding start and stop points is coming up useless.
Is there a way to have the batch create a folder using the %COMPUTERNAME% function telling it to stop at the "-" so all we get is the name and number of the person?
To split a string at a certain character use for /F:
for /F "tokens=1 delims=- eol=-" %L in ("%COMPUTERNAME%") do (set SHORTNAME=%L)
This command line stores the string portion before the (first) - into variable SHORTNAME.
For details type for /? in the console window.
To use the above line in a batch script, replace %L by %%L.

Execute a series of commands when a batch program is closed

So, I am continuing to revise the program I described in my previous question Incrementing FOR loop for each line in a text file (batch).
As I am moving along, I am attempting to create a portion which will only grant access to a text document while the program is running. To do this, I am granting access on start up with net use. However, my dilemma is that I do not know of any event handler in batch that could make my program run net use /delete (<= I know the syntax is incorrect; besides the point) whenever the user closes the window (as in, literally click the red "X").
What event, or any command in batch, would allow me to do this?
Edit: I found something about the /wait command but I have no idea how this would help me with running the net use /delete command.
Instead of net use, you could use pushd with a UNC path to create a temporary network drive mapping. For example:
pushd \\localhost\c$\Users\%username%\Documents
Then, regardless of whether you popd, endlocal, exit /b, or the user terminates the script with the red X, the temporary mapping will be deleted at the end in any case.
Just make sure you setlocal at the top of your script, or at least somewhere before you pushd. It's generally good practice to put setlocal at the top of every script immediately after #echo off anyway, unless you have a specific reason not to.
If you need to authenticate, then use a combination of net use and pushd. Use net use without a drive letter, then pushd \\UNC\path && net use \\computername /delete. Here's a more complete example:
#echo off
setlocal
set "remotePC=minastirith"
set "user=%remotePC%\adminUser"
set "pass=Password"
:: establish an authenticated session
net use \\%remotePC% /user:%user% %pass%
:: pushd and immediately terminate the net use
pushd \\%remotePC%\share && net use \\%remotePC% /delete
:: The next two commands demonstrate that even though
:: the session has been disconnected, pushd still has
:: temporary access.
cd
dir
pause
:: When you popd, endlocal, exit /b, or close the window,
:: there's no longer an authenticated session. Another
:: attempt to pushd should result in an error.
popd
pushd \\%remotePC%\share
pause
Add a second batch. Won't help for the user clicking close button (you need to write a real program for that).
Batch1
Net use etc
Call Batch2
Net Use/delete etc
Batch2
Your commands
When batch two is terminated the rest ofthe commands in Batch1 runs. You need to read help call /?.

Trying to delete multiple appfabric cache names using a batch script

My goal is to write a batch script that will delete all of the cache names from a particular cache server.
The code I wrote below errors because it cannot execute the AppFabric PowerShell commands. It returns "Remove-Cache -CacheName blahblah" is not a recognized as an internal or external command.
I guess what I need to figure out and I need help from you guys is how can I use the shell script FOR /F command but yet be able to execute AppFabric PowerShell commands.
I tried adding the line:
powershell.exe -noexit -command "Import-Module DistributedCacheAdministration;Use-CacheCluster"
in the beginning of the batch script to first bring up the PowerShell window, import the AppFabric module and then run the batch script. But because PowerShell doesn't recognize FOR /F, it bombs there. I'm trying to delete multiple cachenames, but I'm just too not advanced enough to do it. HELP!
:
#echo off
REM using PING and batch line retrieval... only IP address info is called out from ping request
FOR /F "tokens=2,3" %%A IN ('ping %computername% -n 1 -4') DO IF "from"== "%%A" set "IP=%%~B"
echo %IP:~0,-1%
REM GET-CacheClusterHealth > C:\output.txt
REM FIND /n /i "NamedCache" C:\output.txt > C:\results.txt
FOR /F "tokens=4" %%i in (C:\results.txt) DO "Remove-Cache -CacheName %%i"
DEL "C:\output.txt"
DEL "C:\results.txt"
ECHO ALL Cache names have been deleted from Cache Server %IP:~0,-1%
Pause
To be honest it looks like you are making this way more complicated than it needs to be. PowerShell is the way things are moving so you may want to look into how it works a bit more. What you probably need is something along the lines of (kind of pseudo-code since I don't have the actual cmdlet to reference):
Import-Module DistributedCacheAdministration
Use-CacheCluster
$ServerName = "SomeServer01"
$ServerPort = "22233"
Get-Cache -HostName $ServerName -CachePort $ServerPort | ForEach{Remove-Cache $_.CacheName}
Now I don't know what is returned from Get-Cache but I imagine that it returns an array of things and from looking at your script one of the properties is NamedCache, which is what you want to work with. The above example would import the module needed to perform the commands. Then it sets the cache cluster to the current host, assign a variable to the name of a server that you want to work with, and then another variable to the port for it. Lastly it performs the Get-Cache command against the specified host and port, and sends the results of that to a ForEach loop that performs the Remove-Cache command against all of the items returned's NamedCache property. As I said before I assume it returns an array of objects which contain a NamedCache property.
As for FOR /F in PowerShell, what you could do if you really want to go that route is something like:
Get-Content Results.txt | ForEach{Remove-Cache -CacheName $_.Split(" ")[3]}
That gets the contents of the file, and then for each line it splits that line at each space, and references the 4th item (PowerShell's Split method will turn the string into an array of strings, and since arrays start at record 0 in PowerShell [3] references the 4th string in the array).

Resources