当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 PowerShell 脚本中使用加密密码文件?

作者:精品下载站 日期:2024-12-14 15:43:39 浏览:15 分类:玩电脑

如何在 PowerShell 脚本中使用加密密码文件?


要求:在 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脚本,可以自动加密和解密密码文件,使其成为管理敏感信息的便捷高效的解决方案。然而,最大的缺点之一是,如果密码文件落入坏人之手,其中存储的信息将无法保护它免受未经授权的访问。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯