标签:enter tar rap VID string min ogr man bytes
powersheel远程连接密码加密连接高级玩法
1.ConvertTo-SecureString 和 ConvertFrom-SecureString 命令都支持选项 -Key。在处理密码时通过使用 Key 选项可以提供额外的安全性,并且允许在不同的环境中使用密码文件。
先生成 32 位的 Key 并保存在文件 aes.key 中:
$keyFile = "D:\aes.key"
$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 "D:\pwd.txt"
}
使用密码文件创建和 Key 文件创建 Credential 信息:
$userName = "YourUserName"
$passwdFile = "D:\pwd.txt"
$keyFile = "D:\aes.key"
$key = Get-Content $keyFile
$Cred = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $userName, (Get-Content $passwdFile | ConvertTo-SecureString -Key $key)
通过这种方法,把 pwd.txt 和 aes.key 文件拷贝到其它的机器上也是可以工作的。但是我们需要额外维护一个 key 文件的安全,这一般通过设置文件的访问权限就可以了。
$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'
unction connRemoteSever {
# 连接远程服务器
Param (
$remoteServer,$cred
)
#$passwordSecure = ConvertTo-SecureString $pwd -AsPlainText -Force
#$cred = New-Object pscredential($userName, $passwordSecure)
$mySession = New-PSSession -ComputerName $remoteServer -Port 55985 -Credential $cred # 这边的Port默认是5985、5986
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"
}
标签:enter tar rap VID string min ogr man bytes
原文地址:https://www.cnblogs.com/jbps/p/10346482.html