【发布时间】:2016-03-13 17:20:59
【问题描述】:
我正在尝试使用 powershell 编写一个脚本,该脚本在满足打开 3 个或更少文件的特定情况的计算机集合上调用 net.exe 的删除。显然,我在这方面还很陌生,因为我遇到了奇怪的错误。
使用Microsoft's blog 的示例,我用net session 制作了下面的函数。
Function Get-ActiveNetSessions
{
# converts the output of the net session cmd into a PSobject
$output = net session | Select-String -Pattern \\
$output | foreach {
$parts = $_ -split "\s+", 4
New-Object -Type PSObject -Property @{
Computer = $parts[0].ToString();
Username = $parts[1];
Opens = $parts[2];
IdleTime = $parts[3];
}
}
}
这确实产生了一个我可以应用逻辑的可行对象。
我可以使用
$computerList = Get-ActiveNetSessions | Where-Object {$_.Opens -clt 3} | Select-Object {$_.Computer} 将所有少于三个打开的计算机也拉到一个变量中。
失败的是下面的循环
ForEach($computer in $computerList)
{
net session $computer /delete
}
出现错误
net : The syntax of this command is:
At line:5 char:5
net session $computer /delete
CategoryInfo :NotSpecified (The syntax of this command is::String) [], RemoteException
FullyQualifiedErrorId : NativeCommandError
NET SESSION
[\\computername] [/DELETE] [/LIST]
尝试在执行前调用$computer = $computer.ToString() 来运行它,以便它看到一个字符串导致脚本挂起而不丢弃会话,迫使我关闭并重新打开 ISE。
我应该怎么做才能让这个循环正常工作?任何帮助表示赞赏。
【问题讨论】:
标签: asp.net .net powershell