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

Ausgedruckt am: 19.03.2024 um 11:03 Uhr

Mitglied: clSchak
Lösung clSchak 25.08.2016 aktualisiert um 12:07:40 Uhr
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
Lösung 129813 25.08.2016 aktualisiert um 12:09:14 Uhr
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
Mitglied: clSchak
clSchak 25.08.2016 um 12:12:06 Uhr
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 25.08.2016 aktualisiert um 12:16:29 Uhr
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.
Mitglied: Stefan007
Stefan007 25.08.2016 um 12:30:11 Uhr
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 25.08.2016 aktualisiert um 12:36:51 Uhr
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!
Mitglied: Stefan007
Stefan007 25.08.2016 um 12:43:51 Uhr
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 25.08.2016 aktualisiert um 12:56:28 Uhr
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 ...
Mitglied: Stefan007
Stefan007 25.08.2016 um 13:00:37 Uhr
Goto Top
i did!

the ending must be .ps1 right?
Mitglied: 129813
129813 25.08.2016 um 13:05:38 Uhr
Goto Top
Zitat von @Stefan007:
the ending must be .ps1 right?
Yes.
Mitglied: 129813
129813 25.08.2016 aktualisiert um 13:07:13 Uhr
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 25.08.2016 aktualisiert um 14:07:44 Uhr
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.
Mitglied: Stefan007
Stefan007 25.08.2016 um 14:15:32 Uhr
Goto Top
ok!!
Mitglied: Stefan007
Stefan007 25.08.2016 um 18:57:22 Uhr
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 25.08.2016 aktualisiert um 19:59:29 Uhr
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.
Mitglied: Stefan007
Stefan007 25.08.2016 um 20:05:30 Uhr
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 25.08.2016, aktualisiert am 26.08.2016 um 00:07:33 Uhr
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.
Mitglied: Stefan007
Stefan007 26.08.2016 um 18:04:29 Uhr
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 26.08.2016 aktualisiert um 19:14:26 Uhr
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
Mitglied: Stefan007
Stefan007 26.08.2016 um 19:24:34 Uhr
Goto Top
oh wtf..... face-smile

ok it works... i'm sorry!