Running powershell script on network machine - batch-file

I am running a Powershell script from a bat file. These 2 files are placed on a network server and I am trying to run these on network server from my machine by accessing network server folder. I get following error:
CMD doesn't support UNC paths as current directories.
When I execute bat file , it tries to read script file from path
C:\Windows\System32
not from current directory which is set in bat file.
This script and bat file work fine when I run on my local machine.
I tried finding this on Google and possible solutions need to change some settings on network server which is not possible in my case.
What could be possible solution for this?
I am using PowerShell 2.0
Here is my bat file to run Powershell script
setlocal & pushd .
:getting current directory
cd /d %~dp0
Powershell.exe Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Powershell.exe . '%CD%\Hotfix-Automation-Installer.ps1' crpt
exit /B

It's like the message says: CMD doesn't support UNC paths as current directories, so you shouldn't use %CD% or . when you're working with UNC paths.
However, what's actually preventing your PowerShell script from being run is the single quotes around the path:
Powershell.exe . '%CD%\Hotfix-Automation-Installer.ps1' crpt
Single quotes are not valid quoting characters in CMD, so your script is actually trying to run the PowerShell script from a subfolder ' in the current directory. Which doesn't exist.
Change your script to this, and it should work just fine:
#echo off
powershell -ExecutionPolicy ByPass -File "%~dp0Hotfix-Automation-Installer.ps1" crpt

Related

Command Line Installation with SCCM 2012

I have a few applications that I am trying to deploy with SCCM 2012 but the installations are failing through the application catalog. So what I have for the deployment type is a script installer. I have "cmd.exe" (Without quotations) in the Installation program field and "Installer.bat" in the installation start in field.
When I look at the ccmcache folder, all the contents over that application are there but the following error displays the Software Center:
0x8007010B(-217024629)
I have done some reading online and the "10B" is a common command line error for invalid directory. I have tested the batch file when hard coding a path but my question is, how can I edit the batch file or SCCM to pull from the CCMCache path where the files are downloaded to on the local client? Currently the Batch File is simply:
#echo off
ApplicationName.exe
Do I need to edit the file to cd into the CCMCache folder where the files are placed? How can I get the batch file to run the executable that is downloaded to the CCMCache folder?
Thank You!
You need to have the full path to the installation in your script
#echo Off
\\path to .exe
The way the command is written will not be able to find the .exe file. You need to add the full unc path to the .exe into your .cmd file. You should have your installation .exe and .cmd file in the same location on the distribution share
Recommended Solution:
Before starting, since you are only launching an exe with your batch file, I would recommend just using your ApplicationName.exe as your command line parameter in SCCM instead of using a batch. This will eliminate the need to engineer any further.
Modifying the existing solution to work:
If you do still want to use a batch file, keep a few things in mind. The syntax you are using to launch the batch file will not work. I would recommend just using the batch file name "installer.bat" as your command line. If you still want to preface the batch with the cmd.exe, you absolutely need to use the /c switch with it
cmd.exe /c installer.bat
If you don't use /c the console host will only open to a promopt and not execute your batch.
This is not an ideal solution though because using "cmd.exe /c" will set your working directory to the location of cmd.exe (ie "C:\windows\system32") and since your content is staged in ccmcache, you will need to specify its location within your batch. To do this, you would use %~dp0 variable which gives you the directory the current batch is running from. This means modifying your batch to read
#echo off
%~dp0ApplicationName.exe

Batch file to move files from one FTP folder to another folder in the same FTP?

Good morning all,
I have a task where i need to automate a process where we download files from an FTP site, the files are processed in SQL, and then the files on the FTP site are moved to another folder on that same FTP site.
The first two parts i have down but the part of moving the files on the FTP to another folder on the same FTP is tripping me up.
Can anyone please offer some advice?
This is what i currently have:
#Echo Off
Set _FTPServerName=SERVER HERE
Set _UserName=LOGIN HERE
Set _Password=PASSWORD HERE
Set _RemoteFolder=FILES FROM
Set _NewRemoteFolder=FILES TO
Set _Filename=*.*
Set _ScriptFile=ftp1
:: Create script
"%_ScriptFile%" Echo open %_FTPServerName%
"%_ScriptFile%" Echo %_UserName%
"%_ScriptFile%" Echo %_Password%
"%_ScriptFile%" Echo cd %_RemoteFolder%
"%_ScriptFile%" prompt
:: Run script
ftp -s:"%_ScriptFile%"
Del "%_ScriptFile%"
Please let me know if you have further questions!
There's rename command in Windows ftp.exe command-line client:
rename oldname newname
So in your specific batch file:
"%_ScriptFile%" echo rename file.txt %_NewRemoteFolder%/file.txt
Though seeing the _Filename=*.* in your question, it actually looks like you want to move all files. That's not possible with Windows ftp.exe, at least not easily. The rename command does not support wildcards.
You would have to dynamically generate a script file based on a list of downloaded files with a separate rename command for each file.
Or use a different command-line FTP client that supports wildcards when renaming/moving.
For example with WinSCP scripting the batch file would be like:
winscp.com /log=winscp.log /command ^
"open ftp://username:password#example.com" ^
"cd %_RemoteFolder%" ^
"mv *.* %_NewRemoteFolder%/" ^
"exit"
For details see:
mv command
Converting Windows FTP script to WinSCP FTP script
(I'm the author of WinSCP)
The most direct way to handle this is to use PowerShell. This will make the file copy happen on the FTP host without requiring the data to come to your client before being written back to the server.
Invoke-Command -ComputerName FTPHOST -ScriptBlock { Move-Item -Path C:\dir1\thefile.txt -Destination C:\dir2\thefile.txt }
You can run this from a cmd.exe shell.
powershell -NoProfile -Command "Invoke-Command -ComputerName FTPHOST -ScriptBlock { Move-Item -Path C:\dir1\thefile.txt -Destination C:\dir2\thefile.txt }"

Starting a server using start "" "%~dp0\server.exe", parameters being ignored because of spaces

START "Test Server" "%~dp0\server.exe" LAN %M%.aao log=server.log ini=server.ini
Everything after "LAN" is not being executed by server.exe I can see in the servers log file that it is trying to open lan but it should be trying to open %m%.aao which means everything after "LAN" is being ignored.
How can I fix this?
Using
START "Test Server" server.exe LAN %M%.aao log=server.log ini=server.ini
will not work as I'm trying to run the batch file from WOTGreal. I am unsure why, but the way I fixed it for the other two files/programs I open was to use %~dp0, but the server requires that the spaces not be ignored.
I'm trying to run the batch file from WOTGreal
So the batchfile will be run from a different folder. That will also mean server.exe will be run from a different folder. so local filenames like in %M%.aao log=server.log ini=server.ini will be read from the wrong directory.
You could probably fix that by also using %~dp0 in all other paths. But it is probably easier to change the current directory at the start of the batchfile. To do that, add the following line to the start of the batchfile.
cd /d "%~dp0"

SQL Server agent for scheduling SFTP using WinSCP under SSIS

I have a batch script which generates a WinSCP upload script to upload a file to an SFTP location. Now when I run the batch file via command prompt - it runs successfully and loads it. I called the same thru SSIS Execute process task - it runs successfully and loads it. Now when I put the same on SQL Agent - I tried the following two options:
Using Operating System (CmdExec) - cmd.exe /c "\.bat"
Added the SSIS package to SSISDB and added it as a job step.
With both the above options the job showed a successful run. However the file is not uploaded! Any ideas on what is happening?
Here's my batch script:
echo off
SET winscp=C:\"Program Files (x86)"\WinSCP\WinSCP.com
SET stagingDirectory=\\<staging path>\
SET scriptPath=\\<ScriptPath>\UploadScript.txt
SET ftpHost=xx.xx.xx.xx
SET ftpUser=user
SET ftpPass=password
SET fileName=Test.xlsx
SET ftpFlags=
#REM ftpFlags: -explicit
echo deleting uploadScript if it already exists
IF EXIST %scriptPath% del /F %scriptPath%
IF EXIST %scriptPath% exit 1
echo Generating WINSCP Upload Script
>>%scriptPath% echo option batch abort
>>%scriptPath% echo option confirm off
>>%scriptPath% echo open sftp://%ftpUser%:%ftpPass%#%ftpHost% %ftpFlags%
>>%scriptPath% echo option transfer binary
>>%scriptPath% echo put %stagingDirectory%%fileName% /
>>%scriptPath% echo close
>>%scriptPath% echo exit
echo Launching WINSCP upload
start /wait %winscp% /console /script=%scriptPath%
As you start the WinSCP via the start (why?), the exit code is not propagated to the SSIS. So, you never learn, if the script fails. And it most probably fails.
You also should enable logging, so that you can see what's wrong.
You should use this code to propagate the WinSCP exit code to SSIS and to enable logging:
%winscp% /log=\\<ScriptPath>\UploadScript.log /script=%scriptPath%
exit /b %ERRORLEVEL%
(Note that the winscp.com does not have the /console parameter)
Anyway, one clear problem is that you do not specify an expected SSH host key in the script. When you run the script manually, you probably have the key cached in the registry of your Windows account. But under the SSIS a different account is used, and its host key cache is likely empty. You should add the -hostkey switch to the open command in the script to make the script independent on the cache. See Where do I get SSH host key fingerprint to authorize the server?
When testing the script, add the /ini=nul parameter to isolate the script from your configuration.
For this and other hints, when debugging WinSCP running under SSIS, see My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
And finally, see WinSCP SFTP Task for SSIS.
Your variable seems set incorrectly. To manage with a space in the path and into the variable you have to put in quotes the whole path or the whole variable.
i.e.
set "winscp=C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start "%winscp%"
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
or
set winscp="C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start %winscp%
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
Another point, you have to check this file: UploadScript.txt because your script adds new lines rather than remake the file.
change this line to >%scriptPath% echo option batch abort instead of >>%...
Ah, I did not pay attention to the IF EXIST.

batch file command line start in same folder as executable on network path

I know from a desktop I can have a bat file that runs an executable with specific parameters and starts in the same path on the network where the executable exists, but how can I accomplish the same thing when calling the bat file assuming is in same folder as executable from another application?
For example:
\My-Network\app\PR8.exe /noload
I want to start specifically in \My-Network\app (same folder where PR8.exe exists) but not where it defaults to which seems to be c:\windows somehow. I can't seem to do a cd on a UNC path and I don't want to use any path letters as the application also detects as to which server it is executing from as well.
It isn't possible to set a UNC working directory for Windows batch files without network mapping drives. However, in Windows 2000 and later, you can use the PUSHD and POPD commands to change the working directory to a UNC share when running your script.
Wikipedia gives us the example of creating a shortcut to your batch file where the Target is set to the following:
%COMSPEC% /E:ON /C "PUSHD """\\My-Network\app\""" & C:\PATH\TO\BATCHFILE.BAT & POPD"
In this case the Working directory attribute of the shortcut is ignored, as the PUSHD and POPD commands will change it to the path specified.

Resources