enter-pssession invoke-command,什么时候使用? [英] enter-pssession invoke-command, when to use?

查看:45
本文介绍了enter-pssession invoke-command,什么时候使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本来停止和启动两个远程服务器中的服务.这是我的问题,

在我的脚本中,我执行了 new-pssession 并使用 invoke-command 来停止和启动服务.

我需要使用 enter-pssession 吗?

更新:这是我的脚本需要做的.

在 server1 上,我需要停止和启动两个服务.在 server2 上,我只需要停止和启动一项服务.

# foreach for server 1 因为我需要停止和启动两个服务.为服务器 1 创建了一个会话foreach($service 中的 $service){$session = New-PSSession -ComputerName $serverName -Credential $credInvoke-Command -Session $session -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service删除-pssession -session $session}# 为服务器 2 创建了一个会话.我只需要停止和启动服务器 2 中的一项服务$session = New-PSSession -ComputerName $serverName -Credential $credInvoke-Command -Session $session -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service删除-pssession -session $session

这是正确的做法吗?

解决方案

Enter-PSSession - 由于这是一个交互式会话,您可以在控制台输入您想要的内容并立即在控制台中看到结果.(就像CMD).如果它只有 2 个服务器,那么您可以使用 enter-pssession 但它始终是串行的,这意味着您在一台服务器上执行某些操作然后转移到另一台服务器上.

New-PSSession - 创建到远程服务器的持久连接,通常用于在大型脚本\工作流的各个阶段有一系列命令在多个服务器上运行时使用.>

示例:

$s1, $s2 = New-PSSession -ComputerName Server1,Server2Get-Service -Name Bits #on localhostInvoke-Command -session $s1 -scriptblock { # 远程命令在这里}Get-Process #on 本地主机Invoke-Command -session $s1 -scriptblock { # 远程命令在这里}删除-pSSession -session $s1 #on localhost

如果您只想停止\启动几个服务,那么您可以在不打开持久连接的情况下执行此操作.

示例:

Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {Stop-Service -Name Bits}

I am writing a script to stop and start services in two remote servers. Here's my question,

in my script I did new-pssession and used invoke-command to stop and start services.

Do I need to use enter-pssession?

Updates: Here's what my script needs to do.

on server1, I need to stop and start two services. on server2, I need to stop and start just one service.

# foreach for server 1 since I need to stop and start two services. created a session for server 1
foreach($service in $services){

    $session = New-PSSession -ComputerName $serverName -Credential $cred
    Invoke-Command -Session $session -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service
    remove-pssession -session $session

}

# created a session for server 2. I need to stop and start just one service in server 2
$session = New-PSSession -ComputerName $serverName -Credential $cred
Invoke-Command -Session $session -ScriptBlock {param($service) Stop-Service -Name $service} -ArgumentList $service
remove-pssession -session $session

is this the right way to do it?

解决方案

Enter-PSSession - Since this is an interactive session you type what you want at the console and immediately see the results in the console.(just like CMD). If its just 2 servers then you can use enter-pssession but it is always going to be serial meaning you do something on one server then you move onto another.

New-PSSession - creates a persistent connection to a remote server and is generally used when you have a series of commands to run on multiple servers at various stages of a larger script\workflow.

Example:

$s1, $s2 = New-PSSession -ComputerName Server1,Server2
Get-Service -Name Bits                #on localhost
Invoke-Command -session $s1 -scriptblock { # remote commands here }
Get-Process                           #on localhost
Invoke-Command -session $s1 -scriptblock { # remote commands here }
Remove-pSSession -session $s1 #on localhost

if you just want to stop\start a couple of services then you can do this without opening a persistent connection.

Example:

Invoke-Command -ComputerName (Get-Content Machines.txt) -ScriptBlock {Stop-Service -Name Bits}

这篇关于enter-pssession invoke-command,什么时候使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆