stefan007
Goto Top

PowerShell Script um spezielle Rechner herunterzufahren

Hi Leute,

wie man per CMD Rechner runterfährt ist mir bekannt. Mir geht es jetzt aber um den "Luxus" in solch einem Script.

Es soll wie folgt aufgebaut sein:


Doppelklick auf die .cmd -> Abfrage: "Welcher PC soll heruntergefahren werden?" -> da soll dann die Möglichkeit gegeben sein den PC einzutragen und DANACH soll die Abfrage kommen, wann der PC heruntergefahren werden soll 0 = sofort oder ansonsten die normale Zeitangabe in Sekunden -> Ausführung!


Kann mir da jemand helfen?

Content-Key: 313544

Url: https://administrator.de/contentid/313544

Printed on: July 22, 2024 at 11:07 o'clock

Member: clSchak
Solution clSchak Aug 25, 2016 updated at 10:07:40 (UTC)
Goto Top
Hi

in etwa so:
@echo off
set /p rechner=Welcher Rechner:
set /p zeit=Wann:
shutdown /m \\%rechner% /s /t %zeit%

so in etwa sollte das passen face-smile
Mitglied: 129813
Solution 129813 Aug 25, 2016 updated at 10:09:14 (UTC)
Goto Top
PS
cls
$pc = Read-Host "Welcher PC soll heruntergefahren werden?"  
$time = Read-Host "Wann soll der Rechner heruntergefahren werden?"  
if ($time -eq ""){$time = 0}  
& shutdown /M "\\$pc" /s /t $time /f  
Regards
Member: clSchak
clSchak Aug 25, 2016 at 10:12:06 (UTC)
Goto Top
das "/f" kann ggf. aber zu Problemen führen wenn der Benutzer gerade noch was speichern kann ist das hilfreicher face-smile
Mitglied: 129813
129813 Aug 25, 2016 updated at 10:16:29 (UTC)
Goto Top
Zitat von @clSchak:
das "/f" kann ggf. aber zu Problemen führen wenn der Benutzer gerade noch was speichern kann ist das hilfreicher face-smile
But if no one is sitting in front of it or you want to be sure that the system is off after work face-wink.

But i'm sure he knows the command ... so he can decide itself whats right for him.
Member: Stefan007
Stefan007 Aug 25, 2016 at 10:30:11 (UTC)
Goto Top
hi highload,

what does the cls stand for? When i'm trying to start it as powershell file nothing happens
Mitglied: 129813
129813 Aug 25, 2016 updated at 10:36:51 (UTC)
Goto Top
Zitat von @Stefan007:
what does the cls stand for?
Really ???? face-big-smile clear screen.
When i'm trying to start it as powershell file nothing happens
You are new in powershell right face-wink. That's the reason!
Member: Stefan007
Stefan007 Aug 25, 2016 at 10:43:51 (UTC)
Goto Top
Zitat von @129813:

Zitat von @Stefan007:
what does the cls stand for?
Really ???? face-big-smile clear screen.
When i'm trying to start it as powershell file nothing happens
You are new in powershell right face-wink. That's the reason!


yes! i wanna learn it. Anyway: if i start the script i cannot enter any information about the PC's and the time.
Mitglied: 129813
129813 Aug 25, 2016 updated at 10:56:28 (UTC)
Goto Top
It works 100%. I suppose you did not set the Executionpolicy in an administrative command prompt?!

http://www.admin-source.de/BlogDeu/401/powershell-executionpolicy-richt ...
Member: Stefan007
Stefan007 Aug 25, 2016 at 11:00:37 (UTC)
Goto Top
i did!

the ending must be .ps1 right?
Mitglied: 129813
129813 Aug 25, 2016 at 11:05:38 (UTC)
Goto Top
Zitat von @Stefan007:
the ending must be .ps1 right?
Yes.
Mitglied: 129813
129813 Aug 25, 2016 updated at 11:07:13 (UTC)
Goto Top
You should first read
https://blog.udemy.com/powershell-tutorial/
before you post and ask such damn easy things in an administrator board face-sad
Mitglied: 129813
129813 Aug 25, 2016 updated at 12:07:44 (UTC)
Goto Top
Zitat von @Stefan007:
Filename: spezifischen-PC-ausschalten.ps1

Run as Admin
This command "Run As Admin" is not available for ps1 without registry modification! You can start it un-elevated without problems in a ps console or the ISE!

Windows closes .... nothing happens!!!!!
OMG, open Powershell ISE and paste it there then you see any potential messages. This script is so easy, and doesn't contain any magic and it works, tested!

but ok ...."damn easy things" ... alright.
They are really easy, if you would first read the fundamentals (link) how to work with powershell, otherwise you should go to
http://www.gutefrage.net/
and not to an administrator board. We can't explain the scripting basics to everyone, man.
Member: Stefan007
Stefan007 Aug 25, 2016 at 12:15:32 (UTC)
Goto Top
ok!!
Member: Stefan007
Stefan007 Aug 25, 2016 at 16:57:22 (UTC)
Goto Top
Zitat von @129813:

but ok ...."damn easy things" ... alright.
They are really easy, if you would first read the fundamentals (link) how to work with powershell, otherwise you should go to
http://www.gutefrage.net/
and not to an administrator board. We can't explain the scripting basics to everyone, man.


-> it is good that you answer so much questions but NO ONE says that you MUST explain the basics to everyone.

I tested your script, it doesnt shut down the Machine. I set up the GPO to allow Remote-Shutdown but anyway....

i write the PC-Name, the time in seconds, the window closes after a few seconds and that's all.
Mitglied: 129813
129813 Aug 25, 2016 updated at 17:59:29 (UTC)
Goto Top
Are the machines in a domain environment ?
If yes the account you use to start the script needs enough local permissions on the machines, also you need to make sure that file and print services or Port 135 is open in the firewall on the clients.

Here it works without problems (domain).

If your management station is not in the domain. You need to set a registry entry to allow remote shutdown (LocalAccountTokenFilterPolicy) because of UAC.
Member: Stefan007
Stefan007 Aug 25, 2016 at 18:05:30 (UTC)
Goto Top
Hi,

the machines are in a domain environment. I startet the script as Domain-Admin onmy Server and tried to shut down the client. Firewall is disabled (Windows Firewall).
Mitglied: 129813
129813 Aug 25, 2016 updated at 22:07:33 (UTC)
Goto Top
You would help us a lot if you post the errormessage instead of saying "its not working" the whole time, thanks. I would not have posted it if it doesn't work.

And please post what you have copied then we can see if there is an error in copying, perhaps wrong encoding in your browser, on the board or something else.
Member: Stefan007
Stefan007 Aug 26, 2016 at 16:04:29 (UTC)
Goto Top
Zitat von @129813:

You would help us a lot if you post the errormessage instead of saying "its not working" the whole time, thanks. I would not have posted it if it doesn't work.

And please post what you have copied then we can see if there is an error in copying, perhaps wrong encoding in your browser, on the board or something else.


Welcher PC soll heruntergefahren werden?: Haupt-PC
Wann soll der Rechner heruntergefahren werden?: 1
shutdown.exe : System.Management.Automation.Internal.Host.InternalHost: Der eingegebene Computername ist ungltig oder ferngesteuertes Herunterfahren wird auf dem
Zielcomputer nicht untersttzt. šberprfen Sie den Namen und wiederholen Sie den Vorgang oder wenden Sie sich an den Systemadministrator.(53)
In C:\Users\Administrator\Desktop\spez-shutdown.ps1:5 Zeichen:1

back-to-top& shutdown /M "\\$host" /s /t $time /f

back-to-top~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : NotSpecified: (System.Manageme...inistrator.(53):String) , RemoteException
+ FullyQualifiedErrorId : NativeCommandError


PS C:\Windows\system32>


So to make it clear: I added a GPO which supports the Shutdown with Remote. I "forced" this GPO and made it available for "Everyone".

Do you have any idea what i could do?
Mitglied: 129813
129813 Aug 26, 2016 updated at 17:14:26 (UTC)
Goto Top
& shutdown /M "\\$host" /s /t $time /f
Where did you get the variable $host from ??? In my script this is the variable named $pc. Copy the code above correctly then it will run. No wonder when he doesn't find the hostname if the variable is not defined face-smile
Member: Stefan007
Stefan007 Aug 26, 2016 at 17:24:34 (UTC)
Goto Top
oh wtf..... face-smile

ok it works... i'm sorry!