简体   繁体   中英

Powershell commands only work when I run selection in ise

I'm new to powershell and I'm using the ps ise to write a ps script that executes a .exe remotely on another server.

When I click run in the ise, the script doesn't appear to do anything. However, when I right-click on the individual lines of code in the ps script, I can see within the ps console and the target server that each line of code works.

What am I missing to get it to work when I just click run script in the ise or when I run the script from the command line?

Here is what the script basically looks like:

Enter-PSSession -ComputerName MyServer1
D:\Somefolder\On\TargetServer\theremote.exe
Exit-PSSession

I appreciate any help or guidance on this.

Let me explain you.

Enter-PSsession basically creates an interactive session with the remote computer.

So once the session has been created, you can run all the commands which interactive will return the result from the remote computer.

You should use invoke-command to get your job done . It should be like:

Invoke-Command -ComputerName MyServer1 -ScriptBlock {& 'D:\Somefolder\On\TargetServer\theremote.exe'}

Now, by default it uses Windows Authentication. If you want to specify any credentials then you can use -Credential switch.

Or another method is to create a remote session first using new-pssession , then store the session in a variable. Finally, you can specify the session in the invoke-command as -SessionName

As per the comments, this is how you can pass the parameters (I am passing only 1 parameter here):

Invoke-Command -ComputerName MyServer1 -ScriptBlock { param ($param1) & 'D:\Somefolder\On\TargetServer\theremote.exe' -something $param1 } -ArgumentList "param1Value"

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM