Seite 1 von 2

New-LocalUser Cmdlet unbekannt

Verfasst: 13 Okt 2020, 14:12
von wKovacs
Hallo,

ich lasse mein eigenes Skript im context einen lokalen Admins ablaufen, wie hier im Handbuch beschrieben 12.3. Script for Installations in the Context of a Local Administrator

Innerhalb meines Skriptes möchte ich via PowerShell einen neuen User anlegen, was dann fehlschlägt mit folgender Fehlermeldung im log:

Code: Alles auswählen

New-LocalUser -Name itadmin -Password $itadmin_pw -AccountNeverExpires -PasswordNeverExpires
         if ($?) {Exit(0)}
         else {Exit(1)}
         endif
         -----------------------
         ExitCode 1
                
                 output:
                 --------------
                 New-LocalUser : Die Benennung "New-LocalUser" wurde nicht als Name eines Cmdlet, einer Funktion,
                 einer Skriptdatei oder eines ausführbaren Programms erkannt. Überprüfen Sie die Schreibweise des
                 Namens, oder ob der Pfad korrekt ist (sofern enthalten), und wiederholen Sie den Vorgang.
                 In C:\opsi.org\tmp\_opsiscript_Sv10Xd41.ps1:3 Zeichen:1
                 + New-LocalUser -Name itadmin -Password $itadmin_pw -AccountNeverExpire ...
                 + ~~~~~~~~~~~~~
                     + CategoryInfo          : ObjectNotFound: (New-LocalUser:String) [], CommandNotFoundException
                     + FullyQualifiedErrorId : CommandNotFoundException
Mein Code:

Code: Alles auswählen

	set $exitcode$ = "0"
	DosInAnIcon_setpolicy
	set $list$ = getOutStreamFromSection ('ExecWith_powershell_create_user  powershell.exe')
	set $exitcode$ = getLastExitcode
	if not ($exitcode$ = "0")
		comment "Creation of "+ $local_admin_name$ +" failed"
		isFatalError "Creation of "+ $local_admin_name$ +" failed"
	endif
	
[DosInAnIcon_setpolicy]
powershell.exe set-executionpolicy RemoteSigned
exit %ERRORLEVEL%

[ExecWith_powershell_create_user]
$itadmin_key = get-content "%ScriptPath%\files\itadmin.key"
$itadmin_pw = ConvertTo-SecureString "$local_admin_password_secure_string$" -Key $itadmin_key
New-LocalUser -Name $local_admin_name$ -Password $itadmin_pw -AccountNeverExpires -PasswordNeverExpires
if ($?) {Exit(0)}
else {Exit(1)}
endif
Wenn ich das ganze richtig verstehe, wird doch durch das von mir verwendete Template ein lokale Administrator angelegt, mit diesem eingelogt und damit mein Skript ausgeführt.

Warum steht dann in diesem User-Context New-LocalUser nicht zur Verfügung?

Ich hoffe ich hab nur etwas übersehen, da ich ungern auf die CMD-Kommandos zurück greifen möchte.

Danke

wKovacs

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 13 Okt 2020, 14:41
von wKovacs
Ich habe jetzt einmal nach New-LocalUser eine Pause eingefügt, um im aktuellen UserKontext rumspielen zu können.

Wenn ich die Befehle in einer elevated PowerShell ausführe, wie sie von OPSI generiert werden, werden diese ohne Fehler ausgeführt.

Woher kommt also die Meldung New-LocalUser wäre nicht verfügbar?

Regards

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 13 Okt 2020, 16:58
von wKovacs
Ok, dank ein wenig wühlen im Forum bin ich auf einen ziemlich dunklen Hack gekommen....

Ich habe den PowerShell-SubSection-Call durch powershellCall erstetzt und dafür meine Anweisung zu einer einzigen langen Anweisung zusammengesetzt...

Aus:

Code: Alles auswählen

set $exitcode$ = "0"
   DosInAnIcon_setpolicy
   set $list$ = getOutStreamFromSection ('ExecWith_powershell_create_user  powershell.exe')
   set $exitcode$ = getLastExitcode
   if not ($exitcode$ = "0")
      comment "Creation of "+ $local_admin_name$ +" failed"
      isFatalError "Creation of "+ $local_admin_name$ +" failed"
   endif
   
[DosInAnIcon_setpolicy]
powershell.exe set-executionpolicy RemoteSigned
exit %ERRORLEVEL%

[ExecWith_powershell_create_user]
$itadmin_key = get-content "%ScriptPath%\files\itadmin.key"
$itadmin_pw = ConvertTo-SecureString "$local_admin_password_secure_string$" -Key $itadmin_key
New-LocalUser -Name $local_admin_name$ -Password $itadmin_pw -AccountNeverExpires -PasswordNeverExpires
if ($?) {Exit(0)}
else {Exit(1)}
endif
wurde

Code: Alles auswählen

	set $exitcode$ = "0"
	set $exitcode$ = powershellCall("New-LocalUser -Name "+ $local_admin_name$ +" -Password (ConvertTo-SecureString '"+ $local_admin_password_secure_string$ +"' -Key (get-content '%ScriptPath%\files\itadmin.key')) -AccountNeverExpires -PasswordNeverExpires")	

	if not ( $exitcode$ = "0")
		comment "Creation of "+ $local_admin_name$ +" failed"
		isFatalError "Creation of "+ $local_admin_name$ +" failed"
	endif
Ich finde diese Lösung nicht wirklich gut, sie tut aber was sie soll...

Das New-LocalUser eine Fehlermeldung ala "cmdlet nicht vorhanden" auswirft, erscheint mit dann ein Bug zu sein...

Regards

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 14 Okt 2020, 01:51
von SisterOfMercy
There's always the non-powershell alternative.

Code: Alles auswählen

[Winbatch_add_luser]
"%SystemRoot%\system32\net.exe" user /add luser
"%SystemRoot%\system32\net.exe" user luser password
"%SystemRoot%\system32\net.exe" localgroup administrators /add luser
"%SystemRoot%\system32\wbem\wmic.exe" useraccount WHERE "Name='luser'" set PasswordExpires=false

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 14 Okt 2020, 09:01
von wKovacs
Thanks for your suggestion. I'm aware of this solution but I'm not a fan of it. Because in a winbatch solution the password will be written as plain text in a temporary .cmd-file. How short this temporary might be, I hate plain text passwords ;)

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 14 Okt 2020, 14:49
von SisterOfMercy
wKovacs hat geschrieben:Thanks for your suggestion. I'm aware of this solution but I'm not a fan of it. Because in a winbatch solution the password will be written as plain text in a temporary .cmd-file. How short this temporary might be, I hate plain text passwords ;)
Maybe a normal shellcall with SetConfidential "plainpassword" would work.. No time to test it. :(

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 15 Okt 2020, 07:40
von wKovacs
SetConfidential("Plainpassword") will replace "Plainpassword" with *** Confidential *** in all logs. But the .cmd file has still the "Plainpassword" written as plain text. Because this file is called to be executed to run this command.

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 18 Okt 2020, 01:32
von SisterOfMercy
wKovacs hat geschrieben:SetConfidential("Plainpassword") will replace "Plainpassword" with *** Confidential *** in all logs. But the .cmd file has still the "Plainpassword" written as plain text. Because this file is called to be executed to run this command.
I know, but I was wondering if a normal shellcall would also write a .cmd file, or if that would run it in memory and not write the plaintext password somewhere.

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 18 Okt 2020, 12:14
von wKovacs
Interesting thought!

Just out of curiosity I will explore this ;)

Regards

Re: New-LocalUser Cmdlet unbekannt

Verfasst: 24 Okt 2020, 11:37
von Jan.Schmidt
Hi,

(OT)
dein Sicherheitsdenken in aller Ehren, aber mit powershell.exe set-executionpolicy RemoteSigned verscheuchst du den Teufel mit dem Belzebub
(OT)

Manchmal, aber nur manchmal ....
...nehmen Admin für solche Tricks ne GPO und das da ist genau so ein "Trick" für den man eine GPO nutzt.