กำลังเปลี่ยนเส้นทางเอาต์พุตเป็น $null ใน PowerShell แต่ต้องแน่ใจว่าตัวแปรยังคงตั้งค่าอยู่

ฉันมีรหัสบางอย่าง:

$foo = someFunction

สิ่งนี้จะแสดงข้อความเตือนซึ่งฉันต้องการเปลี่ยนเส้นทางไปที่ $null:

$foo = someFunction > $null

ปัญหาคือเมื่อฉันทำเช่นนี้ ในขณะที่ระงับข้อความเตือนได้สำเร็จ แต่ก็มีผลข้างเคียงเชิงลบจากการไม่เติม $foo ด้วยผลลัพธ์ของฟังก์ชันด้วย

ฉันจะเปลี่ยนเส้นทางคำเตือนไปที่ $null แต่ยังคงมี $foo อยู่ได้อย่างไร

นอกจากนี้ คุณจะเปลี่ยนเส้นทางทั้งเอาต์พุตมาตรฐานและข้อผิดพลาดมาตรฐานเป็นโมฆะได้อย่างไร (ใน Linux จะเป็น 2>&1)


person ted    schedule 04.05.2011    source แหล่งที่มา
comment
อะไรทำให้เกิดข้อความเตือน? หากคุณเป็นผู้เขียน someFunction คุณอาจเปลี่ยนแปลงได้อย่างเหมาะสม   -  person stej    schedule 04.05.2011
comment
ที่จริงแล้วใน Bourne Shell (Linux) มันคือ 2>/dev/null 1>/dev/null; การเปลี่ยนเส้นทางที่คุณแสดงจะเปลี่ยนเส้นทาง stderr ไปยังตำแหน่งเดียวกับ stdout ซึ่งอาจเป็น /dev/null หรืออาจเป็นไฟล์ปกติ   -  person jpaugh    schedule 13.12.2016


คำตอบ (5)


ฉันต้องการวิธีนี้ในการเปลี่ยนเส้นทางเอาต์พุตมาตรฐาน (PowerShell ดั้งเดิม)...

($foo = someFunction) | out-null

แต่มันก็ใช้ได้ผลเช่นกัน:

($foo = someFunction) > $null

หากต้องการเปลี่ยนเส้นทางข้อผิดพลาดมาตรฐานหลังจากกำหนด $foo ด้วยผลลัพธ์ของ "someFunction" ให้ทำ

($foo = someFunction) 2> $null

นี่เป็นสิ่งเดียวกันกับที่กล่าวไว้ข้างต้นอย่างมีประสิทธิภาพ

หรือหากต้องการเปลี่ยนเส้นทางข้อความแสดงข้อผิดพลาดมาตรฐานจาก "someFunction" แล้วกำหนด $foo ด้วยผลลัพธ์:

$foo = (someFunction 2> $null)

หากต้องการเปลี่ยนเส้นทางทั้งสอง คุณมีทางเลือกดังนี้:

2>&1>$null
2>&1 | out-null
person J Bills    schedule 23.06.2011
comment
วิธีนี้ใช้ได้ผลสำหรับฉันหลังจากที่ฉันใส่คำสั่งใน {วงเล็บปีกกา} แทน (วงเล็บ) ฉันอาจใช้ PS เวอร์ชันใหม่กว่า - person ManEatingCheese; 08.09.2019
comment
และหากเรากำลังสร้างงานพื้นหลัง เราจำเป็นต้องสร้างงานเอง: { myCommandWithAnyOutput & } | Out-Null - person arberg; 28.09.2019

สิ่งนี้ควรจะได้ผล

 $foo = someFunction 2>$null
person ravikanth    schedule 04.05.2011

หากเป็นข้อผิดพลาดที่คุณต้องการซ่อน คุณสามารถทำได้เช่นนี้

$ErrorActionPreference = "SilentlyContinue"; #This will hide errors
$someObject.SomeFunction();
$ErrorActionPreference = "Continue"; #Turning errors back on
person Christian Flem    schedule 09.10.2013

ควรเขียนข้อความเตือนโดยใช้ Write-Warning cmdlet ซึ่งอนุญาตให้ระงับข้อความเตือนด้วยพารามิเตอร์ -WarningAction หรือตัวแปรอัตโนมัติ $WarningPreference ฟังก์ชันจำเป็นต้องใช้ CmdletBinding เพื่อใช้ฟีเจอร์นี้

function WarningTest {
    [CmdletBinding()]
    param($n)

    Write-Warning "This is a warning message for: $n."
    "Parameter n = $n"
}

$a = WarningTest 'test one' -WarningAction SilentlyContinue

# To turn off warnings for multiple commads,
# use the WarningPreference variable
$WarningPreference = 'SilentlyContinue'
$b = WarningTest 'test two'
$c = WarningTest 'test three'
# Turn messages back on.
$WarningPreference = 'Continue'
$c = WarningTest 'test four'

หากต้องการทำให้สั้นลงที่พรอมต์คำสั่ง คุณสามารถใช้ -wa 0:

PS> WarningTest 'parameter alias test' -wa 0

Write-Error, Write-Verbose และ Write-Debug มีฟังก์ชันการทำงานที่คล้ายคลึงกันสำหรับข้อความประเภทที่สอดคล้องกัน

person Rynant    schedule 15.02.2012

ใช้ฟังก์ชัน:

function run_command ($command)
{
    invoke-expression "$command *>$null"
    return $_
}

if (!(run_command "dir *.txt"))
{
    if (!(run_command "dir *.doc"))
    {
        run_command "dir *.*"
    }
}

หรือถ้าคุณชอบซับใน:

function run_command ($command) { invoke-expression "$command  "|out-null; return $_ }

if (!(run_command "dir *.txt")) { if (!(run_command "dir *.doc")) { run_command "dir *.*" } }
person Elroy Flynn    schedule 18.11.2019