Windows应用-C#使用命令行执行PowerShell脚本

17 篇文章 0 订阅
前言

类似于bat脚本,能够自动执行一些任务,但是对bat不熟悉,因此选择使用C#来实现,具体是能够通过执行特定的语句实现对文件的读写与执行

代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace CrackDown_BIOS
{
    class Program
    {
        static async Task Main(string[] args)
        {
            //Console.WriteLine("Hello World!");
            try
            {
                var error = true;
                var output = "";
                var outerror = "";
                string path = "";

                var passwords = ReadFromLine(path,0);
                var count = 0;
                
                foreach (var word in passwords)
                {
                    
                    string password = word;
                    string strCMD = "& " + "\"" + @"C:\Program Files (x86)\HP\BIOS Configuration Utility\HPQPswd.exe" + "\"  " + "/s /p" + "\"" + password + "\"" + " /f" + "\"" + "sample password.bin" + "\" exit";
                    //向cmd窗口发送输入信息
                    Console.WriteLine(strCMD);
                    //p.StandardInput.WriteLine(strCMD + "");
                    //p.StandardInput.AutoFlush = true;

                    //Thread.Sleep(1);

                    string crackCMD = "& " + "\"" + @"C:\Program Files (x86)\HP\BIOS Configuration Utility\BiosConfigUtility.exe" + "\"  " + "/nspwdfile:" + "\"" + "\"" + " /cspwdfile:" + "\"" + "sample password.bin" + "\" exit";
                    Console.WriteLine(crackCMD);


                    if (!error)
                    {
                        //Console.WriteLine("CURRENT IS: " + word);
                    }
                    else
                    {
                        Console.WriteLine("PASSWORD IS: " + word);
                    }
                    outerror = "";
                    output = "";
                    count++;
                }

                count++;
               
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n跟踪;" + ex.StackTrace);
            }
        }

        private string GenerateWord()
        {
            try {
                string path = "xxxx";
            }
            catch (Exception e) {
            }
            return "";
        }

        private static List<string> ReadFromLine(string fullPath, int lineNum)
        {
            int i = 0;
            var content = new List<string>();
            FileStream fs = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default))
            {
               
                var current = string.Empty;
                while ((current = sr.ReadLine())!=null) {
                    i++;
                    content.Add(current);
                    if (i != 0)
                    {
                        lineNum = i;
                        // Console.WriteLine("LineNum:" + lineNum + " Content: " + current);
                    }
                }
            }

            return content;
        }
    }
    }

参考链接

Process.StandardError 属性
C#程序调用CMD执行命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值