Lancer des commandes bat

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
cage
Messages : 604
Inscription : ven. 16/oct./2015 18:22
Localisation : France
Contact :

Re: Lancer des commandes bat

Message par cage »

Le code suivant doit être lancé avec les droits administrateur pour fonctionner.

Code : Tout sélectionner

sc config dhcp start= disabled
sc stop dhcp
sc config dnscache start= disabled
sc stop dnscache
sc config netman start= disabled
sc stop netman
■ Win10 Pro 64-bit (Intel Celeron CPU N2920 @ 1.86GHz, 4,0GB RAM, Intel HD Graphics) & PB 6.12 LTS
■ Vivre et laisser vivre.
■ PureBasic pour le fun
■ Gérard sur le forum Anglais
■ Mes sites: http://pbcage.free.fr - http://yh.toolbox.free.fr
Avatar de l’utilisateur
ChaudEf
Messages : 179
Inscription : dim. 27/déc./2015 17:02
Localisation : Strasbourg

Re: Lancer des commandes bat

Message par ChaudEf »

merci beaucoup por l'aide

j'ai lance ce code (merci a mesa!!)

Code : Tout sélectionner

;trouvé ici http://purebasic.info/phpBB3ex/viewtopic.php?f=1&t=4449&p=84131&hilit=%23PB_Program_Write#p84131

Procedure.s OemToChar(String.s) 
  OemToChar_(@String, @String) 
  ProcedureReturn String 
EndProcedure

prog = RunProgram("cmd.exe", "", "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Write|#PB_Program_Hide) 
;Delay(200)
If IsProgram(prog) 
  
	WriteProgramStringN(prog, "sc config dhcp start= disabled")
	WriteProgramStringN(prog, "net stop dhcp /y")
	WriteProgramStringN(prog, "sc config dnscache start= disabled")
	WriteProgramStringN(prog, "net stop dnscache /y")
	WriteProgramStringN(prog, "sc config netman start= disabled")



  While ProgramRunning(prog)
    If AvailableProgramOutput(prog)
      x=0 
      Answer$=OemToChar(ReadProgramString(prog))
      If Answer$
        Debug Answer$
      EndIf
    EndIf
    Delay(50) ; commentez cette ligne pour aller plus vite
  Wend
EndIf
et rien ne se passe.
Windows 10 x64 -- Purebasic 5.70 LTS x86
Avatar de l’utilisateur
cage
Messages : 604
Inscription : ven. 16/oct./2015 18:22
Localisation : France
Contact :

Re: Lancer des commandes bat

Message par cage »

Bonjour ChaudEf,

Tu ne dois pas faire le test correctement.
J'ai pris le code et je l'ai copié dans un fichier cmd-bat.pb
Je l'ai ouvert dans PB
Dans le menu Compiler->Compiler Options...
J'ai coché la case: Request Administrator mode for Windows Vista and above
J'ai compile le code dans cmd-bat.exe
Après avoir lancé ce fichier avec élévation des droits, les trois services sont arrêtés et désactivés.

J'ai vérifié l'état initial pour les remettre en état avec la commande:

Code : Tout sélectionner

sc query type= service state= all > query.txt
Fichier bat de contrôle:

Code : Tout sélectionner

sc query type= service state= all > query.txt
:: Client DHCP (Automatique)
sc query dhcp
:: Client DNS (Automatique)
sc query dnscache
:: Conneions réseau (Manuel)
sc query netman
pause
Gérard
■ Win10 Pro 64-bit (Intel Celeron CPU N2920 @ 1.86GHz, 4,0GB RAM, Intel HD Graphics) & PB 6.12 LTS
■ Vivre et laisser vivre.
■ PureBasic pour le fun
■ Gérard sur le forum Anglais
■ Mes sites: http://pbcage.free.fr - http://yh.toolbox.free.fr
Avatar de l’utilisateur
ChaudEf
Messages : 179
Inscription : dim. 27/déc./2015 17:02
Localisation : Strasbourg

Re: Lancer des commandes bat

Message par ChaudEf »

ca doit etre alors mon ordi qui fait des problemes, c'est extremement bizzarre, j'ai fait exactement comme ca et rien ne se passe!!!
Windows 10 x64 -- Purebasic 5.70 LTS x86
Avatar de l’utilisateur
cage
Messages : 604
Inscription : ven. 16/oct./2015 18:22
Localisation : France
Contact :

Re: Lancer des commandes bat

Message par cage »

Peux-tu nous dire quel OS Windows tu utilises.
■ Win10 Pro 64-bit (Intel Celeron CPU N2920 @ 1.86GHz, 4,0GB RAM, Intel HD Graphics) & PB 6.12 LTS
■ Vivre et laisser vivre.
■ PureBasic pour le fun
■ Gérard sur le forum Anglais
■ Mes sites: http://pbcage.free.fr - http://yh.toolbox.free.fr
Avatar de l’utilisateur
ChaudEf
Messages : 179
Inscription : dim. 27/déc./2015 17:02
Localisation : Strasbourg

Re: Lancer des commandes bat

Message par ChaudEf »

Win 7 32 bit
Windows 10 x64 -- Purebasic 5.70 LTS x86
Mesa
Messages : 1126
Inscription : mer. 14/sept./2011 16:59

Re: Lancer des commandes bat

Message par Mesa »

Tu peux utiliser PowerShell qui est présent par défaut de XP SP3 à Windows 10 (et même sur linux et macos si l'utilisateur l'a installé).

Voici ce que j'ai dans mes cartons, à toi de faire des essais et tiens nous au courant. :wink:

Code : Tout sélectionner

; Liste des fonctions PS: http://ss64.com/ps/
; voir https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters/
; http://ss64.com/ps/syntax.html

Procedure.s GetPath_PowerShell()
  Protected path$ = GetEnvironmentVariable("PATH"), ; List of all paths
            PowerShell$ = "PowerShell not found!",  ; Initialize with failure, success will change it
            thispath$ = #Empty$,                    ; Temporary var to hold path for comparison
            i.i                                     ; Loop counter set to number of paths available
  
  For i = 1 To CountString(path$, ";") + 1
    thispath$ = StringField(path$, i, ";")
    If FindString(thispath$, "powershell", 1, #PB_String_NoCase)
      PowerShell$ = thispath$
      Break
    EndIf
  Next
  ProcedureReturn PowerShell$
EndProcedure

Debug GetPath_PowerShell() ; chemin de powershell. Attention: même si le chemin indique v1.0, la version de PS peut être 2 ou plus !



Procedure.s SendPS(Command.s)
  
  OutCmd$ = ""
  KeepOutCmd$ = ""
  CmdPromptRun = RunProgram("powershell.exe", Command, "", #PB_Program_Open|#PB_Program_Read|#PB_Program_Hide);
  If CmdPromptRun
    While ProgramRunning(CmdPromptRun)
      OutCmd$=ReadProgramString(CmdPromptRun)
      KeepOutCmd$ = KeepOutCmd$ + RTrim(OutCmd$) + #CR$
    Wend
    CloseProgram(CmdPromptRun)
  EndIf
  ;ShowMemoryViewer(@KeepOutCmd$,1000)
  ProcedureReturn KeepOutCmd$
  
EndProcedure

;Debug SendPS("Get-Host") ; info sur la console PS
Debug SendPS("get-host | Select version") ; Version de PowerShell, la version 2 ou plus est recquise (XP SP3 minimum)

;Debug SendPS("Get-WmiObject Win32_NetworkAdapterConfiguration -computername . | select DNSServerSearchOrder")
;Debug SendPS("Get-Service LAN* | % { $_.Name; SC.EXE SDSHOW $_.Name }")
;Debug SendPS("Get-Service LANMANSERVER | FL")
;Debug SendPS("get-service | where-object {$_.Status -eq 'Running'}");Display only services that are currently running:

Debug SendPS("get-service");Get all the services on the computer.:

;Attention, certains fonctions ont besoin d'être élevées (administrator), Voir ici http://ss64.com/ps/syntax-elevate.html 
; il faut alors ajouter -verb RunAs

;     Restart-Service:            Stop And then restart a service
;     Resume-Service:            Resume a suspended service
;        Set-Service:            Change the start mode/properties of a service
;      Start-Service     sasv:   Start a stopped service
;       Stop-Service     spsv:   Stop a running service
;    Suspend-Service:            Suspend a running service

Debug SendPS("SC.EXE  Query"); voir http://ss64.com/nt/sc.html et surtout https://blogs.technet.microsoft.com/josebda/2012/03/03/using-windows-powershell-to-run-old-command-line-tools-and-their-weirdest-parameters/
M.
Avatar de l’utilisateur
cage
Messages : 604
Inscription : ven. 16/oct./2015 18:22
Localisation : France
Contact :

Re: Lancer des commandes bat

Message par cage »

Si ton PC dispose d'un antivirus, il se peut que se soit lui qui bloque les changements d’état des services.

J'utilise 360 Total Security et lorsque je lance l'exécutable cmd-bat.exe, j'ai un avertissement m'indiquant qu'un programme essaye de modifier le service concerné et je dois confirmer chaque changement d'état.

Ton antivirus fait peut-être la même chose mais en mode silencieux, ce qui fait que tu ne vois pas d'avertissement.
■ Win10 Pro 64-bit (Intel Celeron CPU N2920 @ 1.86GHz, 4,0GB RAM, Intel HD Graphics) & PB 6.12 LTS
■ Vivre et laisser vivre.
■ PureBasic pour le fun
■ Gérard sur le forum Anglais
■ Mes sites: http://pbcage.free.fr - http://yh.toolbox.free.fr
Répondre