zoukankan      html  css  js  c++  java
  • powersheel远程连接方法操作

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

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

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

    	$keyFile = "C:powersheelaes.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:powersheelpwd.txt"
    

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

    	$userName = "YourUserName"
    	$passwdFile = "C:powersheelpwd.txt"
    	$keyFile = "C:powersheelaes.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:powersheelpwd.txt"
    	$keyFile = "C:powersheelAES.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:webkpi_dev" "D:webBackupkpi_dev_$backtime" -Recurse -Force
    
    				Write-Host '-----------3.删除IIS绩效后端-----------'
    				#Remove-Item -Path $DelFilePath -Recurse -Force
    
    				Write-Host '-----------4.复制最新绩效后端到IIS发布的文件夹-----------'
    				#Copy-Item -Path "D:webWebFTPpublish_kpi_BackEnd_dev$banben*" "D:webkpi_devBackEnd" -Recurse -Force
    
    				Write-Host '-----------5.复制web.config到IIS发布的文件夹-----------'
    				#Copy-Item -Path "D:webkpi_devweb.config" "D:webkpi_devBackEnd" -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:webkpi_devBackEnd*'
    $DelFileExcludePath='D:webkpi_devBackEndwwwroot*'
    $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:webkpi_dev" "D:webBackupkpi_dev_$backtime" -Recurse -Force
    
    Write-Host '-----------3.删除D:webkpi_devBackEnd 文件夹下除了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:webWebFTPpublish_kpi_BackEnd_dev$banben*" "D:webkpi_devBackEnd" -Recurse -Force
    
    Write-Host '-----------5.复制web.config到IIS发布的文件夹-----------'
    #Copy-Item -Path "D:webkpi_devweb.config" "D:webkpi_devBackEnd" -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:webBackup   #查看当前远程服务器这个文件夹下得目录列表情况
    
      } -credential $Cred
    
  • 相关阅读:
    java类中为什么设置set和get方法操作属性
    Java常用排序算法+程序员必须掌握的8大排序算法+二分法查找法
    自学Zabbix之路15.3 Zabbix数据库表结构简单解析-Triggers表、Applications表、 Mapplings表
    自学Zabbix之路15.2 Zabbix数据库表结构简单解析-Items表
    自学Zabbix之路15.1 Zabbix数据库表结构简单解析-Hosts表、Hosts_groups表、Interface表
    21 Zabbix系统性能优化建议
    20 Zabbix 利用Scripts栏目对Hosts远程执行命令
    19 Zabbix web监控实例
    18 Zabbix 新增map中的icon图标
    自学Zabbix3.12.6-动作Action-Escalations配置
  • 原文地址:https://www.cnblogs.com/jbps/p/10348270.html
Copyright © 2011-2022 走看看