Skip to content

Commit b00ee5b

Browse files
cfergeaugbraad
authored andcommitted
Issue #391 Fix adding user to group
The previous code had miscellaneous issues when dealing with usernames containing accents. A reboot/relogin is still needed after doing this addition.
1 parent 9cb193a commit b00ee5b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

pkg/crc/preflight/preflight_checks_windows.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package preflight
33
import (
44
//"errors"
55
"fmt"
6+
"os"
67
"strconv"
78
"strings"
89

910
"github.com/code-ready/crc/pkg/crc/logging"
1011
"github.com/code-ready/crc/pkg/crc/oc"
1112

1213
"github.com/code-ready/crc/pkg/crc/errors"
13-
"github.com/code-ready/crc/pkg/os/windows/win32"
1414
"github.com/code-ready/crc/pkg/os/windows/powershell"
1515
)
1616

@@ -115,14 +115,13 @@ func fixUserPartOfHyperVAdmins() (bool, error) {
115115
}
116116
groupName := strings.TrimSpace(strings.Replace(strings.TrimSpace(outGroupName), "BUILTIN\\", "", -1))
117117

118-
outUsername, _, err := powershell.Execute(`Write-Host $env:USERNAME`)
118+
username := os.Getenv("USERNAME")
119+
120+
netCmdArgs := fmt.Sprintf(`([adsi]"WinNT://./%s,group").Add("WinNT://%s,user")`, groupName, username)
121+
_, _, err = powershell.ExecuteAsAdmin(netCmdArgs)
119122
if err != nil {
120123
return false, errors.New("Unable to get user name")
121124
}
122-
username := strings.TrimSpace(outUsername)
123-
124-
netCmdArgs := fmt.Sprintf(`localgroup "%s" "%s" /add`, groupName, username)
125-
win32.ShellExecuteAsAdmin(win32.HWND_DESKTOP, "net", netCmdArgs, "", 0)
126125

127126
return true, nil
128127
}

pkg/os/windows/powershell/powershell_windows.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@ func ExecuteAsAdmin(cmd string) (stdOut string, stdErr string, err error) {
7676
}
7777

7878
// Write a temporary script
79-
psFile.WriteString(scriptContent)
79+
/* Add UTF-8 BOM at the beginning of the script so that Windows
80+
* correctly detects the file encoding
81+
*/
82+
_, err = psFile.Write([]byte{0xef, 0xbb, 0xbf})
83+
if err != nil {
84+
return "", "", err
85+
}
86+
_, err = psFile.WriteString(scriptContent)
87+
if err != nil {
88+
return "", "", err
89+
}
8090
psFile.Close()
8191

8292
return Execute(psFile.Name())

0 commit comments

Comments
 (0)