Framework/Listeners/UserReports/WriteEnvironmentFile.ps1

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
108
Set-StrictMode -Version Latest 
class WriteEnvironmentFile: FileOutputBase
{
    hidden static [WriteEnvironmentFile] $Instance = $null;
    static [WriteEnvironmentFile] GetInstance()
    {
        if ($null -eq  [WriteEnvironmentFile]::Instance)
        {
            [WriteEnvironmentFile]::Instance = [WriteEnvironmentFile]::new();
        }

        return [WriteEnvironmentFile]::Instance
    }

    [void] RegisterEvents()
    {        
        $this.UnregisterEvents();

        $this.RegisterEvent([AzSKRootEvent]::GenerateRunIdentifier, {
            $currentInstance = [WriteEnvironmentFile]::GetInstance();
            try 
            {
                $currentInstance.SetRunIdentifier([AzSKRootEventArgument] ($Event.SourceArgs | Select-Object -First 1));                         
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });

        $this.RegisterEvent([SVTEvent]::CommandStarted, {
            $currentInstance = [WriteEnvironmentFile]::GetInstance();
            try 
            {
                $currentInstance.CommandStartedAction($Event.SourceArgs.SubscriptionContext);
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });

        $this.RegisterEvent([AzSKRootEvent]::CommandStarted, {
            $currentInstance = [WriteEnvironmentFile]::GetInstance();
            try 
            {
                $currentInstance.CommandStartedAction($Event.SourceArgs.SubscriptionContext);
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });
    }
    
    hidden [void] AddOutputLog([string] $message, [bool] $includeTimeStamp)   
    {
        if([string]::IsNullOrEmpty($message) -or [string]::IsNullOrEmpty($this.FilePath))
        {
            return;
        }
             
        if($includeTimeStamp)
        {
            $message = (Get-Date -format "MM\/dd\/yyyy HH:mm:ss") + "-" + $message
        }

        Add-Content -Value $message -Path $this.FilePath
    } 

    hidden [void] AddOutputLog([string] $message)   
    {
       $this.AddOutputLog($message, $false);  
    } 

    [void] CommandStartedAction([SubscriptionContext] $context)
    {
        $this.SetFilePath($context, [FileOutputBase]::ETCFolderPath, "EnvironmentDetails.LOG");      
        $this.AddOutputLog([Constants]::DoubleDashLine);

        $currentVersion = $this.GetCurrentModuleVersion();
        $moduleName = $this.GetModuleName();
        $this.AddOutputLog("Environment log");
        $this.AddOutputLog("$moduleName Version: $currentVersion");
        $this.AddOutputLog([Constants]::DoubleDashLine);

        $this.AddOutputLog("Method Name: $($this.InvocationContext.MyCommand.Name) `r`nInput Parameters: ");
        $this.AddOutputLog([Helpers]::ConvertObjectToString($this.InvocationContext.BoundParameters, $false));
        $this.AddOutputLog([Constants]::DoubleDashLine);

        $loadedModules = (Get-Module | Select-Object -Property Name, Path, Description, Version);
        $this.AddOutputLog("Loaded PowerShell modules");
        $this.AddOutputLog([Helpers]::ConvertObjectToString($loadedModules, $false));
        $this.AddOutputLog([Constants]::DoubleDashLine);

        $rmContext = Get-AzureRmContext;

        $this.AddOutputLog("Logged-in user context");
        $this.AddOutputLog([Helpers]::ConvertObjectToString(($rmContext.Account | Select-Object -Property Id, Type, ExtendedProperties), $false));
        $this.AddOutputLog([Constants]::DoubleDashLine);

        $this.AddOutputLog("AzureRM context");
        $this.AddOutputLog([Helpers]::ConvertObjectToString(($rmContext | Select-Object -Property Environment, Subscription, Tenant), $false));
        $this.AddOutputLog([Constants]::DoubleDashLine);
    }

}