I need to create a simple .ps1 file on a target machine and run it.
param($my_exe) $CMD = $my_exe $CMD >> c:\logs\exe_name.log & $CMD
At the moment I copy my file but I'd prefer to create it on the target machine.
$line1 = "param($my_exe)" $line2 = "$CMD = $my_exe" $line3 = "$CMD >> c:\logs\exe_name.log" $line4 = "& $CMD" $target = "C:\myfiles\myscript.ps1" $line1 > $target $line2 >> $target $line3 >> $target $line4 >> $target
I have two troubles:
$line1 > $target
how can I ensure that the task was executed successfully?
$line2 = "$CMD = $my_exe" $line2 >> $target
how can I ensure that they are written as expected?
It appears you are trying to run a command on remote machine. Is there a reason you are not using Invoke-Command or other powershell remoting solutions? https://blogs.technet.microsoft.com/heyscriptingguy/2011/06/13/use-powershell-invoke-command-for-remoting/
Well... OK, you're taking a very Cmd.exe approach, which isn't the best for PowerShell. First, some things to look up:
Try using a here-string instead of this half-dozen "line" variables. Get all your text in one variable.
Use Out-File instead of the angle brackets. Output all the text in one command.
Use single quotes to stop the shell from treating anything as a special variable.
You must be logged in to reply to this topic.