12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- using Microsoft . Azure . Commands . Profile . Properties ;
15
+ using Microsoft . Azure . Commands . Common . Authentication ;
16
+ using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
16
17
using System ;
17
18
using System . Collections . Generic ;
18
- using System . Collections . ObjectModel ;
19
19
using System . IO ;
20
20
using System . Linq ;
21
21
using System . Management . Automation ;
@@ -28,33 +28,47 @@ public class AliasHelper
28
28
private const string STARTALIASIMPORTMARKER = "#Begin Azure PowerShell alias import" ;
29
29
private const string ENDALIASIMPORTMARKER = "#End Azure PowerShell alias import" ;
30
30
31
+ private IDataStore _dataStore ;
32
+
33
+ public AliasHelper ( IDataStore dataStore )
34
+ {
35
+ _dataStore = dataStore ;
36
+ FileUtilities . DataStore = dataStore ;
37
+ }
38
+
39
+ public AliasHelper ( ) : this ( AzureSession . Instance . DataStore )
40
+ {
41
+ }
42
+
31
43
public static string GetProfilePath ( string Scope , SessionState sessionState )
32
44
{
33
- object userprofile = "" ;
45
+ var userprofile = "" ;
34
46
if ( Scope != null && Scope . Equals ( "CurrentUser" ) )
35
47
{
36
- userprofile = Path . Combine ( sessionState . PSVariable . GetValue ( "env:USERPROFILE" ) . ToString ( ) , "Documents" , "PowerShell" , "profile.ps1" ) ;
48
+ var editionType = sessionState . PSVariable . GetValue ( "PSEdition" ) as string ;
49
+ var psFolder = string . Equals ( editionType , "Desktop" , StringComparison . OrdinalIgnoreCase ) ? "WindowsPowerShell" : "PowerShell" ;
50
+ userprofile = Path . Combine ( sessionState . PSVariable . GetValue ( "env:USERPROFILE" ) . ToString ( ) , "Documents" , psFolder , "profile.ps1" ) ;
37
51
}
38
52
39
53
else if ( Scope != null && Scope . Equals ( "LocalMachine" ) )
40
54
{
41
55
userprofile = Path . Combine ( sessionState . PSVariable . GetValue ( "PSHOME" ) . ToString ( ) , "profile.ps1" ) ;
42
56
}
43
57
44
- return userprofile . ToString ( ) ;
58
+ return userprofile ;
45
59
}
46
60
47
- public static void RemoveAliasesInProfile ( string userprofile , string [ ] Module , Dictionary < string , object > mapping )
61
+ public void RemoveAliasesInProfile ( string userprofile , string [ ] Module , Dictionary < string , object > mapping )
48
62
{
49
63
ParseFile ( userprofile , Module , out string filecontent , out List < string > modulesToKeep , add : false ) ;
50
64
51
65
// Add script to enable aliases to profile if there are any modules to keep
52
66
CreateFileEntry ( modulesToKeep , filecontent , userprofile , mapping ) ;
53
67
}
54
68
55
- public static void AddAliasesToProfile ( string userprofile , string [ ] Module , Dictionary < string , object > mapping )
69
+ public void AddAliasesToProfile ( string userprofile , string [ ] Module , Dictionary < string , object > mapping )
56
70
{
57
- CreateProfileIfNotExisting ( userprofile ) ;
71
+ CreateProfileIfNotExists ( userprofile ) ;
58
72
59
73
ParseFile ( userprofile , Module , out string filecontent , out List < string > modulesToKeep , add : true ) ;
60
74
@@ -68,15 +82,17 @@ public static void AddAliasesToProfile(string userprofile, string[] Module, Dict
68
82
}
69
83
}
70
84
71
- public static void ParseFile ( string userprofile , string [ ] Module , out string filecontent , out List < string > modulesToKeep , bool add )
85
+ public void ParseFile ( string userprofile , string [ ] Module , out string filecontent , out List < string > modulesToKeep , bool add )
72
86
{
73
87
filecontent = "" ;
74
88
modulesToKeep = new List < String > ( ) ;
75
89
if ( add )
76
90
{
77
91
modulesToKeep = Module ? . ToList ( ) ;
78
92
}
79
- string originalText = File . ReadAllText ( userprofile . ToString ( ) ) ;
93
+
94
+
95
+ string originalText = _dataStore . ReadFileAsText ( userprofile . ToString ( ) ) ;
80
96
if ( originalText . Contains ( STARTALIASIMPORTMARKER ) )
81
97
{
82
98
// Add back profile code unrelated to Azure PowerShell aliases
@@ -119,12 +135,12 @@ public static void ParseFile(string userprofile, string[] Module, out string fil
119
135
}
120
136
}
121
137
122
- public static void CreateFileEntry ( List < string > modulesToKeep , string filecontent , string userprofile , Dictionary < string , object > mapping )
138
+ public void CreateFileEntry ( List < string > modulesToKeep , string filecontent , string userprofile , Dictionary < string , object > mapping )
123
139
{
124
140
if ( modulesToKeep . Count > 0 )
125
141
{
126
142
filecontent += STARTALIASIMPORTMARKER + Environment . NewLine + "Import-Module Az.Profile -ErrorAction SilentlyContinue -ErrorVariable importError" +
127
- Environment . NewLine + "if (` $importerror.Count -eq 0) { " + Environment . NewLine ;
143
+ Environment . NewLine + "if ($importerror.Count -eq 0) { " + Environment . NewLine ;
128
144
129
145
var validModules = new List < string > ( ) ;
130
146
foreach ( var name in modulesToKeep )
@@ -139,43 +155,34 @@ public static void CreateFileEntry(List<string> modulesToKeep, string fileconten
139
155
filecontent += "}" + Environment . NewLine + ENDALIASIMPORTMARKER ;
140
156
}
141
157
142
- using ( System . Management . Automation . PowerShell PowerShellInstance = System . Management . Automation . PowerShell . Create ( ) )
158
+ ExecuteFileActionWithFriendlyError ( ( ) =>
143
159
{
144
- PowerShellInstance . AddScript ( "Set-Content -Path '" + userprofile + "' -Value \" " + filecontent + "\" " ) ;
145
- Collection < PSObject > PSOutput = PowerShellInstance . Invoke ( ) ;
160
+ FileUtilities . EnsureDirectoryExists ( Path . GetDirectoryName ( userprofile ) ) ;
161
+ _dataStore . WriteFile ( userprofile , filecontent ) ;
162
+ } ) ;
163
+ }
146
164
147
- // Check for error thrown when LocalMachine profile is accessed from non-admin mode.
148
- if ( PowerShellInstance . Streams . Error . Count > 0 )
165
+ public void CreateProfileIfNotExists ( string userprofile )
166
+ {
167
+ ExecuteFileActionWithFriendlyError ( ( ) =>
168
+ {
169
+ FileUtilities . EnsureDirectoryExists ( Path . GetDirectoryName ( userprofile ) ) ;
170
+ if ( ! _dataStore . FileExists ( userprofile ) )
149
171
{
150
- foreach ( var error in PowerShellInstance . Streams . Error )
151
- {
152
- if ( error . ToString ( ) . Contains ( "Access to the path" ) && error . ToString ( ) . Contains ( "is denied." ) )
153
- {
154
- throw new Exception ( Resources . AliasImportFailure ) ;
155
- }
156
- }
172
+ _dataStore . WriteFile ( userprofile , Environment . NewLine ) ;
157
173
}
158
- }
174
+ } ) ;
159
175
}
160
176
161
- public static void CreateProfileIfNotExisting ( string userprofile )
177
+ private static void ExecuteFileActionWithFriendlyError ( Action fileAction )
162
178
{
163
- using ( System . Management . Automation . PowerShell PowerShellInstance = System . Management . Automation . PowerShell . Create ( ) )
179
+ try
164
180
{
165
- PowerShellInstance . AddScript ( "if (!(Test-Path '" + userprofile + "')) { New-Item '" + userprofile + "' -ItemType file -Force }" ) ;
166
- Collection < PSObject > PSOutput = PowerShellInstance . Invoke ( ) ;
167
-
168
- // Check for error thrown when LocalMachine profile is accessed from non-admin mode.
169
- if ( PowerShellInstance . Streams . Error . Count > 0 )
170
- {
171
- foreach ( var error in PowerShellInstance . Streams . Error )
172
- {
173
- if ( error . ToString ( ) . Contains ( "Access to the path" ) && error . ToString ( ) . Contains ( "is denied." ) )
174
- {
175
- throw new Exception ( Resources . AliasImportFailure ) ;
176
- }
177
- }
178
- }
181
+ fileAction ( ) ;
182
+ }
183
+ catch ( UnauthorizedAccessException accessException ) when ( accessException ? . Message != null && accessException . Message . ToLower ( ) . Contains ( "denied" ) )
184
+ {
185
+ throw new UnauthorizedAccessException ( Properties . Resources . AliasImportFailure , accessException ) ;
179
186
}
180
187
}
181
188
}
0 commit comments