随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

脚本中使用密文密码

将明文转换为密文

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
复制代码

 

posted on   momingliu11  阅读(3395)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示