Powershell-执行命令而不在 powershell 终端中显示它们?

Powershell-执行命令而不在 powershell 终端中显示它们?

我有一个用于生成文件夹内信息的脚本。

非常基本的例子:

PS C:\Users\> write-host "teste"
teste
PS C:\Users\>

我想以静默模式打开 powershell,粘贴命令write-host "teste"并仅显示结果:

teste

此外,我不想PS C:\Users\>

我知道我可以创建一个 ps1 脚本并运行,但我需要通过 PowerShell 手动导航。

恢复:我不想看到我在 PowerShell 命令行中输入的内容,只想看到结果。

如果可能的话,执行后打开并关闭,类似于:

PS C:\Users\> <enable silent mode>
    (I paste the commands here, but is not showed in the powershell command line)
    teste
             <disable silent mode>
PS C:\Users\> now I can work again....

关于第一条评论,原因如下。粘贴命令后我看到的情况:

PS C:\Users> set-location C:\Users\xxxxxxxxxxxxxx
PS C:\Users\> $ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
PS C:\Users\> $CUR_DIR = "spool ""$pwd\"
PS C:\Users\> $DIR = ($CUR_DIR| Select-String -Pattern  "(?<!;)XX.*").Matches.Value.replace('\','')
PS C:\Users\> write-host $CUR_DIR$DIR".log"""
spool "C:\Users\xxxx.log"
PS C:\Users\> set-location xxxxxxxxxxxx
PS C:\Users> foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural))
>> {
>> write-host "@""$row"""
>> write-host "show erros"
>> }
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.sql"
show erros
PS C:\Users\xxxxxxxxxxxxxx> cd ..
PS C:\Users\> write-host "spool off"
spool off
PS C:\Users\>
PS C:\Users\> foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural)|Select-String 'registro')
>> {
>> write-host "@""$row"""
>> }
@"C:\Users\xxxxxxxxxxxxxx\xxxxx.txt"
PS C:\Users\>

我倒要看看:

PS C:\Users> set-location C:\Users\xxxxxxxxxxxxxx
PS C:\Users\> <enable silent mode>
spool "C:\Users\xxxx.log"
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.txt"
show erros
@"C:\Users\xxxxxxxxxx.sql"
show erros
spool off
@"C:\Users\xxxxxxxxxxxxxx\xxxxx.txt"
           <disable silent mode>
PS C:\Users\> now I can work again....

我已将一些信息替换为 xxx,以不显示客户内容。

答案1

使用脚本来保存所有命令,并且它们不会写入控制台:

# script.ps1 contents:
set-location C:\Users\xxxxxxxxxxxxxx
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
$CUR_DIR = "spool ""$pwd\"
$DIR = ($CUR_DIR| Select-String -Pattern  "(?<!;)XX.*").Matches.Value.replace('\','')
write-host $CUR_DIR$DIR".log"""

set-location xxxxxxxxxxxx
foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural)) {
  write-host "@""$row"""
  write-host "show erros"
}

cd ..
write-host "spool off"

foreach ($row in $(Get-ChildItem| % { $_.FullName }| Sort-Object $ToNatural)|Select-String 'registro') {
  write-host "@""$row"""
}

然后只需根据需要从控制台运行脚本:

PS C:\Users\> c:\path\to\script.ps1

# output looks like your "want to see" block

如果您的脚本执行策略不允许这样做,或者您不想保存文件,您可以改为使用shift+将所有命令写入终端enter以一次性运行它们。这样可以将输出放在一起以便于复制/粘贴:

PS C:\Users> Write-Host "hello world"
>> Write-host Foo
>> Write-Host Bar

hello world
Foo
Bar

相关内容