-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCredentials.ps1
More file actions
50 lines (42 loc) · 1.7 KB
/
TestCredentials.ps1
File metadata and controls
50 lines (42 loc) · 1.7 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#Requires -Version 5.1
#region Import Encrypted Credentials
function Import-EncryptedCredential {
param([string]$CredentialName, [string]$CredentialPath = "C:\Config\Credentials")
$FilePath = Join-Path $CredentialPath "$CredentialName.xml"
if (-not (Test-Path $FilePath)) { throw "Credential file not found: $FilePath" }
$Config = Import-Clixml -Path $FilePath
$Creds = @{}
foreach ($Key in $Config.Credentials.Keys) {
if ($Config.Credentials[$Key] -is [string] -and $Config.Credentials[$Key] -match '^[0-9a-f]+$') {
$Creds[$Key] = $Config.Credentials[$Key] | ConvertTo-SecureString
} else { $Creds[$Key] = $Config.Credentials[$Key] }
}
return $Creds
}
function ConvertFrom-SecureStringToPlainText {
param([SecureString]$SecureString)
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString)
try { return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) }
finally { [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR) }
}
#endregion
# === YOUR SCRIPT STARTS HERE ===
try {
# Load credentials
$Creds = Import-EncryptedCredential -CredentialName "TestAppReg"
# Connect to Microsoft Graph
$ClientSecretCredential = New-Object PSCredential($Creds.AppId, $Creds.ClientSecret)
Connect-MgGraph -TenantId $Creds.TenantId -ClientSecretCredential $ClientSecretCredential -NoWelcome
Write-Host "✓ Connected to Microsoft Graph successfully" -ForegroundColor Green
# Your code here...
}
catch {
Write-Host "✗ Error: $($_.Exception.Message)" -ForegroundColor Red
exit 1
}
finally {
# Cleanup
if (Get-MgContext) {
Disconnect-MgGraph | Out-Null
}
}