I am trying to pass args from powershell to js,and using iconv to decode the string,but afterward , i need to pass it back,but when passing back it become gibberish,so how can i decode it in powershell?
const { exec} = require('node:child_process')
const os = require("os");
const iconv = require('iconv-lite');
const encoding = os.platform() == 'win32' ? 'cp936' : 'utf-8';
const binaryEncoding = 'binary';
// 执行命令并获取结果
const { argv } = require('node:process');
argv.forEach((args) => {
const runExec = (powershell) => {
return new Promise((resolve, reject) => {
exec(powershell, { encoding: binaryEncoding }, function(error, stdout, stderr) {
if (error) {
reject(iconv.decode(new Buffer.from(error.message, binaryEncoding), encoding));
} else {
resolve(iconv.decode(new Buffer.from(stdout, binaryEncoding), encoding));
}
});
})
}
var url = process.argv.slice(2);
return new Promise((resolve, reject) => {
runExec(`"POWERSHELL" C:\\Users\\Administrator\\dre\\dre-main\\crawl1.ps1 ${url}`,{ encodingback: binaryEncoding }).then(res => {
const jsdom = require("jsdom");
const dom = new jsdom.JSDOM(res)
console.log(dom.window.document.title)
}).catch(err => {
console.log('执行失败', err);
})})
});
function url {
[CmdletBinding()]
param (
[Parameter()]
[string]
$url
)
function get-title {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$title
)
$title
}
try {
*$title = node "C:\Users\Administrator\dre\dre\GETtitle.js" $url*
$title=$title[1]
$Destination = "C:\Users\Administrator\favorites\$title.lnk"
$sourcepath =$url
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Destination")
$Shortcut.TargetPath = "$sourcepath"
$Shortcut.save()
}
catch {
$title = get-title
$Destination = "C:\Users\Administrator\favorites\$title.lnk"
$sourcepath =$url
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Destination")
$Shortcut.TargetPath = "$sourcepath"
$Shortcut.save()
}
}