PowerShell远程重启IIS和Service

本文详细介绍如何测试远程电脑是否开启远程连接,如何开启远程服务,以及如何通过PowerShell进行远程电脑的信任设置、会话建立和自动重启IIS ApplicationPool及远程服务。

测试要连接的电脑开没开远程

Test-WsMan 192.168.1.1

或者直接在远程电脑查看

Get-Service WinRM

如果没开的话打开

Enable-PSRemoting –Force

连接之前需要信任远程的电脑

查看TrustedHosts列表

get-item wsman:\localhost\Client\TrustedHosts

将所有计算机都添加到TrustedHosts列表

set-item wsman:\localhost\Client\TrustedHosts -value *

将一个域里的所有计算机都添加到TrustedHosts列表

set-item wsman:\localhost\Client\TrustedHosts -value *.omg.com

将指定计算机名的计算机添加到TrustedHosts列表

set-item wsman:\localhost\Client\TrustedHosts -value <ComputerName>

将一个IP地址添加到TrustedHosts列表

set-item wsman:\localhost\Client\TrustedHosts -value 192.168.1.2

进入会话

Enter-PSSession 192.168.1.1 -Credential "OMG\admin"

分享链接

Windows PowerShell 帮助

about Remote Troubleshooting - PowerShell | Microsoft Docs

PowerShell 在线教程 – PowerShell 中文博客

自动使用管理员身份运行

$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi

if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
$boundPara = ($MyInvocation.BoundParameters.Keys | foreach{
'-{0} {1}' -f? $_ ,$MyInvocation.BoundParameters[$_]} ) -join ' '
$currentFile = (Resolve-Path? $MyInvocation.InvocationName).Path

$fullPara = $boundPara + ' ' + $args -join ' '
Start-Process "$psHome\powershell.exe"?? -ArgumentList "$currentFile $fullPara"?? -verb runas
return
}

自动实现重启IIS的Application Pool

$IP = '192.168.1.1'
$Username = 'omg\administrator'
$Password = 'laotie666'

$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

$s = New-PSSession $IP -Credential $Cred

Invoke-Command $s -ScriptBlock { Restart-WebAppPool  -Name "Test" }

自动重启远程的Service

Invoke-Command $IP -Credential $Cred -ScriptBlock { Get-Service -Name 'Test' | Restart-Service -Force }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Admini$trat0r

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值