2024-11-08 12:38:20 +01:00
|
|
|
The *check-credentials.ps1* Script
|
|
|
|
===========================
|
2024-01-25 13:31:10 +01:00
|
|
|
|
|
|
|
This PowerShell script asks for credentials and checks them against saved ones ("$HOME\my.credentials" by default).
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
```powershell
|
2024-11-08 12:35:11 +01:00
|
|
|
/home/markus/Repos/PowerShell/scripts/check-credentials.ps1 [[-TargetFile] <String>] [<CommonParameters>]
|
2024-01-25 13:31:10 +01:00
|
|
|
|
|
|
|
-TargetFile <String>
|
|
|
|
Specifies the target file ("$HOME\my.credentials" by default)
|
|
|
|
|
|
|
|
Required? false
|
|
|
|
Position? 1
|
|
|
|
Default value "$HOME\my.credentials"
|
|
|
|
Accept pipeline input? false
|
|
|
|
Accept wildcard characters? false
|
|
|
|
|
|
|
|
[<CommonParameters>]
|
|
|
|
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
|
|
|
|
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
|
|
|
|
```
|
|
|
|
|
|
|
|
Example
|
|
|
|
-------
|
|
|
|
```powershell
|
|
|
|
PS> ./check-credentials.ps1
|
|
|
|
Enter username and password, please.
|
2024-11-08 12:35:11 +01:00
|
|
|
✅ Your credentials are correct.
|
2024-01-25 13:31:10 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
|
|
|
|
Related Links
|
|
|
|
-------------
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
|
|
|
|
Script Content
|
|
|
|
--------------
|
|
|
|
```powershell
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Checks credentials
|
|
|
|
.DESCRIPTION
|
|
|
|
This PowerShell script asks for credentials and checks them against saved ones ("$HOME\my.credentials" by default).
|
|
|
|
.PARAMETER TargetFile
|
|
|
|
Specifies the target file ("$HOME\my.credentials" by default)
|
|
|
|
.EXAMPLE
|
|
|
|
PS> ./check-credentials.ps1
|
|
|
|
Enter username and password, please.
|
2024-11-08 12:35:11 +01:00
|
|
|
✅ Your credentials are correct.
|
2024-01-25 13:31:10 +01:00
|
|
|
.LINK
|
|
|
|
https://github.com/fleschutz/PowerShell
|
|
|
|
.NOTES
|
|
|
|
Author: Markus Fleschutz | License: CC0
|
|
|
|
#>
|
|
|
|
|
|
|
|
param([string]$TargetFile = "$HOME\my.credentials")
|
|
|
|
|
|
|
|
try {
|
|
|
|
Write-Host "Enter username and password, please." -foreground red
|
|
|
|
$credsFromUser = Get-Credential
|
|
|
|
|
|
|
|
$secureString = Get-Content "$TargetFile" | ConvertTo-SecureString
|
|
|
|
$credsFromFile = New-Object System.Management.Automation.PSCredential($credsFromUser.UserName, $secureString)
|
|
|
|
|
|
|
|
if ($credsFromUser.UserName -ne $credsFromFile.UserName) { throw "Sorry, your username is wrong." }
|
|
|
|
|
|
|
|
$pw1 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($credsFromUser.Password))
|
|
|
|
$pw2 = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($credsFromFile.Password))
|
|
|
|
if ($pw1 -cne $pw2) { throw "Sorry, your password is wrong." }
|
|
|
|
|
2024-11-08 12:35:11 +01:00
|
|
|
"✅ Your credentials are correct."
|
2024-01-25 13:31:10 +01:00
|
|
|
exit 0 # success
|
|
|
|
} catch {
|
|
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2024-11-20 11:52:20 +01:00
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:51)*
|