Powershell -whatif question

I am trying to force logoff remote computers via a powershell script.  I would like to use the -whatif to make sure it is doing what I want but don't know hoe to implement it.
My powershell contains:
foreach ($_ in get-content servers.txt) {(gwmi win32_operatingsystem -ComputerName $_).Win32Shutdown(4)}
How  can it use the -whatif condition with this stafement?

Thanks for any help.

Art W.
ajwellmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
You can't use -WhatIf with WMI directly, so you have to implement it yourself. But that can get quite complex, if done correctly.
And please don't use $_ as a foreach variable - it is a predefined var standing for "the current object" in a loop.

How do you want the command to be used? In a script without defining functions (your own "cmdlets")? If so, we would have to allow for a commandline parameter for the script, for example:
$whatif = $args[0] -eq '-whatif'
Get-Content Servers.txt | % {
  $OS = gwmi Win32_OperatingSystem -Computername $_
  if ($whatif) {
    write-host "WhatIf: Shutting down $_"
  } else {
    $OS.Win32Shutdown(4)
  } 
}

Open in new window

You would call the script with e.g.
 .\remoteshutdown.ps1 -whatif
0
David Johnson, CD, MVPOwnerCommented:
from powershell get-help stop-computer -examples
$s = get-content servers.txt
$c = get-credential domain01\admin01  ## needs a domain Admin credential
stop-computer -computername $s -force -throttlelimit 10 -credential $c -WhatIf

Open in new window

1
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you want to know how to do it correctly, which allows inheritance to other cmdlets:
[CmdletBinding(SupportsShouldProcess=true)]
Get-Content Servers.txt | % {
  $OS = gwmi Win32_OperatingSystem -Computername $_
  if ($PSCmdlet.ShouldProcess([String] 'Win32_OperatingSystem', [String] "Shutting down $_")) {
    $OS.Win32Shutdown(4)
  } 
}

Open in new window

The call would be the same.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ajwellmanAuthor Commented:
This worked great.  Since I wanted to force a logoff rather than a reboot, I couldn't find a cmdlet to do that.
Thank you all who responded.  It is a great help.
Art W
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.