Skip to content

Commit

Permalink
windows: set text encodig for powershell to utf-8
Browse files Browse the repository at this point in the history
by default powershell uses the OEM-US/codepage 437
encoding which is an extension of ASCII

when a character with umlaut is used and powershell
uses the default encoding go is unable to  properly
convert it to a go string as it doesn't  understand
the OEM-US encoding by default and replaces it with
'U+FFFD' and we get a ? in the output

the preflight check, checking if  username in group
fails since the username is not correct because  of
the '?' character in it, this fixes it by using the
utf-8 encoding for powershell
  • Loading branch information
anjannath committed Jul 4, 2024
1 parent 22a112d commit 08ed099
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/os/windows/powershell/powershell_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func Execute(args ...string) (string, string, error) {
if err != nil {
return "", "", err
}
args = append([]string{"-NoProfile", "-NonInteractive", "-ExecutionPolicy", "RemoteSigned", "-Command", "$ProgressPreference = 'SilentlyContinue';"}, args...)
args = append([]string{"-NoProfile", "-NonInteractive", "-ExecutionPolicy", "RemoteSigned", "-Command",
"$ProgressPreference = 'SilentlyContinue';", "[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new();"}, args...)
cmd := exec.Command(powershell, args...) // #nosec G204
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}

Expand Down Expand Up @@ -85,6 +86,7 @@ func ExecuteAsAdmin(reason, cmd string) (string, string, error) {

func runAsCmds(powershell string) []string {
return []string{
`[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new();`,
`$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();`,
`$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);`,
`$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;`,
Expand Down

0 comments on commit 08ed099

Please sign in to comment.