Author Posts

July 24, 2015 at 8:37 am

Is this possible? It should also be able to take the credentials. Something similar to what psexec does. ps1 script is already stored in the remote system.

Ex Client1 is having C:\Temp\test.ps1 and I should be able to launch it from Server1's powershell console using a supplied credentials and Psremoting is not enabled in Client1.

July 24, 2015 at 10:15 am

You could potentially use the Win32-Process WMI class, but keep in mind that the "designed" solution for this is Remoting. PowerShell doesn't natively provide an alternative, although as I mentioned you could possibly use WMI.

July 27, 2015 at 3:31 am

The thought of doing this through WMI intrigued me so I started some experiments. You can get WMI/CIM to start a new powershell session and run a script

$computer = $env:COMPUTERNAME
$class = Get-CimClass -ClassName Win32_process -ComputerName $computer
Invoke-CimMethod -CimClass $class -MethodName Create -Arguments @{CommandLine = "powershell.exe -NoExit -File C:\TestScripts\script1.ps1" }

The major draw back to using this approach is getting the data back.

You are creating a new PowerShell process and your script will run in the context of that process. PowerShell doesn't allow for data being moved across instances. You could get you script to write output to a location you can access but that is starting to build in a lot of complications to avoid using remoting

I did think of trying to use a CIM session but you still run into the issue of opening a separate PowerShell session from which you can't return the data

Remoting is designed specifically for this scenario and is the recommended way to approach this type of problem