Powershell Invoke-WmiMethod to execute bat file on server 2003 - batch-file

I'm trying to execute a bat file on a different server that copies files to several other servers. I can get to the bat file and execute it just fine. But the commands in the bat file don't run. I know the bat file executes because I added an echo > text.txt statement, and that worked.
Here is what I have:
Invoke-WmiMethod -class Win32_process -name Create -ComputerName $serverName -Credential $Credentials -ArgumentList "cmd /c D:\startBat.bat"
Here is the bat file on the other server
d:
cd \path\path2
echo "asdf" > text.txt
copy /Y file.zip \\serv5\e$\temp\temp1
copy /Y file.zip \\serv4\e$\temp\temp1
copy /Y file.zip \\serv3\e$\temp\temp1
copy /Y file.zip \\serv2\e$\temp\temp1
copy /Y file.zip \\serv1\e$\temp\temp1
copy /Y file2.zip \\serv5\e$\temp\temp1
copy /Y file2.zip \\serv4\e$\temp\temp1
copy /Y file2.zip \\serv3\e$\temp\temp1
copy /Y file2.zip \\serv2\e$\temp\temp1
copy /Y file2.zip \\serv1\e$\temp\temp1
I'm at a loss as to what's going on here. I don't think it's a permission thing because I can get a service as a wmi object just fine and start, stop the service all I want. So I'm fairly certain my credentials are good.
thanks!

Related

How to solve batch file that keep deleting my file?

How can I fix my batch file??
When I run the command in Windows 10 1903, it run totally fine. But when I run it in Windows 10 2004, the batch keep deleting file that's in the batch folder.
Here is the script (not full) :
ipconfig /flushdns
cd %windir%/temp
powershell ri * -recurse -force >NUL
cd %temp%
powershell ri * -recurse -force >NUL
this is the line that keep deleting my file in Windows 10 2004
Can someone plz help me, I already try to solve it. But I didn't find a way out
Rather than changing directory, directly pass the path of the directory to the ri command.
ipconfig /flushdns
powershell ri %windir%/temp/* -recurse -force >NUL
powershell ri %temp%/* -recurse -force >NUL
The Temp directory you're trying to empty is inside the protected %SystemDrive%\Windows location. In order for it to be emptied, you must, in the first instance, have permission to do that, and generally for that you should be running your script elevated, i.e. 'Run as administrator'.
Also, I would have thought it would be quicker to do this directly with native cmd.exe commands, rather than calling out to an external utility like %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe:
#"%SystemRoot%\System32\ipconfig.exe" /FlushDNS
#PushD "%SystemRoot%\Temp" && (RD /S /Q . 2> NUL & PopD)
#PushD "%Temp%" && (RD /S /Q . 2> NUL & PopD)

Check multiple servers for a directory existence and copy it

Currently, I have the following batch script which copies the file MyFile1.accdb to C:\Computer:
xcopy "\\serverA\Folder1\FolderInside\MyFile1.accdb" "C:\Computer\" /Y
I would like to add various servers to this script, and have it copy only if the directory exists, (ignore the others).
How can I add the following and create an if then statement?
xcopy "\\serverB\Folder1\FolderInside\MyFile1.accdb" "C:\Computer\" /Y
xcopy "\\serverC\Folder1\FolderInside\MyFile1.accdb" "C:\Computer\" /Y
xcopy "\\serverD\Folder1\FolderInside\MyFile1.accdb" "C:\Computer\" /Y
e.g.
If serverB exists copy ...
If serverC exists copy ...

How can I execute psexec commands in bat file

I am trying to write a bat file that will accomplish the following:
psexec \\host1 cmd
d: #to change the remote server's drive
cd\
dir /s/b "file1" #searches for this file in host1 in its d: drive
How can I go about doing this
If you are on a current day Windows system, you can invoke the command on a remote computer using PowerShell from within your cmd .bat script. No need for psexec. The remote machine does need to be setup for PowerShell remoting. Get-Help about_Remote
powershell -NoProfile -Command "invoke-command HOST01 { cmd /C dir /S /B D:\file1 }"
If you are running in PowerShell:
invoke-command HOST01 { cmd /C dir /S /B D:\file1 }
Of course, in PowerShell you might as well use PowerShell cmdlets.
icm HOST01 { gci -n -rec D:\file1 }
-or-
Invoke-Command HOST01 { Get-ChildItem -Name -Recurse D:\file1 }
psexec \\host1 cmd /c "d: & cd\ & dir /s/b file1"
or simply
psexec \\host1 cmd /c "dir /s/b d:\file1"
The console in which the cmd is executed is automatically closed when the command finishes executing, so you won't actually see the result. You could leave cmd running (and console along with it) by using /k instead of /c, but that doesn't make much sense either. You appear to have an XY problem.

I want to write a script to copy a file from a directory to another directory and rename it

I want to write a batch script and ones I double click on it, it should copy a file from a directory to another directory and rename it to be fileX, knowing that there is a file with the same name in the destination directory.
So what I want to do is to remove the already existed fileX and copy the other one instead.
How can I do that, and please comment the script so I can understand.
You can use the command xcopy to do it. For example :
echo [Copy files]
echo * Create the file xcopy_EXCLUDE.txt in order to ignore some file and/or directory.
echo .au3 > xcopy_Exclude.txt
echo .pspimage >> xcopy_Exclude.txt
echo \psp\ >> xcopy_Exclude.txt
echo * - ignore all .au3 files
echo * - ignore all .pspimage files
echo * - ignore the \psp\ directory
echo * The file xcopy_EXCLUDE.txt is created.
echo.
echo * Copy additional files with xcopy.
xcopy "%FOLDER_SRC%" "%FOLDER_OUT%" /E /H /Y /EXCLUDE:xcopy_Exclude.txt
echo * Files and directory are copied.
echo.
echo * Delete xcopy_Exclude.txt.
del xcopy_Exclude.txt
If you are willing to look into powershell. Here is the command you would run.
# Path is the location to the original file
# Destination is where you want it to go
# -force indicates that it shoud replace a file if it finds it and not prompt you first
Copy-Item -Path "c:\temp\original.txt" -Destination "c:\folder2\filex.txt" -force
# For more help:
Get-Help Copy-Item

Creating a folder on a remote server using batch file from teamcity build agent

I am trying to create a folder on a remote machine by running the batch file from teamcity and then copy source into that folder but it seems to be not doing.
Using following code to create and copy
SET dirTempBackup=\\server1\BackupStorage\temp\test
SET Current=\\server1\web\BuildEnvironment\test
ECHO Starting to copy files.
IF NOT EXIST "%dirTempBackup%" MKDIR "%dirTempBackup%"
IF NOT EXIST "!Current!" (
ECHO ERROR! Not found: !Current!
) ELSE (
ECHO Copying: !Current!
SET Destination=%dirTempBackup%\!Current:~0,1!
REM Directory.
XCOPY "!Current!" "!Destination!" /v /c /i /g /h /q /r /y /e
)
using the temp directory as compressing it later and then deleeting it at the end. Teamcity is generating Access is denied. and Copy failing due to Invalid path. Have checked teamcity user has full rights on that folder.
checking the permissions on both source and destination fixed the problem.

Resources