我正在尝试编写一个简短的脚本来显示所有计算机的PowerShell版本和远程协议。我试图让输出看起来像
PC: PC1 ---------- PS:4.0------------Remoting Protocol: 2.2
PC: PC2 ---------- PS:3.0------------Remoting Protocol: 2.2
PC名称存储在文件中。到目前为止,我已经尝试了下面的代码,但无法弄清楚如何以线性方式显示远程协议或PC名称
cls
$1 = (gc .\computers)
invoke-command -computername $1 {$psversiontable.PSVersion.major}
# this only gives me the PS version
有任何建议/帮助吗?
答案 0 :(得分:1)
使用format operator(-f
):
Get-Content .\computers | ForEach-Object {
Invoke-Command -Computer $_ -ScriptBlock {
$fmt = 'PC: {0} ---------- PS:{1}.{2} ------------Remoting Protocol: {3}.{4}'
$ps = $PSVersionTable.PSVersion
$remoting = $PSVersionTable.PSRemotingProtocolVersion
$fmt -f $env:COMPUTERNAME, $ps.Major, $ps.Minor, $remoting.Major, $remoting.Minor
}
}
答案 1 :(得分:0)
试试这个:
$Computers = Get-Content -Path C:\Computers.txt
foreach ($c in $Computers)
{
$winRMVersion = (test-wsman).ProductVersion.Split(':')[-1].Trim()
$psVersion = $PSVersionTable.PSVersion.Major
"$c ---------- PS:$psVersion ------------Remoting Protocol: $winRMVersion"
}