[英]Powershell ForEach-Object output
More of a basic question but I can't seem to get the syntax correct.更多的基本问题,但我似乎无法获得正确的语法。 I am trying to go through a list and then output the Computer Name and BDE status with it.
我正在尝试通过列表 go 然后 output 计算机名称和 BDE 状态与它。
Get-Content 'C:\Users\Test\Test\IT\Lists\LaptopList.txt' | ForEach-Object { write-host "***" | manage-bde -status }
Is there something I need to put where the *'s are so that it correlates with the computer/object and the status?我需要在 * 的位置放置一些东西,以便它与计算机/对象和状态相关联吗?
Thanks!谢谢!
Is there something I need to put where the *'s are so that it correlates with the computer/object and the status?
我需要在 * 的位置放置一些东西,以便它与计算机/对象和状态相关联吗?
Yes!是的! You're looking for
$_
:您正在寻找
$_
:
Get-Content 'C:\Users\Test\Test\IT\Lists\LaptopList.txt' | ForEach-Object {
Write-Host $_
}
As the name indicates, Write-Host
writes output directly to the host application - in the case of powershell.exe
it writes directly to the screen buffer - which means it doesn't output anything to downstream cmdlets in a pipeline statement like the one you've constructed.顾名思义,
Write-Host
将 output直接写入主机应用程序- 在powershell.exe
的情况下,它直接写入屏幕缓冲区 - 这意味着它不会像管道语句中的任何东西一样Z78E6221F6393D1456681DB398中的下游 cmdlets '已经建成。
You'll therefore want two separate statements:因此,您需要两个单独的语句:
Get-Content 'C:\Users\Test\Test\IT\Lists\LaptopList.txt' | ForEach-Object {
Write-Host $_
manage-bde -status
}
Now, manage-bde
is not a PowerShell cmdlet - it's a windows executable, and it doesn't support managing remote computers.现在,
manage-bde
不是PowerShell cmdlet - 它是 windows 可执行文件,它不支持管理远程计算机。
So we need something in PowerShell that can run manage-bde
on the remote machine - my choice would be Invoke-Command
:所以我们需要在 PowerShell 中可以在远程机器上运行
manage-bde
的东西——我的选择是Invoke-Command
:
Get-Content 'C:\Users\Test\Test\IT\Lists\LaptopList.txt' | ForEach-Object {
Write-Host "Remoting into $_ to fetch BitLocker Drive Encryption status
Invoke-Command -ComputerName $_ -ScriptBlock { manage-bde -status }
}
Here, we instruct Invoke-Command
to connect to the computer with whatever name is currently assigned to $_
, and then execute manage-bde -status
on the remote machine and return the resulting output, if any.在这里,我们指示
Invoke-Command
以当前分配给$_
的任何名称连接到计算机,然后在远程计算机上执行manage-bde -status
并返回生成的 output(如果有)。 Assuming that WinRM/PowerShellRemoting is configured on the remote machines, and the user executing this code locally is a domain account with local admin privileges on the remote computers, this will work as-is.假设在远程计算机上配置了 WinRM/PowerShellRemoting,并且在本地执行此代码的用户是在远程计算机上具有本地管理员权限的域帐户,这将按原样工作。
Further reading:进一步阅读:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.