จะใช้ไฟล์รหัสผ่านที่เข้ารหัสในสคริปต์ PowerShell ได้อย่างไร
ความต้องการ: ใช้ไฟล์รหัสผ่านที่เข้ารหัสในสคริปต์ PowerShell
จะใช้ไฟล์รหัสผ่านที่เข้ารหัสเพื่ออ่าน/เขียนข้อมูลประจำตัวใน PowerShell ได้อย่างไร
โมดูล PowerShell เช่น PnP PowerShell เสนอกลไกในการใช้ที่เก็บข้อมูลรับรอง Windows เพื่อบันทึกและดึงชื่อผู้ใช้และรหัสผ่านเพื่อใช้ในสคริปต์ อย่างไรก็ตาม สำหรับโมดูล PowerShell อื่นๆ เช่น SharePoint Online Management Shell, AzureAD, CSOM. ฯลฯ เราไม่มีวิธีโดยตรงในการระงับการพร้อมท์รหัสผ่าน นอกเหนือจากการจัดเก็บรหัสผ่านเป็นข้อความธรรมดาภายในสคริปต์ ในสถานการณ์เช่น เราจำเป็นต้องกำหนดเวลาสคริปต์ในตัวกำหนดเวลางานของ Windows สำหรับการเรียกใช้สคริปต์แบบอัตโนมัติโดยที่ผู้ใช้ไม่ต้องดำเนินการใด ๆ เราสามารถใช้วิธีนี้:
นี่คือวิธีที่เราสามารถจัดเก็บและอ่านรหัสผ่านที่เข้ารหัสจากไฟล์ในสคริปต์ PowerShell
- ขั้นตอนที่ 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 "salaudeen@crescent.com" -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 "salaudeen@crescent.com" -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 "salaudeen@crescent.com" -KeyPath "C:\Scripts"
#Connect to SharePoint Online PowerShell
Connect-SPOService -URL "https://Crescent-admin.sharepoint.com" -Credential $Cred
#Get all Site Collections
Get-SPOSite
ในทำนองเดียวกัน หากต้องการเชื่อมต่อกับ SharePoint Online โดยใช้ CSOM PowerShell ให้ใช้:
Import-Module Microsoft.Online.SharePoint.PowerShell
#Get encrypted password from the file
$Cred = Get-SavedCredential -UserName "salaudeen@crescent.com" -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
ฉันใช้มันในสคริปต์ PowerShell อัตโนมัติที่กำหนดเวลาไว้ในตัวกำหนดเวลางาน Windows เช่น 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 ทำให้เป็นโซลูชันที่สะดวกและมีประสิทธิภาพในการจัดการข้อมูลที่ละเอียดอ่อน อย่างไรก็ตาม ข้อเสียที่ใหญ่ที่สุดประการหนึ่งคือ หากไฟล์รหัสผ่านตกไปอยู่ในมือของผู้ไม่ประสงค์ดี ข้อมูลที่จัดเก็บไว้ในไฟล์จะไม่สามารถป้องกันการเข้าถึงโดยไม่ได้รับอนุญาตได้
