我正在嘗試能夠使用 node-powershell 與 powershell 互動。
Powershell 輸出通常是可以存盤在變數中的資料幀型別物件。我只是不清楚如何將輸出回傳到 JS 中以存盤/顯示它。
我想我需要能夠讀取標準輸出,但我不知道如何做到這一點并以格式化的方式保存資料。我希望在物件中回傳資料。
在我的代碼中,我得到了一個未決的承諾,我不明白為什么。
const { PowerShell } = require('node-powershell')
const poshInstance = async () => {
const ps = new PowerShell({
executionPolicy: 'Bypass',
noProfile: true,
})
const command = PowerShell.command`Get-LocalGroup`
const output = await ps.invoke(command)
//const output = await PowerShell.$`Get-LocalGroup`
ps.dispose()
const stringFromBytes = String.fromCharCode(...output.stdout)
// console.log(stringFromBytes)
return JSON.stringify(stringFromBytes)
}
const op = poshInstance()
console.log(op)
uj5u.com熱心網友回復:
這個庫接縫回傳命令的原始文本輸出。要從此創建物件,最簡單的解決方案是在 powershell 會話中將結果序列化為 JSON 格式。然后你只需要決議輸出。
const { PowerShell } = require('node-powershell')
const poshInstance = async () => {
const ps = new PowerShell({
executionPolicy: 'Bypass',
noProfile: true
})
const command = PowerShell.command`Get-LocalGroup | ConvertTo-Json`
const output = await ps.invoke(command)
ps.dispose()
console.log(output)
const result = JSON.parse(output.raw)
console.log(result)
}
(async () =>
{
await poshInstance()
})();
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457169.html
標籤:javascript 节点.js 电源外壳 目的
上一篇:通過鍵和值在物件陣列中查找物件
