我一直在努力思考,但无法弄清楚为什么我的代码会失去完整性...
我有以下脚本,我正在尝试将其转换为Base64:
Function Get-Networks {
$Network = Get-WmiObject Win32_NetworkAdapterConfiguration | where { $_.MACAddress -notlike $null } | select Index, Description, IPAddress, DefaultIPGateway, MACAddress | Format-Table Index, Description, IPAddress, DefaultIPGateway, MACAddress
$WLANProfileNames = @()
$Output = netsh.exe wlan show profiles | Select-String -pattern " : "
Foreach ($WLANProfileName in $Output) {
$WLANProfileNames += (($WLANProfileName -split ":")[1]).Trim()
}
$WLANProfileObjects = @()
Foreach ($WLANProfileName in $WLANProfileNames) {
try {
$WLANProfilePassword = (((netsh.exe wlan show profiles name="$WLANProfileName" key=clear | select-string -Pattern "Key Content") -split ":")[1]).Trim()
}
Catch {
$WLANProfilePassword = "The password is not stored in this profile"
}
$WLANProfileObject = New-Object PSCustomobject
$WLANProfileObject | Add-Member -Type NoteProperty -Name "ProfileName" -Value $WLANProfileName
$WLANProfileObject | Add-Member -Type NoteProperty -Name "ProfilePassword" -Value $WLANProfilePassword
$WLANProfileObjects += $WLANProfileObject
Remove-Variable WLANProfileObject
return $WLANProfileObjects
}
}
$Networks = Get-Networks
echo $Networks
最初,我使用以下代码片段来完成它。它会将文件内容读取到一个变量中并进行转换。但我发现,当我将其转换回去后,输出的结果会把所有内容都放在一行上,导致格式混乱。
$Content = Get-Content -Path $FilePath
$converted = [convert]::ToBase64String([System.Text.encoding]::Unicode.GetBytes($Content))
所以我转而使用以下代码片段,使用文件路径转换它,而不是将内容保存到一个变量中。当我将其转换回来时,很高兴看到它保持了完整性。
[convert]::ToBase64String((Get-Content -path $FilePath -Encoding byte))
但当我试图执行它时,这次它抛出了以下错误,我感到更加困惑了。
畆据楴湯䜠瑥中瑥潷歲ൻ␊敎睴牯‽敇浗佩橢捥⁴楗㍮弲敎睴牯䅫慤瑰牥潃普杩牵瑡潩⁼桷牥⁻弤䴮䍁摁牤獥渭瑯楬敫␠畮汬素†⁼敳敬瑣䤠摮硥敄捳楲瑰潩Ɱ䤠䅐摤敲獳敄慦汵䥴䝐瑡睥祡䅍䅃摤敲獳簠䘠牯慭慔汢湉敤ⱸ䐠獥牣灩楴湯偉摁牤獥ⱳ䐠晥畡瑬 偉慇整慷ⱹ䴠䍁摁牤獥ഉ␊䱗乁牐景汩乥浡獥㴠⡀ഩ␊畏灴瑵㴠渠瑥桳攮數眠慬桳睯瀠潲楦敬⁼敓敬瑣匭牴湩瀭瑡整湲∠㨠∠潆敲捡⡨圤䅌偎潲楦敬慎敭椠伤瑵異⥴ൻ : The term
'畆据楴湯䜠瑥中瑥潷歲ൻ␊敎睴牯‽敇浗佩橢捥⁴楗㍮弲敎睴牯䅫慤瑰牥潃普杩牵瑡潩⁼桷牥⁻弤䴮䍁摁牤獥渭瑯楬敫␠畮汬素†⁼敳敬瑣䤠摮硥敄捳楲瑰潩Ɱ䤠䅐摤敲獳敄慦汵䥴䝐瑡睥祡䅍䅃摤敲獳簠䘠牯慭慔汢湉敤ⱸ䐠獥牣灩楴湯偉摁牤獥ⱳ䐠晥畡瑬偉慇整慷ⱹ䴠䍁摁牤獥ഉ␊䱗乁牐景汩乥浡獥㴠⡀ഩ␊畏灴瑵㴠渠瑥桳攮數眠慬桳睯瀠潲楦敬⁼敓敬瑣匭牴湩瀭瑡整湲∠㨠∠潆敲捡⡨圤䅌偎潲楦敬慎敭椠伤瑵異⥴ൻ' is not recognized
as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ 畆据楴湯䜠瑥中瑥潷歲ൻ␊敎睴牯‽敇浗佩橢捥⁴楗㍮弲敎睴牯䅫慤瑰牥潃普杩牵瑡潩⁼桷牥⁻弤䴮䍁摁牤獥渭瑯楬敫␠畮汬素†⁼敳敬瑣䤠摮 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (畆据楴湯䜠瑥中瑥潷歲ൻ␊敎睴...圤䅌偎潲楦敬慎敭椠伤瑵異⥴ൻ:String) [], CommandNotFoundException
我该如何编写一个简单的脚本将这个文件转换为base64格式,以便我可以正确地执行它?
Get-Content
命令中加入-Raw
开关。 - Santiago Squarzon