powersheel远程连接密码加密连接高级玩法

ConvertTo-SecureStringConvertFrom-SecureString 命令都支持选项 -Key。在处理密码时通过使用 Key 选项可以提供额外的安全性,并且允许在不同的环境中使用密码文件。

先生成 32 位的 Key 并保存在文件 aes.key 中:

	$keyFile = "C:\powersheel\aes.key"  #加密的key准备放在这个D盘,最好放在一个文件夹里面
$key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($key)
$key | out-file $keyFile

使用 Key 生成并保存密码文件:

	Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString -key $key | Out-File "C:\powersheel\pwd.txt"

使用密码文件创建和 Key 文件创建 Credential 信息:

	$userName = "YourUserName"
$passwdFile = "C:\powersheel\pwd.txt"
$keyFile = "C:\powersheel\aes.key"
$key = Get-Content $keyFile
$Cred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $userName, (Get-Content $passwdFile | ConvertTo-SecureString -Key $key)

通过这种方法,把 pwd.txtaes.key 文件拷贝到其它的机器上也是可以工作的。但是我们需要额外维护一个 key 文件的安全,这一般通过设置文件的访问权限就可以了。

以上是将密码通过32位的key加密到文件里面,下面的第一个远程连接会用到

第1种方法远程连接(密码加密):

	$userName = "administrator"
$passwdFile = "C:\powersheel\pwd.txt"
$keyFile = "C:\powersheel\AES.key"
$key = Get-Content $keyFile
$cred = New-Object -TypeName System.Management.Automation.PSCredential
-ArgumentList $userName, (Get-Content $passwdFile | ConvertTo-SecureString -Key $key)
$remoteServer='www.bai.com' #连接方法
function connRemoteSever {
#连接远程服务器
Param ($remoteServer,$cred)
#$passwordSecure = ConvertTo-SecureString $pwd -AsPlainText -Force
#$cred = New-Object pscredential($userName, $passwordSecure)
Write-Host '-----------powersheel默认Port是5985、5986-----------'
$mySession = New-PSSession -ComputerName $remoteServer -Port 55985 -Credential $cred
return $mySession
} #连接到远程服务器
$webSession=connRemoteSever $remoteServer $cred Invoke-Command -Session $webSession -ScriptBlock { dir d:
Write-Host '-----------1.在覆盖网站内容前,先停止网站-----------'
#Import-Module WebAdministration; Stop-WebAppPool -Name "$appPoolName"
#Import-Module WebAdministration; Stop-WebSite -Name "$appWebSiteName" Write-Host '-----------2.备份IIS中绩效的网站-----------'
#Copy-Item -Path "D:\web\kpi_dev" "D:\web\Backup\kpi_dev_$backtime" -Recurse -Force Write-Host '-----------3.删除IIS绩效后端-----------'
#Remove-Item -Path $DelFilePath -Recurse -Force Write-Host '-----------4.复制最新绩效后端到IIS发布的文件夹-----------'
#Copy-Item -Path "D:\web\WebFTP\publish_kpi_BackEnd_dev\$banben\*" "D:\web\kpi_dev\BackEnd\" -Recurse -Force Write-Host '-----------5.复制web.config到IIS发布的文件夹-----------'
#Copy-Item -Path "D:\web\kpi_dev\web.config" "D:\web\kpi_dev\BackEnd\" -Recurse -Force Write-Host '-----------6.启动网站-----------'
#Import-Module WebAdministration; Start-WebAppPool -Name "$appPoolName"
#Import-Module WebAdministration; Start-WebSite -Name "$appWebSiteName" }

第2种方法远程连接(密码不加密):

$userName = 'opsadmin'
$pwd = 'ywX*'
$remoteServer='192.168.0.100' function connRemoteSever {
# 连接远程服务器
Param ($userName,$pwd,$remoteServer,$port) #参数 $passwordSecure = ConvertTo-SecureString $pwd -AsPlainText -Force
$cred = New-Object pscredential($userName, $passwordSecure)
$mySession = New-PSSession -ComputerName $remoteServer -Port 5985 -Credential $cred
return $mySession
} # 连接到远程服务器
$webSession=connRemoteSever $pwd $userName $remoteServer $banben='V'+$env:BUILD_NUMBER #这边是在windows中的jenkins中使用到的
$backtime=Get-Date -Format 'yyyy_M_d_Hms'
$DelFilePath='D:\web\kpi_dev\BackEnd\*'
$DelFileExcludePath='D:\web\kpi_dev\BackEnd\wwwroot*'
$appPoolName='kpi_dev'
$appWebSiteName='kpi_dev' Invoke-Command -Session $webSession -ScriptBlock {
param($appPoolName,$appWebSiteName,$backtime,$DelFilePath,$banben) #以下都是一些操作都是一些基本操作命令,大家有用到的话借鉴下
dir d:
Write-Host '-----------1.在覆盖网站内容前,先停止网站-----------'
#Import-Module WebAdministration; Stop-WebAppPool -Name "$appPoolName"
#Import-Module WebAdministration; Stop-WebSite -Name "$appWebSiteName" Write-Host '-----------2.备份IIS中绩效的网站-----------'
#Copy-Item -Path "D:\web\kpi_dev" "D:\web\Backup\kpi_dev_$backtime" -Recurse -Force Write-Host '-----------3.删除D:\web\kpi_dev\BackEnd 文件夹下除了wwwroot文件夹,其他的全删除-----------'
#Remove-Item -Path $DelFilePath -Recurse -Force Get-ChildItem -Path $DelFilePath -Recurse -exclude wwwroot | Select -ExpandProperty FullName |
Where {$_ -notlike $DelFileExcludePath} |
sort length -Descending |
Remove-Item -force Write-Host '-----------4.复制最新绩效后端到IIS发布的文件夹-----------'
#Copy-Item -Path "D:\web\WebFTP\publish_kpi_BackEnd_dev\$banben\*" "D:\web\kpi_dev\BackEnd\" -Recurse -Force Write-Host '-----------5.复制web.config到IIS发布的文件夹-----------'
#Copy-Item -Path "D:\web\kpi_dev\web.config" "D:\web\kpi_dev\BackEnd\" -Recurse -Force Write-Host '-----------6.启动网站-----------'
#Import-Module WebAdministration; Start-WebAppPool -Name "$appPoolName"
#Import-Module WebAdministration; Start-WebSite -Name "$appWebSiteName" } -ArgumentList $appPoolName,$appWebSiteName,$backtime,$DelFilePath,$banben

第3种方法远程连接:

$Username = 'opsadmin' #远程电脑的用户名
$Password = 'ywX*^R' #远程电脑的密码
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName 192.168.0.100 -Port 5985 -ScriptBlock { dir D:\web\Backup #查看当前远程服务器这个文件夹下得目录列表情况 } -credential $Cred

powersheel远程连接方法操作的更多相关文章

  1. Python 使用Python远程连接并操作InfluxDB数据库

    使用Python远程连接并操作InfluxDB数据库 by:授客 QQ:1033553122 实践环境 Python 3.4.0 CentOS 6 64位(内核版本2.6.32-642.el6.x86 ...

  2. Java java jdbc thin远程连接并操作Oracle数据库

    JAVA jdbc thin远程连接并操作Oracle数据库 by:授客 QQ:1033553122 测试环境 数据库:linux 下Oracle_11g_R2 编码工具:Eclipse 编码平台:W ...

  3. Mysql开启远程连接方法

    分类: 数据库开发技术 解决MySQL不允许从远程访问的方法 开启 MySQL 的远程登陆帐号有两大步: 1.确定服务器上的防火墙没有阻止 3306 端口. MySQL 默认的端口是 3306 ,需要 ...

  4. 基于Git项目管理客户端SourceTree的免注册安装及远程连接方法

    作为程序员,不可避免的要在github上查询代码,而在企业项目中,为了使得项目好管理需要使用项目管理客户端,所以接下来详细讲解一下基于git的sourceTree在windows系统下的安装及与Git ...

  5. redis 远程连接方法

    解决方法 1.修改redis服务器的配置文件 vi redis.conf 注释以下绑定的主机地址 # bind 127.0.0.1 或 vim  redis.conf bind  0.0.0.0 pr ...

  6. mysql配置远程连接方法之一(改表法)

    1.问题:如果在远程连接报错:1130-host ... is not allowed to connect to this MySql server,可能是你的帐号不允许从远程登陆,只能在local ...

  7. AIX加入能telnet远程连接方法的帐户

    AIX 加入该账户可以使用命令mkuser 和 SMIT 两种方法,这里有SMIT方式 1.采用root 帐户登录AIX 2.输入 smitty user 3.选择Add a User 4.输入&qu ...

  8. 配置sql server 允许远程连接

    如果要想远程连接数据库那么则需要在一个局域网中或一个路由器中才可以做到 接下来就是具体的操作检查sqlserver数据库是否允许远程连接 具体操作为 (1)打开数据库,用本地帐户登录,右击第一个选项, ...

  9. 电脑远程连接windows阿里云服务器解决卡顿【小白教程】

    我们在阿里云服务器网页上进行远程连接进行操作,会卡顿.解决办法如下: 1.登录阿里云服务器,进入服务器控制台,复制服务器IP:  2.回到桌面,打开cmd命令窗口,输入mstsc  3.在‘’计算机’ ...

随机推荐

  1. java持有对象【2】ArrayList容器续解

    此为JDK API1.6.0对ArrayList的解释. ArrayList 使用java泛型创建类很复杂,但是应用预定义的泛型很简单.例如,要想定义用来保存Apple对象的ArrayList,可以声 ...

  2. 平衡树、AVL树

    平衡树 平衡树有AVL树.红黑树.2-3树.2-3-4树 AVL树 AVL树是最早的一种平衡树,它以发明者的名字命名:AVL是一种特殊的二叉搜索树,平移保证二叉搜索树的正确. 特征 在AVL树中节点的 ...

  3. go语言练习:类型转换

    package main import "fmt" func main() { var a int var b uint var c float32 var d float64 a ...

  4. Asp.Net Core混合使用cookie和JwtBearer认证方案

    自己有时捣鼓一些小型演示项目,服务端主要是提供Web Api功能.为了便于管理,需要在服务端加一些简单的MVC网页,用管理员身份登录,做一些简单的操作. 因此需要实现一个功能,在一个Asp.Net C ...

  5. Mongodb集群与分片 1

    分片集群   Mongodb中数据分片叫做chunk,它是一个Collection中的一个连续的数据记录,但是它有一个大小限制,不可以超过200M,如果超出产生新的分片.   下面是一个简单的分片集群 ...

  6. 大于2t的磁盘分区,并格式化ext4挂载

    1:MBR分区表:(MBR含义:主引导记录) 所支持的最大卷:2T (T; terabytes,1TB=1024GB) 对分区的设限:最多4个主分区或3个主分区加一个扩展分区. 2:GPT分区表:(G ...

  7. Error Fix – Replication subscriber does not exist on the server anymore(删除Replication时报错的解决办法)

    Recently one of my client has faced weird situation related to SQL Server Replication. Their main da ...

  8. Oracle中,时间的相关操作方法

    无论是DATE还是timestamp都可以进行加减操作.可以对当前日期加年.月.日.时.分.秒,操作不同的时间类型,有三种方法: 1 使用内置函数numtodsinterval增加小时,分钟和秒2 加 ...

  9. NGUI Clip Animation (UI动画)

    效果预览 视频:http://pan.baidu.com/s/1ntr3XSt 运行环境 Unity 4.5, NGUI3.5, iTween 场景搭建 创建一个UIPanel,UIPanel下再创建 ...

  10. 【转】Linxu学习---top实践

    [原文]https://www.toutiao.com/i6591053058258502147/ 在实际开发中,有时候会收到一些服务的监控报警,比如CPU飙高,内存飙高等,这个时候,我们会登录到服务 ...