powersheel遠程鏈接密碼加密鏈接高級玩法javascript
ConvertTo-SecureString 和 ConvertFrom-SecureString 命令都支持選項 -Key。在處理密碼時經過使用 Key 選項能夠提供額外的安全性,而且容許在不一樣的環境中使用密碼文件。
java
先生成 32 位的 Key 並保存在文件 aes.key 中:web
$keyFile = "C:\powersheel\aes.key" #加密的key準備放在這個D盤,最好放在一個文件夾裏面 $key = New-Object Byte[] 32 [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($key) $key | out-file $keyFile
使用 Key 生成並保存密碼文件:windows
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.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' #鏈接方法 function connRemoteSever { #鏈接遠程服務器 Param ($remoteServer,$cred) #$passwordSecure = ConvertTo-SecureString $pwd -AsPlainText -Force #$cred = New-Object pscredential($userName, $passwordSecure) Write-Host '-----------powersheel默認Port是598五、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" }
$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
$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