Obtenir l'heure de démarrage d'un pc distant en se connectant à sa base de registre.

Soyez le premier à donner votre avis sur cette source.

Snippet vu 5 998 fois - Téléchargée 19 fois

Contenu du snippet

Ce code permet d'obtenir l'heure de démarrage d'un PC distant sur le réseau en mappant un lecteur réseau, connexion à la base de registre et récupération de la valeur de la clé de registre StartTime.
Enfin calcule l'écart avec l'heure système.

Source / Exemple :


Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports System.Security.Permissions
Imports Microsoft.Win32
Public Class _Main

        'Définitions des variables

        Dim PC As String = TextBox1.Text
        Dim StrSTV, YS, Mo1S, Mo2S, D1S, D2S, H1S, H2S, Mi1S, Mi2S, Datreg As String
		Dim YI, YIR2, MOI, MOIR2, DI, DIR2, HI, HIR2, MII, MIR2, TOR As Integer
		Dim DatregD As Date
        Dim STV As Object
		

    'µµµµµµµµµµµµµµµµµµµµµµµµµµ
    '    Menu Bar
    'µµµµµµµµµµµµµµµµµµµµµµµµµµ

    '    File
    '------------

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        If MsgBox("Your Information will get lost if you open a new window!" + vbCrLf + "Are you sure to open a new window?", MsgBoxStyle.Critical + MsgBoxStyle.YesNo, "New") = MsgBoxResult.Yes Then
            Me.Hide()
            Call Me.Show()
        End If
    End Sub
    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        SaveFileDialog1.Filter = ("Text File (*.txt)| *.txt|All File (*.*)| *.*")
        SaveFileDialog1.ShowDialog()
        SaveFileDialog1.AddExtension = True
        SaveFileDialog1.CheckPathExists = True
        SaveFileDialog1.OverwritePrompt = True
        My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, "The " & PC & "was rebooted for the last time on " & TextBox2.Text & "." & Ch(13) & " Last reboot for:" & Chr(13) & YIR2 & " year(s)" & Chr(13) & MOIR2 & " Month(s)" & Chr(13) & DIR2 & " day(s)" & Chr(13) & HIR2 & " hour(s)" & Chr(13) & MIR2 & " minute(s)", False)
		End Sub
    Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
        End
    End Sub

    '    View
    '-------------

    Private Sub BlueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
        If BlueToolStripMenuItem.Checked = True Then
            RedToolStripMenuItem.Checked = False
            GreenToolStripMenuItem.Checked = False
            GrayToolStripMenuItem.Checked = False
            OrangeToolStripMenuItem.Checked = False
            YellowToolStripMenuItem.Checked = False
            Me.BackColor = Color.SteelBlue

        ElseIf BlueToolStripMenuItem.Checked = False Then
            Me.BackColor = Color.Gainsboro
        End If
    End Sub
    Private Sub GreyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GrayToolStripMenuItem.Click
        If GrayToolStripMenuItem.Checked = True Then
            RedToolStripMenuItem.Checked = False
            GreenToolStripMenuItem.Checked = False
            BlueToolStripMenuItem.Checked = False
            OrangeToolStripMenuItem.Checked = False
            YellowToolStripMenuItem.Checked = False
            Me.BackColor = Color.Gray

        ElseIf BlueToolStripMenuItem.Checked = False Then
            Me.BackColor = Color.Gainsboro
        End If
    End Sub
    Private Sub GreenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GreenToolStripMenuItem.Click
        If GreenToolStripMenuItem.Checked = True Then
            BlueToolStripMenuItem.Checked = False
            OrangeToolStripMenuItem.Checked = False
            GrayToolStripMenuItem.Checked = False
            RedToolStripMenuItem.Checked = False
            YellowToolStripMenuItem.Checked = False
            Me.BackColor = Color.PaleGreen
        ElseIf GreenToolStripMenuItem.Checked = False Then
            Me.BackColor = Color.Gainsboro
        End If
    End Sub
    Private Sub OrangeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrangeToolStripMenuItem.Click
        If OrangeToolStripMenuItem.Checked = True Then
            BlueToolStripMenuItem.Checked = False
            GreenToolStripMenuItem.Checked = False
            GrayToolStripMenuItem.Checked = False
            RedToolStripMenuItem.Checked = False
            YellowToolStripMenuItem.Checked = False
            Me.BackColor = Color.DarkOrange
        ElseIf OrangeToolStripMenuItem.Checked = False Then
            Me.BackColor = Color.Gainsboro
        End If
    End Sub
    Private Sub RedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedToolStripMenuItem.Click
        If RedToolStripMenuItem.Checked = True Then
            BlueToolStripMenuItem.Checked = False
            GreenToolStripMenuItem.Checked = False
            GrayToolStripMenuItem.Checked = False
            OrangeToolStripMenuItem.Checked = False
            YellowToolStripMenuItem.Checked = False
            Me.BackColor = Color.Red
        ElseIf RedToolStripMenuItem.Checked = False Then
            Me.BackColor = Color.Gainsboro
        End If

    End Sub
    Private Sub YellowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YellowToolStripMenuItem.Click
        If YellowToolStripMenuItem.Checked = True Then
            BlueToolStripMenuItem.Checked = False
            GreenToolStripMenuItem.Checked = False
            GrayToolStripMenuItem.Checked = False
            OrangeToolStripMenuItem.Checked = False
            RedToolStripMenuItem.Checked = False
            Me.BackColor = Color.Orange
        ElseIf YellowToolStripMenuItem.Checked = False Then
            Me.BackColor = Color.Gainsboro
        End If

    End Sub

    '    Help
    '-------------

    Private Sub AboutPCStartTimeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutPCStartTimeToolStripMenuItem.Click
        Me.Hide()
        About.Show()
    End Sub

    'µµµµµµµµµµµµµµµµµµµµµµµµµµ
    '    Button
    'µµµµµµµµµµµµµµµµµµµµµµµµµµ

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        TextBox1.Enabled = False
        TextBox2.Text = ""
        ProgressBar1.ForeColor = Color.Pink
        ProgressBar1.Value = 10

        'Envoi d'une requête de ping du pc souhaité.
        If My.Computer.Network.Ping(PC) = True Then
            ProgressBar1.Increment(15)
            'Si ping Ok alors faire progresser la barre de progression"

            Try

                Shell("net use m: /delete")
                Shell("net use m: \\" & PC & "\c")
                'Déconnexion du lecteur réseau M: et mappage d'un lecteur pour le PC en admin.

                ProgressBar1.Increment(25)
                Timer1.Enabled = True
                Timer1.Interval = 3000
            Catch ex As Exception
                MsgBox("Network Drive could not be connected", vbOKOnly, PC)
                TextBox1.Enabled = True
                ProgressBar1.Value = 0
                'Si le lecteur n'a pas pu être mappé ou si l'IP réponds pour un autre PC

            End Try

        Else : MsgBox("PC not connected to the network", vbOKOnly, PC)
            TextBox1.Enabled = True
            ProgressBar1.Value = 0
            'Ping NOK ou délai d'attente dépassé alors RAZ.
        End if
    End Sub
    
	
	Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

	'Récupération de la clé de registre à afficher
        Try
            STV = Registry.LocalMachine.OpenRemoteBaseKey(RegistryHive.LocalMachine, PC).OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Prefetcher").GetValue("StartTime")
            StrSTV = CStr(STV)
            TextBox2.Text = StrSTV

            'Récupération des données dans la chaîne de caractères :
            'année, mois, jour, heure, minute

            YS = Microsoft.VisualBasic.Left(StrSTV, 4)
            Mo1S = Microsoft.VisualBasic.Left(StrSTV, 7)
            Mo2S = Microsoft.VisualBasic.Right(Mo1S, 2)
            D1S = Microsoft.VisualBasic.Left(StrSTV, 10)
            D2S = Microsoft.VisualBasic.Right(D1S, 2)
            H1S = Microsoft.VisualBasic.Right(StrSTV, 8)
            H2S = Microsoft.VisualBasic.Left(H1S, 2)
            Mi1S = Microsoft.VisualBasic.Right(StrSTV, 5)
            Mi2S = Microsoft.VisualBasic.Left(Mi1S, 2)

            'Convertion des données de la clé de registre en nombres

            YI = CInt(YS)
            MOI = CInt(Mo2S)
            DI = CInt(D2S)
            HI = CInt(H2S)
            MII = CInt(Mi2S)

            'Formatage des nombres de la clé de registre en format Date

            Datreg = D2S & "." & Mo2S & "." & YS & " " & H2S & ":" & Mi2S
            DatregD = CDate(Datreg)

            'déconnexion du lecteur réseau.
            Shell("net use m: /delete")

            'Fin de la progression de la barre car info récupérée.
            ProgressBar1.Increment(50)
            TextBox1.Enabled = True
            Timer1.Enabled = False

            'Différence de date
            TOR = DateDiff(DateInterval.Minute, DatregD, Now, FirstDayOfWeek.Monday, FirstWeekOfYear.Jan1)

            'Calcul du nombres d'années, mois, jours, heures, minutes en ne prenant que les entiers.
            YIR2 = Math.Truncate(TOR / 525484.8)
            MOIR2 = Math.Truncate((TOR - (YIR2 * 525484.8)) / 43790.4)
            DIR2 = Math.Truncate((TOR - (YIR2 * 525484.8) - (MOIR2 * 43790.4)) / 1440)
            HIR2 = Math.Truncate((TOR - (YIR2 * 525484.8) - (MOIR2 * 43790.4) - (DIR2 * 1440)) / 60)
            MIR2 = Math.Truncate(TOR - (YIR2 * 525484.8) - (MOIR2 * 43790.4) - (DIR2 * 1440) - (HIR2 * 60))

            'Affichage des messages d'information.
            If YIR2 = 0 And MOIR2 = 0 And DIR2 = 0 And HIR2 = 0 Then
                MsgBox("The " & PC & " has not been restartet for :" & Chr(13) & MIR2 & " minute(s)", vbOKOnly, PC)
            ElseIf YIR2 = 0 And MOIR2 = 0 And DIR2 = 0 Then
                MsgBox("The " & PC & " has not been restartet for :" & Chr(13) & HIR2 & " hour(s)" & Chr(13) & MIR2 & " minute(s)", vbOKOnly, PC)
            ElseIf YIR2 = 0 And MOIR2 = 0 Then
                MsgBox("The " & PC & " has not been restartet for :" & Chr(13) & DIR2 & " day(s)" & Chr(13) & HIR2 & " hour(s)" & Chr(13) & MIR2 & " minute(s)", vbOKOnly, PC)
            ElseIf YIR2 = 0 Then
                MsgBox("The " & PC & " has not been restartet for :" & Chr(13) & MOIR2 & " Month(s)" & Chr(13) & DIR2 & " day(s)" & Chr(13) & HIR2 & " hour(s)" & Chr(13) & MIR2 & " minute(s)", vbOKOnly, PC)
            Else
                MsgBox("Please contact immediatly your supervisor", MsgBoxStyle.Critical, "CRITICAL")
                MsgBox("The " & PC & " has not been restartet for :" & Chr(13) & YIR2 & " year(s)" & Chr(13) & MOIR2 & " Month(s)" & Chr(13) & DIR2 & " day(s)" & Chr(13) & HIR2 & " hour(s)" & Chr(13) & MIR2 & " minute(s)", vbOKOnly, PC)
            End If
        Catch ex As Exception
            ProgressBar1.Value = 0
        End Try
    End Sub
End Class

Conclusion :


Voici mon premier code posté sur le site.
Etant débutant, je suis preneur de toutes critiques constructives sur ce code et plus particulièrement sur la partie de calcul de date.

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.