mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-12-04 22:01:26 +01:00
86 lines
2.5 KiB
Markdown
86 lines
2.5 KiB
Markdown
The *check-credentials.ps1* Script
|
|
===========================
|
|
|
|
This PowerShell script asks for credentials and checks them against saved ones ("$HOME\my.credentials" by default).
|
|
|
|
Parameters
|
|
----------
|
|
```powershell
|
|
/home/markus/Repos/PowerShell/scripts/check-credentials.ps1 [[-TargetFile] <String>] [<CommonParameters>]
|
|
|
|
-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.
|
|
✅ Your credentials are correct.
|
|
|
|
```
|
|
|
|
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.
|
|
✅ Your credentials are correct.
|
|
.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." }
|
|
|
|
"✅ Your credentials are correct."
|
|
exit 0 # success
|
|
} catch {
|
|
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
|
exit 1
|
|
}
|
|
```
|
|
|
|
*(generated by convert-ps2md.ps1 as of 11/20/2024 11:51:51)*
|