Powershell类型转换格式

时间:2018-02-19 16:00:33

标签: powershell

我已经编写了/ nabbed代码,用于迭代主机名列表,获取IP,然后如果用户在计算机上没有“活动”则关闭用户。迭代主机名并将IP列表放入数组列表非常有效:

[HttpPost]
public ActionResult SaveData(Data model)
{
   // to do : do something with model
   // to do : return something   
}

我打电话的时候:

$HostNames = "google.com","facebook.com","test.com" #Create an array with the hostnames.

$IPList = [System.Collections.ArrayList]@() #Create an empty array list so we can add/remove objects. 

for ($i=0;$i -lt $HostNames.Length; $i++){ #Loop through the hostnames.
    [System.Net.Dns]::GetHostAddresses($HostNames[$i]) | foreach {$IPList.Add($_.IPAddressToString) } #Get all the IPs for the host and add them to the array.
}

我得到了预期的结果:

echo $IPList

我直接包含主机名或IP时,我从https://stackoverflow.com/a/35848515/3718225窃取的代码也很有效,例如:

216.58.198.78
31.13.73.35
69.172.200.235

但是当我做的事情如下:

$sessions = qwinsta /server "localhost" | ?{ $_ -notmatch '^ SESSIONNAME' } | %{#Return information about the selected server | Using the previous command, do not return a row where anything  matches SESSIONNAME in caps
$item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device" #Select what information to return
#All the items below this line trim up the session info. 
$item.Active = $_.Substring(0,1) -match '>'
$item.SessionName = $_.Substring(1,18).Trim()
$item.Username = $_.Substring(19,20).Trim()
$item.Id = $_.Substring(39,9).Trim()
$item.State = $_.Substring(48,8).Trim()
$item.Type = $_.Substring(56,12).Trim()
$item.Device = $_.Substring(68).Trim()
$item
}

我收到错误:

$HostNames = "google.com","facebook.com","test.com" #Create an array with the hostnames.

$IPList = [System.Collections.ArrayList]@() #Create an empty array list so we can add/remove objects. 

for ($i=0;$i -lt $HostNames.Length; $i++){ #Loop through the hostnames.
    [System.Net.Dns]::GetHostAddresses($HostNames[$i]) | foreach {$IPList.Add($_.IPAddressToString) } #Get all the IPs for the host and add them to the array.
}

for ($i=0;$i -lt $IPList.Length; $i++){ #For all of the IP's in the array. 

    $sessions = qwinsta /server $IPList[$i]| ?{ $_ -notmatch '^ SESSIONNAME' } | %{#Return information about the selected server | Using the previous command, do not return a row where anything  matches SESSIONNAME in caps
    $item = "" | Select "Active", "SessionName", "Username", "Id", "State", "Type", "Device" #Select what information to return
    #All the items below this line trim up the session info. 
    $item.Active = $_.Substring(0,1) -match '>'
    $item.SessionName = $_.Substring(1,18).Trim()
    $item.Username = $_.Substring(19,20).Trim()
    $item.Id = $_.Substring(39,9).Trim()
    $item.State = $_.Substring(48,8).Trim()
    $item.Type = $_.Substring(56,12).Trim()
    $item.Device = $_.Substring(68).Trim()
    $item
    }

    foreach ($session in $sessions){ #For all the sessions
        if (($session.Username -ne "" -or $session.Username.Length -gt 1) -and ($session.State -eq 'Active')){ #So long as the session name is longer then 1 and not blank AND the session is not 'active'
            #logoff /server $IPList $session.Id
            Write-Host $IPList[$i] $session.Username $session.State #Disconnect the user. 
   }
  }
}   

我想我需要找到一种方法将ArrayList从System.Object转换为字符串。但是,像:

Could not compare "0" to "13 11 14". Error: "Cannot convert the "System.Object[]" value of type "System.Object[]" to
type "System.Int32"."
At line:1 char:11
+ for ($i=0;$i -lt $IPList.Length; $i++){ #For all of the IP's in the a ...
+           ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : ComparisonFailure

似乎不起作用/成为解决方案。为什么?转换它的最佳方法是什么?我不确定我理解为什么$ ArrayList将字符串存储为对象,而不是字符串。

1 个答案:

答案 0 :(得分:2)

您的循环正在迭代错误数量的项目,以获取ArrayList或任何其他数组的项目计数,请使用Count方法,请参阅ArrayList.Count

$IPList.Length将显示数组中每个项目的总字符长度,而$IPList.Count将为您提供数组中的总项目。

所以你的Could not compare "0" to "13 11 14".错误是:

google.com -> IP -> 216.58.206.78 Length -> 13
facebook.com -> IP -> 31.13.73.35 Length -> 11
test.com -> IP -> 69.172.200.235 Length -> 14

替换此行中length中的Count

for ($i=0;$i -lt $IPList.Length; $i++)

对此:

for ($i=0;$i -lt $IPList.Count; $i++)