1

so as the title self implies i'm trying to code a powershell script so I can log off a list of computers, from a central server which is connected to all of them.

Luckily, the hard part is over. All computers are completely intertwined, and communicating with my server. I'm trying to come up with a powershell script which will automate the log off process, on a list of stations.

So basically, i'd have a basic txt file which would have a list of the station IPs. Based on the list, i would ideally simply run a powershell script which would send out a shutdown -l -f command, to log the stations off.

I can already communicate with the stations, but logging them off is kind of a pain. I'm currently using this simple command to get the stations off

shutdown -m 10.123.45.67 -l -f

naturally, typing this over and over again for 10 or so stations at any given time is time consuming. I've tried searching aroudn here but its somewhat hard to word x_x

Any suggestions?

  • for reading the file, see http://stackoverflow.com/questions/17917088/how-to-read-a-line-from-a-file-in-powershell – lib Jul 13 '16 at 20:45
  • [What have you tried so far?](http://whathaveyoutried.com) Please [edit] your question to show a [mcve] of the code that you are having problems with, then we can try to help with the specific problem. You should also read [ask]. – Toby Speight Jul 13 '16 at 21:26

3 Answers3

1

try this computers.txt contains ip addres

192.168.1.1

192.168.1.3

$comp =  Get-content c:\computers.txt
(gwmi win32_operatingsystem -ComputerName $comp).Win32Shutdown(4)
DisplayName
  • 1,008
  • 10
  • 24
  • Thanks for the help. Doesn't look like it worked...I receive the following error: Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) At D:\MASSLOGOFF\MASSLOGOFF.ps1:2 char:6 + (gwmi <<<< win32_operatingsystem -ComputerName $comp).Win32Shutdown(4) + CategoryInfo : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessExcept ion + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Com mands.GetWmiObjectCommand – Bryan Soto Jul 18 '16 at 19:13
0

Looks like this ended up working for me:

foreach ($_ in get-content computers.txt) 
{(gwmi win32_operatingsystem -ComputerName $_).Win32Shutdown(4)}
-1

Try this:

$machines = Get-content <filepath>\<filename>.txt    
foreach ($machine in $machines)
        {
            shutdown -m $machine -l -f
            $machines = $machines | Where-Object {$_ -ne "$machine"}

        }
EvanM
  • 155
  • 8