Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PSNativeCommandUseErrorActionPreference example in about_Preference_Variables to use scriptblock scoping #10689

Closed
2 tasks done
rkeithhill opened this issue Nov 30, 2023 · 0 comments · Fixed by #10694
Closed
2 tasks done
Labels
issue-doc-idea Issue - request for new content

Comments

@rkeithhill
Copy link
Contributor

Prerequisites

  • Existing Issue: Search the existing issues for this repository. If there is an issue that fits your needs do not file a new one. Subscribe, react, or comment on that issue instead.
  • Descriptive Title: Write the title for this issue as a short synopsis. If possible, provide context. For example, "Document new Get-Foo cmdlet" instead of "New cmdlet."

PowerShell Version

7.3

Summary

The script example in the help topic for $PSNativeCommandUseErrorActionPreference could be simplified to use a scriptblock to reset the variable's value when the scope exits.

Related to PowerShell issue: PowerShell/PowerShell#20034

Details

The following example is given in the current docs:

$definedPreference = $PSNativeCommandUseErrorActionPreference
$PSNativeCommandUseErrorActionPreference = $false
robocopy.exe D:\reports\operational "\\reporting\ops" CY2022Q4.md
$robocopyExitCode = $LASTEXITCODE
if ($robocopyExitCode -gt 8) {
    throw "robocopy failed with exit code $robocopyExitCode"
}
$PSNativeCommandUseErrorActionPreference = $definedPreference

This works but I think the use of a scriptblock would clean this up a bit:

& {
    # Disable $PSNativeCommandUseErrorActionPreference for this scriptblock / call to robocopy
    $PSNativeCommandUseErrorActionPreference = $false
    robocopy.exe D:\reports\operational "\\reporting\ops" CY2022Q4.md
    if ($LASTEXITCODE -gt 8) {
        throw "robocopy failed with exit code $LASTEXITCODE"
    }
}

This will automatically reset the value of $PSNativeCommandUseErrorActionPreference after the scriptblock exits.

Proposed Content Type

About Topic

Proposed Title

N/A

Related Articles

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.3
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.3#psnativecommanduseerroractionpreference

@rkeithhill rkeithhill added issue-doc-idea Issue - request for new content needs-triage Waiting - Needs triage labels Nov 30, 2023
sdwheeler added a commit that referenced this issue Nov 30, 2023
* Update example to use a scriptblock

* Update TOC
@sdwheeler sdwheeler removed the needs-triage Waiting - Needs triage label Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue-doc-idea Issue - request for new content
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants