I am trying to run a particular script out on a remote machine
$script:remoteSession = New-PSSession -ComputerName $script:currentnode -Credential $script:cred -ErrorAction Stop
Invoke-Command -Session $script:remoteSession -ScriptBlock {
. "$($using:RemotedriveLetter):\SilentInstaller\configureWindows.ps1" -driveLetter $using:RemotedriveLetter
}
but the above code gives the below error
"A Using variable cannot be retrieved. A Using variable can be used only with Invoke-Command, Start-Job, or InlineScript in the script workflow. When it is used with Invoke-Command, the Using variable is valid only if the script block is invoked on a remote computer."
Related
I am having an issue with running a batch file that is located on a remote server.
I have a batch file located on a remote server that i want to run that kicks off an automated Selenium test. For simplicity, let's say the name of my batch file is mybatch.bat
I have the following code in a Powershell script located on the server:
$BatchFile = "mybatch.bat"
Start-Process -FilePath $BatchFile -Wait -Verb RunAs
If I run this PowerShell script locally on the server in ISE then it runs fine and it kicks off the selenium test which takes a couple minutes to run.
Now I want to try to execute this test from another machine by using PowerShell remoting. Let's assume that remoting is already configured on the servers.
I have a PowerShell script located on another server which has the following code segment. Assume that all of the session variables have the correct information set:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
When this script runs, it does connect to the remote machine and create a remote session. It does look like it kicks off the batch file because PowerShell does not give me an error. But it does not wait for the full 3 or 4 minutes for the Selenium test to finish. It seems like it just times out. Also if I am logged onto the other machine, I don't see any Selenium web test running. No Selenium log files or results files are created on remote server as should be expected.
I was wondering what I could be doing wrong with my code.
Also, it seems that the server always returns the echo outputs of the batch file to my local machine. I see these random blinking white screen on ISE which looks like output from the batch file
$command = "powershell.exe -File $CMD -Wait"
Invoke-Command -Session $TargetSession -ScriptBlock { $command }
There are 2 issues with the above code:
$command inside the scriptblock and $command outside the scriptblock are different variables due to different scopes. The variable inside the scriptblock is thus undefined and the scriptblock will simply echo an emtpy value.
Even if $command weren't undefined, the scriptblock would still just echo its value, since it's defined as a string. PowerShell does not execute strings unless you're using something like Invoke-Expression (which you shouldn't).
This should do what you want:
$CMD = "D:\mybatch.bat"
$TargetSession = New-PSSession -ComputerName $FullComputerName -Credential $myCreds -ConfigurationName RemoteExecution
Invoke-Command -Session $TargetSession -ScriptBlock { & $using:CMD }
If you would like execute a bat file from another machine by using PowerShell Remote Session, simply enter dot and then follow by a whitespace, then enter the exact path of bat file located on that remote machine.
Example
Invoke-Command -ComputerName RemoteComputerName -Credential $credential -ScriptBlock {. "D:\Temp\Install_Something.bat"}
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
I'm trying to write a powershell script that will take a list of commands and then run them on a remote machine, I have the following code:
foreach($command in $commands)
{
Invoke-Command -computer "BNEBAK" -scriptblock{"$command"}
}
Which does not throw any error but also does not actually run the command (e.g stop-service servicename). $commands is read in from a text file passed as an argument when the script is called, I know the rest of this script works because I have been using it to do local commands with Invoke-Expression for some time.
Any help would be fantastic.
The correct code would be
$commands = #(get-content com.txt)
for($command in $commands) {
$scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock($command)
Invoke-Command -computer $computer -scriptblock $scriptblock
}