0

I need to execute a command file (B.cmd) on Machine B from a Powershell script (A.ps1) on Machine A. I don't want to statically specify the path

B.cmd is supposed to execute C.ps1 which is in the same folder as B.cmd

MACHINE A: A.ps1
MACHINE B: B.cmd, C.ps1 (all in same folder)

so my command file looks like this B.cmd

@echo off
powershell.exe -file ".\C.ps1" -Iterations 10
echo
echo
pause

There's an error thrown in a A.ps1 file from which I'm calling the B.cmd file

A.ps1

Invoke-Command -ComputerName $systemName -credential $credentials -ScriptBlock {Invoke-Expression -Command: "cmd.exe /c C:\Temp\Batch\Test\B.cmd"}

A.ps1 throws error:

**The argument '.\C.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.**
+ CategoryInfo          : NotSpecified: (The argument '....File parameter.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName        : XXX

How do I make the .\ work or is there an alternative or is it wrong to use the .\ for a remote execution ?

Please forgive my ignorance anywhere as I'm very very new to PS, Thank You !

3
  • Aren't you calling the Powershell from a CMD/batch file? Otherwise, why the echo and pause commands? Where is the remote server defined and by what mechanism are you calling the remote script? WinRS, WMIC, psexec? Commented Mar 7, 2019 at 7:17
  • Im sorry im very new to PS... may sound weird but im calling this command file from a powershell file and the command file again calls a powershell file. So its PS (Machine A) -> CMD (Machine B) -> PS (Machine B) Commented Mar 7, 2019 at 7:29
  • 1
    Why you use forward slashes in the path? Commented Mar 7, 2019 at 7:34

2 Answers 2

1

If you're able to change the ABC.cmd you could use the following

powershell.exe -file "%~dp0\XYZ.ps1"

This will get the folder the ABC.cmd script is running from. Note that %~dp0 won't work in cmd for testing you'll need to test it from within a script.

Sign up to request clarification or add additional context in comments.

4 Comments

no, the file im running(ps script that executes the cmd file) is on system A and the file thats supposed to execute (the cmd file) is on system B so theyre not in the same folder.
Without knowing the path how do you expect to be able to find the file?
well, im calling the command file(with the path) from PS in machine A - so when the CMD from machine B is executed, will it not know its own path ? Ive updated the question, please see if it helps
I've changed my answer. Let me know if this helps.
0

So don't wrap a batch file around it. A waste of time and confuses the issue.

invoke-command -comp $computername -filepath C:\Temp\Batch\Test\XYZ.ps1 

If you need to run something 10 times, do it in a loop inside your PS script.

5 Comments

i need to actually ping multiple systems 86400 as iteration, just added a 10 randomly nevertheless ill see if i can just eliminate the batch
I can just about guarantee that if you "need" to ping something every second for a day, you've got the wrong solution to your actual problem. Test-Connection $dest -Count 86400 -Delay 1
um no, the particulars are really lengthy to explain(ping will be manually stopped after a fixed amt of time, the ping usually is for 30 mins or so -- so that i can perform load test on that machines post knowing the health based on the ping test results), but my basic question is how to use .\ or cant it be used ?
Dude, I know you have an answer now, but if you don't call the PS script from within a batch file, I don't know why you'd be so obsessed with using the `.\` . You need to know the location of the batch file first before you can run it. Same with a PS script if you're not putting the wrapper around it.
I'm obsessed with the .\ as its throwing error. and with finding a replacement as i want to be able to move the files to any location without having to open them every time and change the path. I have constraints and have to make do with this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.