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.
Related
how to obtain process module on remote computers using powershell, Get-Process -Module gives it only for local machine. basically i want to obtain absolute paths of .dll files that are being loaded/used by processes running on that remote server/computer.
Running invoke-Command should work.
C:\> Invoke-Command -ScriptBlock {Get-Process -Module} -ComputerName comp1
You are not able to pass the -ComputerName switch to Get-Process unfortunately.
C:\> Get-Process -Module -ComputerName comp1
Get-Process : Exception getting "Modules" or "FileVersion": "This feature is not
supported for remote computers.".
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
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
I am working on a remote machine from my desktop and I have the following script :
Invoke-Command -computername $name -authentification default
-credential $creds1
-scriptblock {net use \share $password2 /user:otherdomain\otheruser}
Then I get A specified logon session does not exist. It may have already been terminated. This thing is frustrating because if I run net use \\share $password2 /user:otherdomain\otheruser directly on the remote machine it works perfectly. Is there a workaround or did I miss something ?
You need to pass the variable $password2 into the script block, otherwise its value will be empty:
Invoke-Command -Computer $name ... -ScriptBlock {
net use \\host\share $args[0] /user:otherdomain\otheruser
} -ArgumentList $password2
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