powershell - How to change the ps1 file in Unicode BigEndian to ASCII? -
our automated build script sign powershell scripts too. of our powershell scripts not signed . when analyzed , found there known gotcha files saved powershell ise saving in unicode bigendian can't signed.
as automated process, if way check whether file saved in unicode big endian change ascii solve our issue.
in powershell there way?
i found function here gets file encoding:
<# .synopsis gets file encoding. .description get-fileencoding function determines encoding looking @ byte order mark (bom). based on port of c# code http://www.west-wind.com/weblog/posts/197245.aspx .example get-childitem *.ps1 | select fullname, @{n='encoding';e={get-fileencoding $_.fullname}} | {$_.encoding -ne 'ascii'} command gets ps1 files in current directory encoding not ascii .example get-childitem *.ps1 | select fullname, @{n='encoding';e={get-fileencoding $_.fullname}} | {$_.encoding -ne 'ascii'} | foreach {(get-content $_.fullname) | set-content $_.fullname -encoding ascii} same previous example fixes encoding using set-content #> function get-fileencoding { [cmdletbinding()] param ( [parameter(mandatory = $true, valuefrompipelinebypropertyname = $true)] [string]$path ) [byte[]]$byte = get-content -encoding byte -readcount 4 -totalcount 4 -path $path if ( $byte[0] -eq 0xef -and $byte[1] -eq 0xbb -and $byte[2] -eq 0xbf ) { write-output 'utf8' } elseif ($byte[0] -eq 0xfe -and $byte[1] -eq 0xff) { write-output 'unicode' } elseif ($byte[0] -eq 0 -and $byte[1] -eq 0 -and $byte[2] -eq 0xfe -and $byte[3] -eq 0xff) { write-output 'utf32' } elseif ($byte[0] -eq 0x2b -and $byte[1] -eq 0x2f -and $byte[2] -eq 0x76) { write-output 'utf7'} else { write-output 'ascii' } }
and using this re-encode ascii:
if ((get-fileencoding -path $file) -ine "ascii") { [system.io.file]::readalltext($file) | out-file -filepath $file -encoding ascii }
Comments
Post a Comment