zl程序教程

您现在的位置是:首页 >  其它

当前栏目

PowerShell 相关常用命令(update...)

相关 ... 常用命令 update PowerShell
2023-09-11 14:15:23 时间

 

# PowerShell提示系统禁止执行脚本解决方法(报错提示)

 

 

## 获得当前的执行策略

Get-executionpolicy 

>> Bypass 策略完全不限制

>> Restricted  策略不允许任何脚本运行

>> AllSigned  可以运行脚本,但要求所有脚本和配置文件由可信发布者签名,包括在本地计算机上编写的脚本。

>> RemoteSigned  可以运行脚本,但要求从网络上下载的脚本和配置文件由可信发布者签名; 不要求对已经运行和已在本地计算机编写的脚本进行数字签名。

>> Unrestricted  可以运行未签名脚本。(危险!)

>> Undefined  表示运行级别尚未定义。但此时实际是执行上面提到的系统默认运行级别。

## 修改当前执行策略

Set-ExecutionPolicy RemoteSigned  (管理员方式执行Powershell)

 

【远程相关】

## 查看TrustedHost列表
gci wsman::localhost\client\trustedhosts

 

## 添加TrustedHost列表

Set-item wsman:localhost\client\trustedhosts –value *                               # 允许自己访问所有
Set-item wsman:localhost\client\trustedhosts –value sever1,server2         # 允许自己访问别人de计算机名
Set-item wsman:localhost\client\trustedhosts –value *.testj.com                # 允许自己访问的域名

Set-item wsman:localhost\client\trustedhosts –value 192.168.X.X            # 允许自己访问的IP地址

PS:如果自己谁都不允许

Set-item wsman:localhost\client\trustedhosts –value -

 

 

# 远程其他电脑的PS

需要开启端口:5985

Enable-PSremoting/Disable-PSremoting  # 启动允许PS远程,且网络位置不能被设置为public,因为Windows 防火墙例外不能在网络位置是public时被启用。

test-wsman -computername localhost  # 测试服务器上的WINRM服务是否在运行

 

Enter-PSSession X.X.X.X -Credential administrator  # 远程X.X.X.X 电脑的PS

 

## 关闭PS远程

Disable-PSremoting  # 关闭

Set-Service winrm -StartupType Manual  # 服务设置为手动

Stop-Service winrm  # 停止服务

# 移除默认情况下WinRM在5985端口设置的监听器

Get-ChildItem WSMan:\localhost\Listener –Recurse | Foreach-Object { $_.PSPath } | Where-Object { (Get-Item "$_\Port").Value -eq 5985 } | Remove-Item 

 

## 查看WinRM正在侦听的特定IP:

winrm enumerate winrm/config/listener  Or  winrm e winrm/config/listener

 

## 修改WinRM端口

WinRM默认的http端口是5985,https端口5986
winrm set winrm/config/listener?Address=+Transport=HTTP ‘@{Port=“1212”}’
或 set-item wsman:\localhost\listener\listener\port 1212