在日常的维护工作,经常需要读取或者写入一些敏感数据或者密码,那应该如何操作呢?
其实在PS中安全性方面做得很好,read-host提供了一个-asSecureString参数,可以SecureString存储、调用此部分数据。
PS C:\Windows\system32> $username = Read-Host "username"
username: shawnshi
PS C:\Windows\system32> $username
shawnshi
PS C:\Windows\system32> $pwd = Read-Host -asSecureString "Password"
Password: *****
PS C:\Windows\system32> $PWD
System.Security.SecureString
从上面可以看出,整个过程还是相当安全的。
可能有人会问,如果需要读取SecureString中的内容应该怎么做呢,其实在.net中是有现成的方法的。
PS C:\Windows\system32> [Runtime.InteropServices.Marshal]::`
>> PtrToStringAuto([Runtime.InteropServices.Marshal]::`
>> SecureStringToBSTR($pwd))
>>
12345