脚本中使用密文密码
将明文转换为密文
1 #将明文转换为密文 2 $storage = "d:\pass.txt" 3 $mysecret = 'mypassword.' 4 5 $mysecret | 6 ConvertTo-SecureString -AsPlainText -Force | 7 ConvertFrom-SecureString | 8 Out-File -FilePath $storage
#将密文转换回明文(只能在加密时使用的计算机上解密)
1 #将密文转换回明文 2 $storage = "d:\pass.txt" 3 4 $secureString = Get-Content -Path $storage | ConvertTo-SecureString 5 $ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($secureString) 6 $mysecret = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) 7 $mysecret
#将加密后的密码保存在PowerShell脚本中使用
1 #此为加密后的密文密码 2 $storage = "01000000d08c9ddf0115d1118c7a00c04fc297eb01000000142c3028558376439749dc9d50f6124a00000000020000000000106600000001000020000000bfb113b1fda4d92cc94eb770b2fda81956fa0d4ab633d7e736af63f7f5e270f6000000000e800000000200002000000048de85352a65d2deb01be43affdc25b54e598cce6f8d9f936fdfa4be16752b8720000000419c3990839f7028fc2056fd97a404f3e79ed3fe22770d66a0bc8a85f78f7994400000005780e2034bd644d68a9acfa2b385d71b24f68b0acd1358553add8b7c3b0ed597aff53bf78cc44ce60ee2e6228b154b4ca520d50c9133843397f12033c60b6f73" 3 #对密文进行解密 4 $secureString = $storage | ConvertTo-SecureString 5 $ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($secureString) 6 $serverpass = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptr) 7 $Password = ConvertTo-SecureString $serverpass -AsPlainText –Force 8 $UserName = "administrator" #定义管理员账户名称 9 $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password) 10 11 Enter-PSSession -ComputerName 10.10.12.12 -Credential $cred
分类:
PowerShell
标签:
SecureString
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?