[玩转系统] 如何在 PowerShell 脚本中使用加密密码文件?
作者:精品下载站 日期:2024-12-14 15:43:39 浏览:15 分类:玩电脑
如何在 PowerShell 脚本中使用加密密码文件?
要求:在 PowerShell 脚本中使用加密的密码文件。
如何使用加密密码文件在 PowerShell 中读取/写入凭据?
PnP PowerShell 等 PowerShell 模块提供了一种使用 Windows 凭据存储来保存和检索用户名和密码以在脚本中使用的机制。但是,对于其他 PowerShell 模块(例如 SharePoint Online Management Shell、AzureAD、CSOM 等),除了在脚本中以纯文本形式存储密码之外,我们没有任何直接方法来抑制密码提示。在像我们需要在Windows任务计划程序中调度脚本的情况下,为了在没有任何用户干预的情况下无人值守地执行脚本,我们可以使用这种方法:
以下是我们如何在 PowerShell 脚本中存储和读取文件中的加密密码。
必须关闭 MFA 才能使保存的凭据发挥作用!- 第 1 步: 创建加密密码文件来存储凭据
- 第 2 步: 从文件中读取加密密码并在脚本中使用它。
创建加密密码文件
基本上,我们需要从用户那里获取凭据(一次!)并将加密的密码存储在文件中。以下是将加密密码保存到文件的 PowerShell 脚本。
#function to Save Credentials to a file
Function Save-Credential([string]$UserName, [string]$KeyPath)
{
#Create directory for Key file
If (!(Test-Path $KeyPath)) {
Try {
New-Item -ItemType Directory -Path $KeyPath -ErrorAction STOP | Out-Null
}
Catch {
Throw $_.Exception.Message
}
}
#store password encrypted in file
$Credential = Get-Credential -Message "Enter the Credentials:" -UserName $UserName
$Credential.Password | ConvertFrom-SecureString | Out-File "$($KeyPath)$($Credential.Username).cred" -Force
}
#Get credentials and create an encrypted password file
Save-Credential -UserName "[email protected]" -KeyPath "C:\Scripts"
这将在给定路径上创建一个具有加密凭据的文件。
从文件中获取加密密码
创建加密的密码文件后,我们可以读取该文件并在脚本中使用保存的凭据,例如:
#function to get credentials from a Saved file
Function Get-SavedCredential([string]$UserName,[string]$KeyPath)
{
If(Test-Path "$($KeyPath)$($Username).cred") {
$SecureString = Get-Content "$($KeyPath)$($Username).cred" | ConvertTo-SecureString
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $SecureString
}
Else {
Throw "Unable to locate a credential for $($Username)"
}
Return $Credential
}
#Get encrypted password from the file
$Cred = Get-SavedCredential -UserName "[email protected]" -KeyPath "C:\Scripts"
#Connect to Azure AD from saved credentials
Connect-AzureAD -Credential $Cred
好吧,以下是我们如何使用此方法连接到 SharePoint Online Management Shell:
#Get encrypted password from the file
$Cred = Get-SavedCredential -UserName "[email protected]" -KeyPath "C:\Scripts"
#Connect to SharePoint Online PowerShell
Connect-SPOService -URL "https://Crescent-admin.sharepoint.com" -Credential $Cred
#Get all Site Collections
Get-SPOSite
同样,要使用 CSOM PowerShell 连接到 SharePoint Online,请使用:
Import-Module Microsoft.Online.SharePoint.PowerShell
#Get encrypted password from the file
$Cred = Get-SavedCredential -UserName "[email protected]" -KeyPath "C:\Scripts"
#Parameter
$SiteUrl = "https://Crescent.sharepoint.com/sites/marketing"
#Set up the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the Web Object
$Web = $Ctx.web
$Ctx.Load($Web)
$Ctx.ExecuteQuery()
#Get the Title of the Web
Write-host $Web.Title
我在 Windows 任务计划程序中计划的自动化 PowerShell 脚本中使用了它。例如,PowerShell 将自定义用户配置文件属性从 Azure AD 导入到 SharePoint Online 用户配置文件存储。
使用 Clixml 方法导出-导入凭证
这是安全导出和导入凭据的替代方法。
#Function to Export Credentials to secure File
Function Export-Credential
{
param
(
[Parameter(Mandatory=$true)]
$Path,
[System.Management.Automation.Credential()]
[Parameter(Mandatory=$true)]
$Credential
)
$CredentialObject = $Credential | Select-Object *
$CredentialObject.Password = $CredentialObject.Password | ConvertFrom-SecureString
$CredentialObject | Export-Clixml $Path
}
#Import Credentials from Saved File
Function Import-Credential
{
param
(
[Parameter(Mandatory=$true)]
$Path
)
$CredentialObject = Import-Clixml $path
$CredentialObject.password = $CredentialObject.Password | ConvertTo-SecureString
New-Object system.Management.Automation.PSCredential($CredentialObject.username, $CredentialObject.password)
}
#Export Credential to a File
Export-Credential -Path "C:\Temp\CredFile.Secure" -Credential (Get-Credential)
#Get Credentials from File
$Cred = Import-Credential -Path "C:\Temp\CredFile.Secure"
#Connect to Azure AD
Connect-AzureAD -Credential $Cred
#Connect to SharePoint Online Management Shell
Connect-SPOService -Url "https://Crescet-admin.sharepoint.com" -Credential $Cred
#Connect to PnP Online
Connect-PnPOnline -Url "https://Crescent-admin.sharepoint.com" -Credentials $Cred
总之,使用 PowerShell 加密密码文件是存储密码等敏感信息的安全方法。加密过程使用安全算法将明文转换为密文,使其不可读。此外,通过使用PowerShell脚本,可以自动加密和解密密码文件,使其成为管理敏感信息的便捷高效的解决方案。然而,最大的缺点之一是,如果密码文件落入坏人之手,其中存储的信息将无法保护它免受未经授权的访问。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag