How can I use CLI to start a service on multiple remote PCs? - command-line

Due to the constraints of my environment, I am unable to use Powershell to start this service and need to use CLI. I need to be able to start a single service on apporx. 100 workstations.
I have been able to start the service remotely on a single PC, but I would like for it to point to a txt list of computers.
This is the command that will work on a single workstation
sc \\remotepc start [service]
I have tried this command but does not start the service on the remote workstation
for /f %H in (D:\Server\workstationlist.txt) do net start [service]

I was able to find a few ways to execute these commands
for /f %i in (computerlist.txt) do sc \\%i start winrm
computerlist.txt is a text file containing the names of the remote computers with one name per line. You can also replace this with a comma-separated list of computer names.
For /f "tokens=1" %i in ("Computer1,Computer2,Computer3") do sc \\%i start winrm
Where 'Tokens=1' tells the for loop to split the list into separate tokens based on the comma character and use only the first token as the computer name

Related

Execute many db2 commands in one command

can I run multiple db2 commands in one command?
i.e: from cmd:
db2cmd /c db2 /c connect to sample user sample_user using sample_pwd /c
"SELECT * FROM table;"
I also tried the following:
db2 connect to sample user db2admin using pwd; EXPORT TO result.csv OF DEL
MODIFIED BY NOCHARDEL SELECT * FROM alarms;
but didn't work with the following error:
SQL0104N An unexpected token "EXPORT" was found following
"". Expected tokens may include: "NEW". SQLSTATE=42601
as an example, for VERTICA, vsql tool, this can be done this way:
vsql -h localhost -U user -w pwd -c "SELECT * FROM alarms" -A -o
"alarms.csv" -F "|" -P footer=off -q
You appear to be using Microsoft Windows db2cmd.exe .
Your question has nothing to do with Db2 per se, but it is instead more about CMD (cmd.exe) scripting syntax, a legacy scripting language for batch files by Microsoft that still works on Windows-10, and which also works in db2cmd.exe.
In a db2cmd.exe shell you can use the "&&" sequence between distinct Db2 commands (and each such command must have the db2 prefix). Additionally each such command line has to escape any of the characters that are special characters to the shell itself. By default the escape character is a caret (^) symbol.
For example db2 connect to dbname && db2 ^"export to alarms.csv of del ... select ^* from alarms^" && db2 connect reset
( I show the ^ before any " that you might want to pass to Db2-CLP ).
But that && will require that each command returns a zero exit code, which might not be what you want, although it is usually the safest option. If a previous command fails then subsequent commands will not run.
If you want to tolerate some non-zero exit codes, use bracketing ( ... ) to group commands, and then use the && or & outside the brackets depending on your requirements. You can read about CMD scripting in any good book, plenty of examples online.
However, when scripting for Db2 on Windows , it can be much wiser to append all of the commands (without the Db2 prefix) into a plain text file, and then ask the Db2 clp to execute the text file via the syntax db2 -tvf texfile. Doing it this way lets you add conditional logic inside the textfile, handle exceptions , avoid shell escaping requirements, etc. If you encapsulate all your logic inside a script, it makes it easier to test, and also easier to run from a single db2cmd /c .... command-line.
If you want to make a batch file (*.bat or *.cmd) that does not need the db2cmd prefix to be invoked, you can alter your batch file to have a few lines at the start of the batch file to re-execute itself via db2cmd.exe. This works better if your db2cmd.exe is already on the PATH environment variable, but if that is not the case then you can fully-qualify the absolute pathname to your db2cmd.exe inside the batch file. The lines to add at the start of the batch file are:
#rem re-execute via db2cmd if running from cmd.exe
#echo off
if "%DB2CLP%"=="" db2cmd /c /i /w "%0" %* & goto :EOF
db2 connect to sample user db2admin using pwd
if errorlevel 1 #echo "Failed to connect to database " && #goto :EOF
db2 "EXPORT TO result.csv OF DEL MODIFIED BY NOCHARDEL SELECT * FROM alarms"
if errorlevel 3 #echo "Export from Db2 failed" && #goto :EOF
Additionally on Windows, you can use Powershell scripting to maniuplate Db2 databases, and you can also use Windows subsystem for unix to run Unix-style shell scripts in some configuration.
Your example's most direct comparison in Db2-land would be clpplus which lets you specify the database but you also have to provide login information (including a password or you can be prompted for it).
Within the db2cmd and db2 framework you have a couple options but most likely will want to use a script file.
One option: Set the registry variable DB2DBDFT to your default database. Personally, I dislike this option because it causes an implicit connection to a database that you may not have intended.
One option: Put your series of commands into a file and run that file. This is the more traditional way of running multiple commands. Commands can be terminated with a semi-colon and newline (it understands DOS and Unix differences here). You can use a different terminator by using -td # (for example). You would then invoke db2 -tf file.sql.
One option: A batch file. It's similar to above but you'd use the db2cmd environment to execute a batch that has the db2 commands in it. db2cmd gets you an appropriate environment for working with Db2. If you connect to a database in this environment you stay connected until you issue a CONNECT RESET, a TERMINATE, are forcibly disconnected, or your environment exits. So your batch file would simply have:
db2 connect to sample user db2admin using pwd
db2 "EXPORT TO result.csv OF DEL MODIFIED BY NOCHARDEL SELECT * FROM alarms"
(note the quotes to keep the command line from substituting all the filenames in the current working directory where the * is)
Two options.
1-st:
db2cmd /i /w /c "db2 ^"connect to sample^" & db2 ^"values 1^" & db2 connect reset"
2-nd:
You may set the following Windows system environment variable DB2CLP to the value **$$** and run db2 commands from windows cmd directly afterwards like this:
db2 "connect to sample" & db2 "values 1" & db2 connect reset

PSExec could not start Regedit.exe on remote PC

Background:
I have several registry keys that are saved and used to update/configure remote PCs in my environment. I may not have deployed the device, previously, but I use these registry keys to update the PC as I fix/repair/etc...I am part of a team of 45 managing about 25,000 workstations and peripherals in our region of the enterprise.
if I use this command from my Win 10 PC to a Win 7 PC, it executes without problems.
psexec \remotecomputer -u domain\adminaccount regedit.exe /i /s "\server\share\directory1\directory2\directory3\someregistry.reg"
psexec starts regedit, imports the above registry file and closes.
If I run that exact same command from my Win 10 PC to another Win 10 PC, I get the following error, from CMD as ADMIN,
"PsExec could not start regedit.exe on REMOTEPC_HOSTNAME:
The system cannot find the file specified."
it can't find regedit??? WTF??
I'm curious why Win 10 is blocking me, as an admin, from importing a registry key?
I've tried reg import as a solution, but I keep getting syntax errors and it can't find the file/pathway.
It's almost like the remote PC, doesn't recognize admin input due to UAC settings?
In testing, I've set the group policy editor to enable remote registry tools, no change. I've enabled remote registry services and set to start the service automatically. I can access the PC registry via remote registry...but psexec just won't work on the remote device.
I can RDP to the device and run the command line equivalent:
regedit.exe /i /s "\UNC\pathway...\etc*.*reg" it executes, without issues.
this proves that regedit is working
Can anyone provide some expertise on this?
add -s flag to psexec to run regedit
The old PsExec trick of running RegEdit doesn't always work. This is because psexec depend on a hidden SMB Administrator service. (Which in any security sense should be disabled.)
PsExec allows redirects of the input and output of a remotely started
executable through the use of SMB and the hidden $ADMIN share on the
remote system. With this share, PsExec uses the Windows Service
control Manager API to start the PsExecsvc service on the remote
system which creates a named pipe that PsExec communicates with. This
named pipe is what allows for input/output redirection back to the
system that launched PsExec.
So instead, in order to remove any TrustedInstaller owned registry Keys, you can use a different tool. This worked:
ExecTI - Run as TrustedInstaller
Run it to open the ExecTI GUI and enter: C:\Windows\regedit.exe

Delete specific files on all remote computers joined to the domain

I need to add something to first run as admin and second to run on all computers on the domain.
forfiles -p "C:\ProgramData\ESET\ESET Endpoint Antivirus\Logs\eScan" -s -m *.dat /D -0 /C "cmd /c del #path"
Add it in a startup script or as a scheduled task by using Group Policy or use a system management solution like System Center Configuration Manager (SCCM) if you have it available.
Avoid script-solutions like PowerShell, VbScript, psexec etc. to run this on the computers because you would need to run them x times to reach every computer that might have been offline/unavailable the first time. The solutions above are far better at making sure every computer runs it as soon as possible.

SqlCompare command line remote server

I am trying to use the sqlcompare command line utility to compare a local database and a remote database but I am having difficulty doing this.
I would like to specify a connection string or some way to connect to the remote server (Destination)
Here is the command I have so far:
sqlcompare /Database1:RootDev /Database2:RootProd /scriptFile:"_build\changes.sql" /f
You'll want something along the lines below, using the switches /Server1 and /Server 2 to specify the servers.
sqlcompare /Server1:local\SQL2008 /Database1:RootDev /Server2:remote\SQL2008 /Database2:RootProd /scriptFile:"_build\changes.sql"
Full details on the docs

Using Netsh with PsExec

I'm trying to dump DHCP settings from an older server thats being decommissioned. I ran the command fine while on the actual server but when trying to run it using psexec remotely, it keeps failing. The command is: psexec \\$source netsh dhcp server \\$source dump>$dhcpSettings
$source = the server being decommissioned
$dhcpSettings = the path to save the dumped settings
I've tried all sorts of combinations of encapsulating quotation marks but still nothing. the errors I'm getting is, "The system cannot find the file specified" and "The system cannot find the path specified"
EDIT: So I got rid of the path to save the dumped settings and now it works. But how should I format the command so that it'll save the settings to the remote computer's C:\USER.SET\LOG directory?
One solution might be to bundle the command you want to run and the stdout redirection into a single line cmd file and use PsExec -c or -f to copy and execute that file on the remote system. As an example
Create a line cmd file named DHCPSettings.cmd with the following in it and save it to C:\temp\:
netsh dhcp server \\localhost dump >c:\user.set\log\dhcpsetting.log
Then use
psexec \\$source -c c:\temp\DHCPSettings.cmd
You did not really provide any code to go by and I am not sure I understand the all requirements and constraints you have, so consider this as an idea; not the exact commands you need to run. Hope it helps.