# Script to collect MDM logs for Windows devices

Here is a custom script that helps IT administrators collect MDM logs for Windows devices. The logs generated at the device consist of agent logs, installer logs, updater logs, and MDM diagnostic reports to facilitate troubleshooting and device monitoring. Administrators can view these logs collected at the device to diagnose device management or enrollment issues for the Windows endpoints enrolled in Hexnode UEM. To execute these commands on the target devices, use Hexnode’s [Execute Custom Script](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action.

 Disclaimer:The Sample Scripts provided below are adapted from third-party Open-Source sites.

 

 Collect MDM logs for Windows devices in Hexnode
------------------------------------------------

Script to fetch Hexnode UEM logs

try{ #Create Folder for Collecting Logs if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection')){ $logs = New-Item 'C:\\Hexnode\\Logs\\LogCollection' -ItemType Directory } #Collect Agent Logs if((Test-Path -Path 'C:\\Hexnode\\Hexnode Agent\\Logs\\hexnodeagent.log' -PathType Leaf)){ if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection\\AgentLogs')){ $agent = New-Item 'C:\\Hexnode\\Logs\\LogCollection\\AgentLogs' -ItemType Directory } Copy-Item 'C:\\Hexnode\\Hexnode Agent\\Logs\\\*' -Destination 'C:\\Hexnode\\Logs\\LogCollection\\AgentLogs\\' } #Collect Installer Logs if((Test-Path -Path 'C:\\hexnodeinstaller\\hexnodeinstaller.log' -PathType Leaf)){ if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection\\InstallerLogs')){ $installer = New-Item 'C:\\Hexnode\\Logs\\LogCollection\\InstallerLogs' -ItemType Directory } Copy-Item 'C:\\hexnodeinstaller\\\*' -Destination 'C:\\Hexnode\\Logs\\LogCollection\\InstallerLogs\\' } #Collect Updater Logs if((Test-Path -Path 'C:\\Hexnode\\Hexnode Updater\\Logs\\hexnodeupdater.log' -PathType Leaf)){ if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection\\UpdaterLogs')){ $updater = New-Item 'C:\\Hexnode\\Logs\\LogCollection\\UpdaterLogs' -ItemType Directory } Copy-Item 'C:\\Hexnode\\Hexnode Updater\\Logs\\\*' -Destination 'C:\\Hexnode\\Logs\\LogCollection\\UpdaterLogs\\' } #Collect MDMDiagnostics Reports $mdmDiagnosticReport = MdmDiagnosticsTool.exe -area DeviceProvisioning -cab C:\\Hexnode\\Logs\\LogCollection\\MDMDiagnosticReport\\Report.cab #Create Zip if((Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection.zip' -PathType Leaf)){ Remove-Item -Path 'C:\\Hexnode\\Logs\\LogCollection.zip' } Compress-Archive 'C:\\Hexnode\\Logs\\LogCollection\\\*' -DestinationPath 'C:\\Hexnode\\Logs\\LogCollection.zip' if((Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection.zip' -PathType Leaf)){ Remove-Item -Path 'C:\\Hexnode\\Logs\\LogCollection' -Recurse Write-Host Successfully Collected Logs at "C:\\Hexnode\\Logs\\LogCollection.zip" } } catch{ Write-Host Log Collection Failed : $\_.Exception.Message } 

   1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

  try{

 \#Create Folder for Collecting Logs 

 if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection')){

 $logs = New-Item 'C:\\Hexnode\\Logs\\LogCollection' -ItemType Directory

 }

 \#Collect Agent Logs 

 if((Test-Path -Path 'C:\\Hexnode\\Hexnode Agent\\Logs\\hexnodeagent.log' -PathType Leaf)){

 if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection\\AgentLogs')){

 $agent = New-Item 'C:\\Hexnode\\Logs\\LogCollection\\AgentLogs' -ItemType Directory

 }

 Copy-Item 'C:\\Hexnode\\Hexnode Agent\\Logs\\\*' -Destination 'C:\\Hexnode\\Logs\\LogCollection\\AgentLogs\\' 

 } 

 #Collect Installer Logs 

 if((Test-Path -Path 'C:\\hexnodeinstaller\\hexnodeinstaller.log' -PathType Leaf)){ 

 if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection\\InstallerLogs')){ 

 $installer = New-Item 'C:\\Hexnode\\Logs\\LogCollection\\InstallerLogs' -ItemType Directory 

 } 

 Copy-Item 'C:\\hexnodeinstaller\\\*' -Destination 'C:\\Hexnode\\Logs\\LogCollection\\InstallerLogs\\' 

 } 

 #Collect Updater Logs 

 if((Test-Path -Path 'C:\\Hexnode\\Hexnode Updater\\Logs\\hexnodeupdater.log' -PathType Leaf)){ 

 if(-not (Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection\\UpdaterLogs')){ 

 $updater = New-Item 'C:\\Hexnode\\Logs\\LogCollection\\UpdaterLogs' -ItemType Directory 

 } 

 Copy-Item 'C:\\Hexnode\\Hexnode Updater\\Logs\\\*' -Destination 'C:\\Hexnode\\Logs\\LogCollection\\UpdaterLogs\\' 

 } 

 #Collect MDMDiagnostics Reports 

 $mdmDiagnosticReport = MdmDiagnosticsTool.exe -area DeviceProvisioning -cab C:\\Hexnode\\Logs\\LogCollection\\MDMDiagnosticReport\\Report.cab 

 #Create Zip 

 if((Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection.zip' -PathType Leaf)){ 

 Remove-Item -Path 'C:\\Hexnode\\Logs\\LogCollection.zip' 

 } 

 Compress-Archive 'C:\\Hexnode\\Logs\\LogCollection\\\*' -DestinationPath 'C:\\Hexnode\\Logs\\LogCollection.zip' 

 if((Test-Path -Path 'C:\\Hexnode\\Logs\\LogCollection.zip' -PathType Leaf)){ 

 Remove-Item -Path 'C:\\Hexnode\\Logs\\LogCollection' -Recurse 

 Write-Host Successfully Collected Logs at "C:\\Hexnode\\Logs\\LogCollection.zip"

 }

}

catch{

 Write-Host Log Collection Failed : $\_.Exception.Message

} 

   

 

  

The script creates a folder at `C:\Hexnode\Logs\LogCollection` to save the logs generated. The logs are saved into this folder as a zip file named `LogCollection.zip`. The IT admin can then retrieve this to diagnose issues.

 Notes:- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.