简体   繁体   English

如何使用nodejs执行使每个离线磁盘联机的powershell脚本

[英]How to execute powershell script that online every offline disk using nodejs

Writing the following directly in the powershell will online every offline disk found in the system直接在powershell中写入以下内容将使系统中找到的每个脱机磁盘联机

Get-Disk | Where-Object IsOffline –Eq $True | Set-Disk –IsOffline $False

However, the problem Is when I try to make it to a script as onlinedisktest.ps1 I will get the following error message.但是,问题是当我尝试将其作为 onlinedisktest.ps1 制作成脚本时,我将收到以下错误消息。

Where-Object : A positional parameter cannot be found that accepts argument 'False'.
At C:\Users\me\Desktop\project-main\new\scripts\onlinedisktest.ps1:1 char:12
+ Get-Disk | Where-Object IsOffline –Eq $True | Set-Disk –IsOffline ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Where-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WhereObjectCommand

My code我的代码

static async run(executable, args, opts = {}) {
        return new Promise((resolve, reject) => {
            const child = spawn(executable, args, {
                shell: true,
                stdio: ["pipe", process.stdout, process.stderr],
                ...opts,
            });
            child.on("error", reject);
            child.on("exit", (code) => {
                if (code === 0) {
                    resolve(code);
                } else {
                    const e = new Error('Process exited with error code ' + code);
                    e.code = code;
                    reject(e);
                }
            });
        });
}
      
static async exeOnlineDisk () {

        try {
            const code = await this.run('powershell', ["-executionpolicy", "unrestricted", "-file", __dirname + "\\scripts\\onlinedisktest.ps1"]);
            if (code == 0) {
                //success
            }
        } catch (e) {
            console.error(e);
            process.exit(e.code || 1);
        }
}

Check the encoding of your file.检查文件的编码。 Try saving the script you're calling as UTF 8.尝试将您调用的脚本保存为 UTF 8。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM