Skip to content

Latest commit

 

History

History
186 lines (157 loc) · 10.9 KB

File metadata and controls

186 lines (157 loc) · 10.9 KB

Saritasa.General

Get-CallerPreference

Synopsis

Fetches "Preference" variable values from the caller's scope.

Syntax

Get-CallerPreference -Cmdlet <Object> -SessionState <SessionState> [<CommonParameters>]

Get-CallerPreference -Cmdlet <Object> -SessionState <SessionState> [-Name <String[]>] [<CommonParameters>]

Parameters

Name Alias Description Required? Pipeline Input Default Value
Cmdlet The $PSCmdlet object from a script module Advanced Function. true false
SessionState The $ExecutionContext.SessionState object from a script module Advanced Function. This is how the Get-CallerPreference function sets variables in its callers' scope, even if that caller is in a different script module. true false
Name Optional array of parameter names to retrieve from the caller's scope. Default is to retrieve all Preference variables as defined in the about\_Preference\_Variables help file \(as of PowerShell 4.0\) This parameter may also specify names of variables that are not in the about\_Preference\_Variables help file, and the function will retrieve and set those as well. false true \(ByValue\)

Inputs

  • String

Outputs

  • None. This function does not produce pipeline output.

Note

Import Preference variables from the caller of a Script Module function https://gallery.technet.microsoft.com/scriptcenter/Inherit-Preference-82343b9d MICROSOFT LIMITED PUBLIC LICENSE

Examples

EXAMPLE 1

Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

Imports the default PowerShell preference variables from the caller into the local scope. EXAMPLE 2

Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState -Name 'ErrorActionPreference','SomeOtherVariable'

Imports only the ErrorActionPreference and SomeOtherVariable variables into the local scope. EXAMPLE 3

'ErrorActionPreference','SomeOtherVariable' | Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

Same as Example 2, but sends variable names to the Name parameter via pipeline input.

Links

Test-IsLocalhost

Synopsis

Returns $true if hostname represents local PC.

Syntax

Test-IsLocalhost [[-ComputerName] <String>] [<CommonParameters>]

Parameters

Name Alias Description Required? Pipeline Input Default Value
ComputerName false false

Use-Object

Synopsis

Dispose an object after using it.

Syntax

Use-Object [-InputObject] <Object> [-ScriptBlock] <ScriptBlock> [<CommonParameters>]

Parameters

Name Alias Description Required? Pipeline Input Default Value
InputObject Object to be disposed. true false
ScriptBlock Script to be executed. true false

Examples

EXAMPLE 1

$text = ""

C:\PS> $fileStream = New-Object System.IO.FileStream("G:\test\1.txt", [System.IO.FileMode]::Open)
C:\PS> Use-Object $fileStream `
{
$streamReader = New-Object System.IO.StreamReader($fileStream)
Use-Object $streamReader `
{
$text = $streamReader.ReadToEnd()
}
}
C:\PS> $text