110 lines
3.3 KiB
PowerShell
110 lines
3.3 KiB
PowerShell
<# ConsoleTest.ps1 (c) infoSpectrum Inc. 2011-2012 #>
|
|
|
|
# Examples of Console commands
|
|
# Run "PShellExec ConsoleTest.ps1 /s" to see console interactions and output
|
|
|
|
$testWrites = `
|
|
{
|
|
Write-Verbose "More Console Output Examples:"
|
|
|
|
Write-Error "Error 1"
|
|
Write-Error "Error 2"
|
|
|
|
Write-Warning "Warning 2"
|
|
|
|
Write-Debug "Debug test 2"
|
|
|
|
Write-Output "Output 1"
|
|
"Output 2"
|
|
}
|
|
|
|
$ErrorActionPreference = "Continue"
|
|
$verbosePreference = "Continue"
|
|
$WarningPreference = "Continue"
|
|
$DebugPreference = "Continue"
|
|
Write-Host $verbosePreference
|
|
|
|
Write-Warning ("Running from "+$pwd)
|
|
|
|
Write-Verbose "Checking for any script arguments..."
|
|
if ($($args.count) -lt 1)
|
|
{Write-Verbose "No arguments"}
|
|
else
|
|
{
|
|
# Script arguments are enclosed in quotes and comma separated:
|
|
# PShellExec ConsoleTest.ps1 /a:"?one,two,?3rdArg"
|
|
for ( $j=0; $j -lt $($args.count); $j++)
|
|
{
|
|
Write-Verbose ("Argument"+($j+1).ToString()+"="+$args[$j])
|
|
}
|
|
}
|
|
|
|
Write-Debug "Debug test 1"
|
|
|
|
$ins = Read-Host "Input a line:" # reads input with a default value
|
|
$ins
|
|
|
|
Write-Verbose "Enter a Password?"
|
|
$ins = Read-Host -AsSecureString # reads in a secure string password
|
|
if ($ins -and $ins -ne "")
|
|
{
|
|
$ins = (New-Object System.Management.Automation.PSCredential('dummy',$ins)).GetNetworkCredential().Password
|
|
Write-Host $ins # outputs password in plain text
|
|
}
|
|
$ins = Read-Host -Prompt 'Age 21 or older?:Yes' # prompts for input with a default value
|
|
$ins
|
|
Write-Verbose "Results for Console Output:"
|
|
$testThis = Start-Job -Name "ConsoleTest" -ScriptBlock $testWrites
|
|
Wait-Job -Job $testThis -TimeOut -1
|
|
|
|
foreach ($jobResult in $testThis.ChildJobs)
|
|
{
|
|
$jobResult.Output | Out-Host | Write-Host
|
|
$jobResult.Verbose | ForEach-Object -Process {$_.Message | Out-Host | Write-Host}
|
|
$jobResult.Warning | ForEach-Object -Process {$_.Message | Out-Host | Write-Host}
|
|
$jobResult.Debug | ForEach-Object -Process {$_.Message | Out-Host | Write-Host}
|
|
$jobResult.Error | ForEach-Object -Process {$_ | Out-Host | Write-Host}
|
|
}
|
|
|
|
|
|
$dOpt = new-object System.Management.Automation.Host.ChoiceDescription 'Demo', "Example of Get-Credential"
|
|
$sOpt = new-object System.Management.Automation.Host.ChoiceDescription 'Skip', "Skip Get-Credential"
|
|
$choices = [System.Management.Automation.Host.ChoiceDescription[]] ($dOpt,$sOpt)
|
|
$sel=0
|
|
$sel = $host.ui.PromptForChoice("Get-Credential Demo",'Your choice:',$choices,$sel)
|
|
if ($sel -eq 0)
|
|
{
|
|
Write-Verbose "Example of Get-Credential"
|
|
try
|
|
{ $logon = Get-Credential -Verbose
|
|
"Logon user: {0}" -f $logon.Username
|
|
if (test-path variable:\PSExecOut)
|
|
{$variable:PSExecOut="Logon user: "+ $logon.Username}
|
|
}
|
|
catch
|
|
{
|
|
$err = $_.Exception.ToString()
|
|
Write-Warning $err
|
|
}
|
|
}
|
|
|
|
Write-Verbose "PShellExec Info:"
|
|
Get-Process PShellExec
|
|
|
|
<#
|
|
NOTES
|
|
|
|
$myInvocation use within PShellExec:
|
|
|
|
The following return empty or null (when outside a function):
|
|
|
|
$myInvocation.ScriptName, $myInvocation.InvocationName,
|
|
$myInvocation.MyCommand.Name, $myInvocation.MyCommand.Path
|
|
|
|
$PSExecName is a variable provided to PShellExec users and contains a string with the full script path and filename.
|
|
#>
|
|
|
|
if (test-path variable:\PSExecName)
|
|
{echo "`$PSExecName: $($PSExecName)"}
|
|
|
|
Read-Host "Press [Enter] to exit ConsoleTest...:Goodbye" |