Get-CimInstance重复输出多次

时间:2017-09-07 14:45:52

标签: powershell cim

我正在尝试获取上次启动时间报告,并且我的脚本不再重复相同的输出8次,我确定它是语法错误。这就是我所拥有的:

$ServerName = (Get-Content -Path 
D:\Users\Admin.sa\Desktop\Computerlist.txt)

##### Script Starts Here ######  

foreach ($Server in $ServerName) {Get-CimInstance -Cn $Servername -ClassName 
win32_operatingsystem | select csname,lastbootuptime}

Format-Table -AutoSize

2 个答案:

答案 0 :(得分:1)

我认为问题出在你的for循环中。尝试:

$ServerName = (Get-Content -Path "D:\Users\Admin.sa\Desktop\Computerlist.txt")

##### Script Starts Here ######  

foreach ($Server in $ServerName) {Get-CimInstance -Cn $Server -ClassName win32_operatingsystem | select csname,lastbootuptime}

Format-Table -AutoSize

答案 1 :(得分:0)

所以你的问题是当你进行for循环时,你使用了错误的变量。请记住,当您使用for循环时,必须使用$Server而不是$ServerName

这样:

$ServerName = (Get-Content -Path 
D:\Users\Admin.sa\Desktop\Computerlist.txt)

##### Script Starts Here ######  

foreach ($Server in $ServerName) {Get-CimInstance -Cn $Server -ClassName 
win32_operatingsystem | select csname,lastbootuptime}

Format-Table -AutoSize
相关问题