Powershell& C#检查主机是否可用 [英] Powershell & C# to check if host available

查看:96
本文介绍了Powershell& C#检查主机是否可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个c#代码段来处理power-shell脚本。以下是代码段。

Hi ALL,

I have a c# code segment which process a power-shell script. The following is the code segment.

Runspace objRunspace = null;
objRunspace = RunspaceFactory.CreateRunspace();
objRunspace.Open();
PowerShell powershell = PowerShell.Create();
powershell.Runspace = objRunspace;
powershell.AddScript("my powershell script");
Collection<PSObject> objPS1 = powershell.Invoke();
if (objPS1.Count == 0)
   {
    //I Assumes that the PC is down
   }



在这种情况下,如果登录凭据错误或者该特定PC不可用; ObjPS1.count将为零。如果该计数为零,则假定主机不可用。但从准确的角度来看,我想确定这些(错误的凭证/主机不可用)情况是分开的。是否有更好的方法或不同的方法来实现相同的目标。



提前致谢

Sebastian


In this case, If the login credentials are wrong or if that particular PC is not available; the ObjPS1.count will be zero. If that count is Zero, I assumes that the host is unavailable. But in accuracy point of view, I want to identify both these (wrong credentials / host unavailable) situations separate. Is there a better way or different approach to achieve the same.

Thanks in advance
Sebastian

推荐答案

这篇关于Powershell&amp; C#检查主机是否可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆