How to start remotely process in PowerShell - powershell

I have a problem, I have a script which:
Connect with PSSession (I use PSSession with admin account)
Stop 2 process
Do change on them files
Start the 2 process (Problem here)
I want to start process on server, so i'm connect with PSSession (No problem)
I do Invoke-Command :
# $pathProg path to my program
Invoke-Command -session $mySession -command {Start-Process $($args[0])} -ArgumentList $pathProg
But it does nothing (I verify with VNC)
I do Invoke-Command too :
# $pathProg path to my program
Invoke-Command -session $mySession -command {&$($args[0])} -ArgumentList $pathProg
It lauch the program (Good) but my script wait the end program (Not good)
Anyone have an idea ?
Thanks

You can try using WMI:
$command = "notepad.exe"
$process = [WMICLASS]"\\$CompName\ROOT\CIMV2:win32_process"
$result = $process.Create($command)
If you need passing credentials:
$cred = get-credential
$process = get-wmiobject -query "SELECT * FROM Meta_Class WHERE __Class = 'Win32_Process'" -namespace "root\cimv2" -computername $CompName -credential $cred
$results = $process.Create( "notepad.exe" )

$pathProg may be not be available within the script block which gets run eventually. You might want to pass it as an argument to the script block
Invoke-Command -session $mySession -command { param($progPath) ... } -argumentlist $progPath
Not that the outer -argumentlist, passes the arguments to the scriptblock.

Have you tried building the command as a string locally, then passing it to the Invoke-Command script as a ScriptBlock?
$remoteSession = New-PSSession -ComputerName 'MyServer'
$processName = 'MyProcess'
$command = 'Start-Service ' + $processName + ';'
Invoke-Command -Session $remoteSession `
-ScriptBlock ([ScriptBlock]::create($command))
Remove-PSSession $remoteSession
If you want feedback from the remote server then you can get the output via Write-Output, like this:
$command = 'Start-Service ' + $processName + ' | Write-Output ;'

Related

Invoke-Command Doesn't Run with Parameters

I'm trying to run Invoke-Command within a PowerShell script to run scripts on remote servers. It retrieves the computer names and scripts to run from an XML file. Code sample is below. The script executes but nothing on the remote server is being run. I've tried 2 different ways to run Invoke-Command.
[string] $computer = "lumen"
[string] $scriptBlock = "cd C:\Scripts\Update-Apps; ./Update-Apps"
$session = New-PSSession -ComputerName $computer
Invoke-Command -Session $session -ScriptBlock { $scriptBlock }
#Invoke-Command -ComputerName $computer -ScriptBlock { $scriptBlock }
What am I doing wrong with Invoke-Command?
Thanks!
You are passing in a scriptblock with a string. When it will call it, it will basically have the same effect as writing $scriptBlock in your terminal. You have to actually execute the string in the scriptblock. You could use Invoke-Expression.
[string] $computer = "lumen"
[scriptblock] $scriptBlock = { "cd C:\Scripts\Update-Apps; ./Update-Apps" | Invoke-Expression }
$session = New-PSSession -ComputerName $computer
Invoke-Command -Session $session -ScriptBlock $scriptblock
Or you could write directly your command as PowerShell code in the scriptblock.
[string] $computer = "lumen"
[scriptblock] $scriptBlock = { cd C:\Scripts\Update-Apps; ./Update-Apps }
$session = New-PSSession -ComputerName $computer
Invoke-Command -Session $session -ScriptBlock $scriptblock
It doesn't have to be a string.

Executing program on remote with Powershell

I'm having trouble executing a program on a remote server. This is my command
$appCommand = "`"c:\SIDApps\{0}\{1}.exe`"" -f $destinationDirectory, $processToKill
Invoke-Command -ComputerName $remoteComputer -Credential $credentials -ScriptBlock { $args[0] } -ArgumentList $appCommand
That prints out "c:\SIDApps\FFMBC_Drone\GCUKTransCodeServiceFFMBCApp.exe" (Including the quotes) in the output window, but does not actually execute the program
If you only output $args[0] in the ScriptBlock it will only display the path not run the command.
Have you tried this ?
$appCommand = "c:\SIDApps\{0}\{1}.exe" -f $destinationDirectory, $processToKill
Invoke-Command -ComputerName $remoteComputer -Credential $credentials -ScriptBlock { Start-Process $args[0] } -ArgumentList $appCommand

PSSession will not change the working directory

I have a script I'm trying to implement. If I run each line seperately, it works just fine. But if I place it in a function, or run with PowerGUI or Powershell ISE, it errors out. The problem is the script isn't changing the working directory, therefore the file that Invoke-Command is calling isn't found.
$DLUpdate = New-PSSession -ComputerName msmsgex10wprd03 -Credential $DLUpdateCred -Name DLUpdate
Enter-PSSession -Session $DLUpdate
$UpdateDLPath = 'c:\users\mascott2\Desktop\Distrolist\Updates\'
Set-Location $UpdateDLPath
Invoke-Command -ScriptBlock {cmd.exe "/c updatedls.bat"}
Exit-PSSession
Remove-PSSession -Name DLUpdate
You shouldn't be using Enter-PSSession in a script like that. Put all the commands you want into the scriptblock you use with Invoke-Command and run it against your session:
$DLUpdate = New-PSSession -ComputerName msmsgex10wprd03 -Credential $DLUpdateCred -Name DLUpdate
Invoke-Command -Session $DLUPdate -ScriptBlock {
$UpdateDLPath = 'c:\users\mascott2\Desktop\Distrolist\Updates\'
Set-Location $UpdateDLPath
cmd.exe "/c updatedls.bat"
}
Remove-PSSession -Name DLUpdate

Running Batch Script on remote Server via PowerShell

I need to connect to some remote servers from a client (same domain as the servers) once connected, I need to run a batch file:
I've done so with this code:
$Username = 'USER'
$Password = 'PASSWORD'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
try {
Invoke-Command -ComputerName "SERVER1" -Credential $Cred -ScriptBlock -ErrorAction Stop {
Start-Process "C:\Users\nithi.sundar\Desktop\Test.bat"
}
} catch {
Write-Host "error"
}
This script does not give any errors, but it doesn't seem to be executing the batch script.
any input on this would be greatly appreciated.
Try replacing
invoke-command -computername "SERVER1" -credential $Cred -ScriptBlock -ErrorAction stop { Start-Process "C:\Users\nithi.sundar\Desktop\Test.bat" }
with
Invoke-Command -ComputerName "Server1" -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Users\nithi.sund
ar\Desktop\Test.bat'"}
It's not possible that the code you posted ran without errors, because you messed up the order of the argument to Invoke-Command. This:
Invoke-Command ... -ScriptBlock -ErrorAction Stop { ... }
should actually look like this:
Invoke-Command ... -ErrorAction Stop -ScriptBlock { ... }
Also, DO NOT use Invoke-Expression for this. It's practically always the wrong tool for whatever you need to accomplish. You also don't need Start-Process since PowerShell can run batch scripts directly:
Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
C:\Users\nithi.sundar\Desktop\Test.bat
} -Credential $Cred -ErrorAction Stop
If the command is a string rather than a bare word you need to use the call operator, though:
Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
& "C:\Users\nithi.sundar\Desktop\Test.bat"
} -Credential $Cred -ErrorAction Stop
You could also invoke the batch file with cmd.exe:
Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
cmd /c "C:\Users\nithi.sundar\Desktop\Test.bat"
} -Credential $Cred -ErrorAction Stop
If for some reason you must use Start-Process you should add the parameters -NoNewWindow and -Wait.
Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
Start-Process 'C:\Users\nithi.sundar\Desktop\Test.bat' -NoNewWindow -Wait
} -Credential $Cred -ErrorAction Stop
By default Start-Process runs the invoked process asynchronously (i.e. the call returns immediately) and in a separate window. That is most likely the reason why your code didn't work as intended.

How to run script on remote pc

I am trying to run a powershell script on remote pc . if i put the path in variable it does not run, but if i put the path as string it works. Not sure why.
This works
Invoke-Command -computername $serverName -scriptblock {& ("C:\_Project\test.ps1") }
This does not work
$server = "test"
$script = "C:\_Project\test.ps1"
Invoke-Command -computername $server -scriptblock {& ( $script) }
I would like to get the 2nd one working
Variables need to be passed to script blocks as arugments or they won't be available in the scriptblock. Here is how to do it.
$server = "test"
$script = "C:\_Project\test.ps1"
Invoke-Command -computername $server -ArugmentList $Script -scriptblock {
Param ($Script)
& ( $script)
}
Try this:
$server = "test"
$script = [ScriptBlock]::Create("& C:\_Project\test.ps1")
Invoke-Command -computername $server -scriptblock $script