gpt4 book ai didi

c# - 在给定 IP 范围内的机器上远程执行 Powershell 脚本

转载 作者:太空宇宙 更新时间:2023-11-03 13:11:46 25 4
gpt4 key购买 nike

我基本上是在尝试创建一个控制台应用程序,它在给定 IP 范围内的远程计算机上执行给定脚本并存储结果。

到目前为止,这是我的代码,但是当我尝试使用 WSManConnectionInfo 对象作为 arg 创建运行空间时出现了问题。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Security;

namespace GetSysInfo
{
class Program
{
private static string outPath;

static void Main(string[] args)
{
//get script path
Console.WriteLine("Enter full path to script.");
string path = Convert.ToString(Console.ReadLine());

//get IP range
Console.WriteLine("Input start IPv4.");
IPAddress sIP = IPAddress.Parse(Console.ReadLine().ToString());

Console.WriteLine("Input end IPv4.");
IPAddress eIP = IPAddress.Parse(Console.ReadLine().ToString());

//get list of IPs in range
RangeFinder rf = new RangeFinder();
List<string> IPrange = rf.GetIPRangeList(sIP, eIP);

//run script
foreach (var IP in IPrange)
{
try
{
RunScriptRemote(LoadScript(path), IP);
}
catch (Exception e)
{
Console.WriteLine("An error occured" + Environment.NewLine + e.Message);
}
}
}

//script executer
private static void RunScriptRemote(string script, string address)
{
Console.WriteLine("Enter username.");
String username = Console.ReadLine();

Console.WriteLine("Enter password.");

ConsoleKeyInfo key;
SecureString pass = new SecureString();

do
{
key = Console.ReadKey(true);

if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
pass.AppendChar(key.KeyChar);
Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && pass.Length > 0)
{
pass.RemoveAt(pass.Length);
Console.Write("\b \b");
}
else if (key.Key == ConsoleKey.Enter && pass.Length > 0)
{
Console.Write(Environment.NewLine);
pass.MakeReadOnly();
}
}
}
while (key.Key != ConsoleKey.Enter);


PSCredential credential = new PSCredential(username, pass);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://" + address + ":5985/wsman"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Negotiate;
connectionInfo.EnableNetworkAccess = true;

Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);//point of crash
runspace.Open();

//set path to save results
Console.WriteLine("Enter full path to save results. Must be a directory.\nThis can be a local path or a network path.");
Console.WriteLine("In case of a network path the results will be merged automatically");
outPath = Convert.ToString(Console.ReadLine());

runspace.SessionStateProxy.SetVariable("filepath", outPath);

using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddScript(script);
ps.Invoke();
}

//Pipeline pipeline = runspace.CreatePipeline();
//pipeline.Commands.AddScript(script);

//pipeline.Invoke();

runspace.Close();
}

//script loader
private static string LoadScript(string filename)
{
try
{
using (StreamReader sr = new StreamReader(filename))
{
StringBuilder fileContents = new StringBuilder();

string curLine;

while ((curLine = sr.ReadLine()) != null)
{
fileContents.Append(curLine + Environment.NewLine);
}

return fileContents.ToString();
}
}
catch (Exception e)
{
return e.Message;
}

}
}

public class RangeFinder
{
public IEnumerable<string> GetIPRange(IPAddress startIP,
IPAddress endIP)
{
uint sIP = ipToUint(startIP.GetAddressBytes());
uint eIP = ipToUint(endIP.GetAddressBytes());
while (sIP <= eIP)
{
yield return new IPAddress(reverseBytesArray(sIP)).ToString();
sIP++;
}
}

public List<string> GetIPRangeList(IPAddress startIP,
IPAddress endIP)
{
uint sIP = ipToUint(startIP.GetAddressBytes());
uint eIP = ipToUint(endIP.GetAddressBytes());
List<string> IPlist = new List<string>();
while (sIP <= eIP)
{
IPlist.Add(new IPAddress(reverseBytesArray(sIP)).ToString());
sIP++;
}

return IPlist;
}


//reverse byte order in array
protected uint reverseBytesArray(uint ip)
{
byte[] bytes = BitConverter.GetBytes(ip);
bytes = bytes.Reverse().ToArray();
return (uint)BitConverter.ToInt32(bytes, 0);
}


//Convert bytes array to 32 bit long value
protected uint ipToUint(byte[] ipBytes)
{
ByteConverter bConvert = new ByteConverter();
uint ipUint = 0;

int shift = 24;
foreach (byte b in ipBytes)
{
if (ipUint == 0)
{
ipUint = (uint)bConvert.ConvertTo(b, typeof(uint)) << shift;
shift -= 8;
continue;
}

if (shift >= 8)
ipUint += (uint)bConvert.ConvertTo(b, typeof(uint)) << shift;
else
ipUint += (uint)bConvert.ConvertTo(b, typeof(uint));

shift -= 8;
}

return ipUint;
}
}
}

尝试运行时出现以下错误:公共(public)语言运行时检测到无效程序。

我尝试按照 Google 的建议创建一个新的解决方案,但在创建运行空间时显然出错了。

我没有想法和资源来寻求帮助,所以我正在联系 Stackoverflow 社区。

提前致谢,X3ntr

编辑:我尝试清理我的解决方案、手动删除任何 pbd、更改 objective-c PU、关闭代码优化、允许不安全代码、重建、创建新解决方案,...按照 this 中的建议发布。

最佳答案

我听从了 this answer 的建议:我添加了对 C:\windows\assembly\GAC_MSIL\System.Management.Automation 的引用,然后在提升的 powershell 中运行此命令:Copy ([PSObject].Assembly.Location) C:\

关于c# - 在给定 IP 范围内的机器上远程执行 Powershell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28280465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com