gpt4 book ai didi

powershell - 隐藏Powershell输出

转载 作者:行者123 更新时间:2023-12-02 22:09:38 24 4
gpt4 key购买 nike

我有以下脚本:

param([Parameter(Mandatory = $ true)] [string] $ dest)

New-Item -force -path "$dest\1\" -itemtype directory
New-Item -force -path "$dest\2\" -itemtype directory
New-Item -force -path "$dest\3\" -itemtype directory

Copy-Item -path "C:\Development\1\bin\Debug\*" -destination "$dest\1\" -container -recurse -force
Copy-Item -path "C:\Development\2\bin\Debug\*" -destination "$dest\2\" -container -recurse -force
Copy-Item -path "C:\Development\3\bin\Debug\*" -destination "$dest\3\" -container -recurse -force

该脚本采用一个字符串,并将所有文件和文件夹从静态原始路径复制到给定的根字符串,并修改了一些文件夹以简化结构。

它工作正常,但会打印出“New-Item”命令的结果,我想将其隐藏。我查看了SE上的网络问题和其他问题,但没有找到关于我的问题的明确答案。

如果有人想知道-我在开始时使用“New-item”是为了避免PS'-recurse参数中的缺陷,如果目标文件夹不存在,则不能正确复制所有子文件夹。 (即,它们是强制性的)

最佳答案

选项1:通过管道将其空置

New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo

选项2:分配给 $null(比选项1更快)
$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo

选项3:强制转换为 [void](也比选项1更快)
[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo

另请参阅: What's the better (cleaner) way to ignore output in PowerShell?

关于powershell - 隐藏Powershell输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46586382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com