• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1132
  • Last Modified:

create a powershell process remotely ?

Hi ,
I am trying to implement creating a process for powershell remotely so that i can push my powershell script to be executed on the remote machine.
the code from i got from soemwhere else isd attached here
But this line
$LaunchArgs = $commandline,$null,$null,0
now on this the fourth parameter is processId that we should assign
But i want the processid to be returned automatically .i mean i don't want to give it .
and also i want to give it a directory on the remote machine as a place to log it
but how can i achieve this ?
Very Urgent ,
Thanks
Dekus

$Class  = "Win32_Process"
  $Method = "Create"
  $MC = [WmiClass]"\\$Computer\ROOT\CIMV2:$Class"
  $LaunchArgs = $commandline,$null,$null,0
  $Result = $mc.PSBase.InvokeMethod($Method, $LaunchArgs)

Open in new window

0
yene
Asked:
yene
  • 4
  • 4
  • 3
6 Solutions
 
yeneAuthor Commented:
Sorry the $Commandline argumetn is like this
$PowershellExepath  = "C:\windows\System32\windows Powershell\powershell.exe"
#$TaskScript -the script i am trying to run on the machine
  $CommandLine = "$PowershellExepath  -ExecutionPolicy Unrestricted -command &{ $TaskScript}"
Please Urgent .
Thanks
0
 
BSonPoshCommented:
Without v2 I am not certain what you are trying will work.

They way I see it you have two options.
1) Powershell Server from nsoftware
2) psexec

Cost and simplicity I think psexec is your best bet.

psexec \\machinename powershell.exe -command script -noprofile -NonInteractive

0
 
leh327Commented:
What version of powershell are you using on both your local and remote system?
If you're using v2 and WInRM 2.0, you can use
$s = new-pssession <remotesystem>
icm $s{ your code here }
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
yeneAuthor Commented:
Currently i have on the client v2 and CTP3
but on the production servers until v2 comes officially we have v1 .
so basically my script should act like V1
0
 
yeneAuthor Commented:
leh327:
Thanks ,But on my test machines with the attached script  creates the  sample text file on the machine with both v2. The real production environment requires V1.How can i work around?

BsonPosh ,
Thanks,i downloaded the psexec try to use it
i just run this
psexec  -i  -u <domain>\administrator -p 'kkkkk'  \\$RemoteServer calc.exe
it still returns nothing it is just waiting .But do i need to have psexec installed on the remote server machines?








psexec \\$server -d  "c:\winnt\System32\WindowsPowerShell\v1.0\powershell.exe" -W "C:\Documents and Settings\kkk" CMD New-Item -type file 'Newfreespace.txt'
it returns me  a processID but no 'Newfreespace.txt' created.

$PowershellExepath = "c:\winnt\System32\WindowsPowerShell\v1.0\powershell.exe"
$CommandLine = "$PowershellExepath   -ExecutionPolicy Unrestricted  -noexit -command &{ New-Item -type file 'freespace.txt'}"
$Class  = "Win32_Process"
$Method = "Create"
$MC = [WmiClass]"\\$Computer\ROOT\CIMV2:$Class"
$LaunchArgs = $CommandLine,$null,$null,0
$Result = $mc.PSBase.InvokeMethod($Method, $LaunchArgs)

Open in new window

0
 
BSonPoshCommented:
Check this blog entry out... it should help you here

http://bsonposh.com/archives/236
0
 
leh327Commented:
What are you trying to accomplish on the server?

if you need to get free space of the drives on the server and you're an admin, this one will do:
$freespace = Get-WmiObject -ComputerName your-server -Class Win32_LogicalDisk -namespace root\cimv2
$freespace | out-file \\your-server\c$\freespace.txt
0
 
BSonPoshCommented:
I think the goal is to distribute load, but along that line. If you have V2 installed you can use -asjob on get-wmiobject and it will run async.
0
 
yeneAuthor Commented:
I have a framework to do everything about the asynchronous running.
But the only thing i needed is the task handler for a bunch of tasks .This task handler does start remote process on the remote machine and returns the processId and the logging path .Hence the piece that i wanted is the psexec line or the starting new process on remote machine part .
BsonPosh,
Do the remote machines should have PSEXEC installed or added to system root?
Issues on PSexec
1.)Is there such things as ..on the first round it runs and on the next round it does nothing ..is it unpredictable because the same thing running again no change while changing the file name
2.)it looks like i need to do some string manipulation on the retrun value of PSexec to get the process iD and returned Value status  which looks a little bit awkward .since it doesn't return an object .

By the way this is part of a tool not a single task so i need some reliable way to do it
Thanks
 
PS C:\Windows\System32> psexec -d \\$server C:\WINNT\system32\WindowsPowerShell\v1.0\powershell.exe New-Item -type f
ile 'createTxtFile.txt'
#this worked but next 
 
PS C:\Windows\System32> psexec -d \\$server C:\WINNT\system32\WindowsPowerShell\v1.0\powershell.exe New-Item -type f
ile 'NewtxtFile.txt' -value "Hello is it working?"

Open in new window

0
 
BSonPoshCommented:
psexec is not required on the remote machine.

0
 
leh327Commented:
Maybe it is still a bit archaic until powershell v2, but schtasks.exe can be used to create job on the remote server which runs a batch job that enable the powershell execution policy, then run the other powershell code.

Make sure that the domain\user has "logon as batch job" right first.
   schtasks /create /s $server /tn onfly /tr "c:\a.bat" /U adminuser /P password /ru domain\user /rp password /sc once /st 23:59:00
   schtasks /run /s $Server /tn onfly
   schtasks /delete /s $server /tn onfly /f

This link has more information for schtasks
http://msdn.microsoft.com/en-us/library/bb736357(VS.85).aspx
0

Featured Post

What Security Threats Are We Predicting for 2018?

Cryptocurrency, IoT botnets, MFA, and more! Hackers are already planning their next big attacks for 2018. Learn what you might face, and how to defend against it with our 2018 security predictions.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now