Invoke-Command invocation of server script error - powershell

I have this script in the client computer that try to run a script from the server computer:
try {
$s = New-PSSession -ComputerName "name" -Authentication CredSSP -Credential $credential
Enter-PSSession -Id $s.Id
Set-ExecutionPolicy ByPass -Scope CurrentUser
Invoke-Command -Session $s -FilePath c:\release1.ps1
} catch {
Write-Error "Error occurred: " $_.Exception.ToString()
} finally {
Exit-PSSession
Remove-PSSession $s }
the script in the server is something like this
Set-Alias vmrun "C:\Program Files (x86)\VMware\VMware Workstation\vmrun.exe"
Start-Process -NoNewWindow -FilePath vmrun -ArgumentList " -T ws revertToSnapshot M:\myvm.vmx Release1"
but I got the error
Write-Error : A positional parameter cannot be found that accepts argument 'System.Management.Automation.ItemNotFoundException: Cannot
find path 'C:\release1.ps1' because it does not exist.

-FilePath must specify a path to a file on the local computer. The file is read locally, then passed to the remote computer as a block of code.
See http://technet.microsoft.com/en-us/library/hh849719.aspx:
Runs the specified local script on one or more remote computers. Enter the path and file name of the script, or pipe a script path to Invoke-Command. The script must reside on the local computer or in a directory that the local computer can access. Use the ArgumentList parameter to specify the values of parameters in the script.
The solution is to put release1.ps1 on the local computer. Or if it must be on the remote computer, then put it in a share that is accessible to the local computer and access it with a UNC path.
Invoke-Command -Session $s -FilePath \\name\share\release1.ps1

Related

Invoke-Command executes on remote machine without having an effect

I'm trying to run a command on a VM using Invoke-Command. The command should stop a program that processes jobs after it finishes its current job. It works if I run it in the terminal using RDC.
& 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe' /stop
But if I run it from a different machine using Invoke-Command nothing seems to happen.
$session = New-PSSession -ComputerName 'hostname' -Credential (Get-Credential)
Invoke-Command -Session $session -ScriptBlock {
& 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe' /stop
}
However Process Monitor shows the command come in for both cases, but the program is still running.
I have also tried using Start-Process with the same result, i.e. it works in the terminal on the VM but not using Invoke-Command.
Start-Process -FilePath 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe' -ArgumentList '/stop'
I've been stuck for many days and I've exhausted my googlable knowledge for this problem.
Are you sure that file exists on the remote computer?
For simplicity, I rewrote your command to a known executable that is always there in Windows and returns unique info for any given computer.
C:\> & 'C:\Windows\system32\HOSTNAME.EXE'
server1
C:\> icm {& 'C:\Windows\system32\HOSTNAME.EXE'}
server1
C:\> icm {& 'C:\Windows\system32\HOSTNAME.EXE'} -ComputerName server2
server2
Here's your script with some error handling.
$session = New-PSSession -ComputerName 'hostname' -Credential (Get-Credential)
Invoke-Command -Session $session -ScriptBlock {
$exe = 'C:\Program Files\Autodesk\Vault Client 2021\Explorer\JobProcessor.exe'
$ok = Test-Path $exe
if ($ok) {& $exe /stop} else {
Write-Warning "EXE not present on $($env:COMPUTERNAME)!"
}
}
Learn how to add error handling and you'll be well on your way to solving your problems faster and getting more stuff done.

Power shell Invoke remote script is not working

I am trying to invoke a remote bat file from my local machine that should start and run the bat in a remote machine terminal. My script is able to connect and show me the output. However, it is invoking the remote bat file but waiting on my screen with bat file output. and my idea is the bat file should invoke and running in the remote machine rather than showing the output on my terminal local terminal. What should be the way here?
loop{
Invoke-Command -ComputerName $computer -credential $cred -ErrorAction Stop -ScriptBlock { Invoke-Expression -Command:"cmd.exe /c 'C:\apacheserver.bat'" }
}
From what I understand you want the .bat file to not show you the result. If so, you should do it with the Out-Null or by redirecting STDOUT and STDERR to NULL.
IE: Invoke-Expression -Command:"cmd.exe /c 'C:\apacheserver.bat'" | Out-Null
If I'm understanding correctly, you want to suppress the output from the .bat file inside your local console? If so, redirecting to some form of $null is the way to go. You also shouldn't need Invoke-Expression as Invoke-Command can run the file directly:
Invoke-Command -ComputerName $computer -Credential $cred -ErrorAction Stop -Scriptblock {
cmd.exe /c 'C:\apacheserver.bat' | Out-Null
}
You could also use > $null instead of Out-Null if you prefer.
If you want to redirect the output of the Invoke-Command call to the remote console instead, that kind of defeats the purpose of the cmdlet. You could try redirecting the console output to a file on the remote computer if you want a local record:
$remoteFile = '\\server\C$\Path\To\Output.txt'
Invoke-Command -ComputerName $computer -Credential $cred -ErrorAction Stop -Scriptblock {
cmd.exe /c 'C:\apacheserver.bat' > $remoteFile
}
If I understand you, you want to run the script in the background and not wait for it:
Invoke-Command $computer -Credential $cred { start-process C:\apacheserver.bat }

Running a remote cmd command in PowerShell

I uploaded some files to a remote host with PowerShell, by FTP. On this host runs Windows 7 Embedded.
It turns out there is EWF (Enhanced Write Filter). So after a restart the uploaded files were gone. For saving the changes it needs commit them in cmd (at the remote host) by: ewfmgr d:-commit How can I include this command in my PowerShell code?
The code:
Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts -Value * -Force
Restart-Service WinRm
Test-WSMan $line
Invoke-Command -ComputerName $line -scriptblock {cmd.exe /c "ewfmgr d: -commit"} -credential $FTPCredential
When I run Enable-PSRemoting -Force manually on the remote computer, it works, but it is uncomfortable and take lots of time. Is there another way to do this once for many hosts simultaneously?
Example-Code:
$session = New-PSSession -ComputerName yourRemoteComputer
Invoke-Command -Session $session -Scriptblock {ewfmgr d: -commit}
Remove-PSSession -Session $session
You have to enable Powershell Remoting on your host to invoke a command like this (https://technet.microsoft.com/en-us/library/ff700227.aspx)
If you need to transmit Credentials to your remote host, you can add the -Credential-Parameter to New-PSSession. This article describes how to add valid Credentials to your script (https://technet.microsoft.com/en-us/library/ff700227.aspx)
Greetings, Ronny

PowerShell - Execute PowerShell Script on the C: drive of a remote server

PowerShell Version 5 build 10586
I am using the following code to remote connect to a server from my local PC
$cimSession = New-CimSession -ComputerName "SERVERNAME.DOMAIN.COM"
Invoke-Command -ComputerName SERVERNAME -ScriptBlock {Get-ChildItem “C:\Temp\ps”}
Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1"
Remove-CimSession -CimSession $cimSession
The first command is able to run successfully and sees the PowerShell file on the remote server.
The second command fails with an error:
Invoke-Command : Cannot find path '\\SERVERNAME\c$\Temp\ps\PS_SCRIPT_FILE.ps1' because it does not exist.
Is there another way to call/run a PowerShell script on the C Drive of a remote server?
I have granted my account full access to the specified file.
I have also tried to share the specified folder and given myself Read/Write access to the folder.
I have changed the file path to the share path and get the same result.
Invoke-Command -ComputerName SERVERNAME -FilePath "\\SERVERNAME\ps\PS_SCRIPT_FILE.ps1"
Try this:
Invoke-Command -ComputerName SERVERNAME -ScriptBlock { Invoke-Command -FilePath "C:\Temp\ps\PS_SCRIPT_FILE.ps1" }
In your existing code the -FilePath parameter is processed on the calling machine. By including that as a parameter within the ScriptBlock it should be processed on the target machine.

Invoke-Command with script block is not working on remote machine with no error

I am trying to call a batch file located in local machine executing the below PowerShell command from remote computer.
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
Start-Process "c:\installagent.bat"
} -Credential abc\XXX
It's not giving any error but nothing happened on the remote computer.
If I run the batch file from local machine, it's working fine.
You can't run a local file on a remote host like that. If the account abc\XXX has admin privileges on your local computer (and access to the administrative shares is enabled) you could try this:
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
param($myhost)
Start-Process "\\$myhost\c$\installagent.bat"
} -ArgumentList $env:COMPUTERNAME -Credential abc\XXX
Otherwise you'll have to copy the script to the remote host first:
Copy-Item 'C:\installagent.bat' '\\XXXXXX\C$'
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
Start-Process "c:\installagent.bat"
} -Credential abc\XXX
Also, I'd recommend using the call operator (&) instead of Start-Process for running the batch file:
Invoke-Command -ComputerName XXXXXX -ScriptBlock {
& "c:\installagent.bat"
} -Credential abc\XXX
That way Invoke-Command should return the output of the batch file, giving you a better idea of what's going on.
Or, you could simply use psexec:
C:\> psexec \\XXXXXX -u abc\XXX -c installagent.bat