gpt4 book ai didi

arrays - 在PowerShell中到数组的字符串(无定界符)

转载 作者:行者123 更新时间:2023-12-03 00:28:40 25 4
gpt4 key购买 nike

我正在尝试使用WMI使用以下命令获取计算机登录 session 的开始时间:

$starttimes = Get-WmiObject Win32_LogonSession -ComputerName HM-ITS-KLP |
select starttime

这给了我日期格式为:
20170120075444.819609+000 (yyyymmddhhmmss.??????+???)

Using the String.ToCharArray() method I managed to convert a string to an array so that I could restructure it better, although in this format it will not accept this as:

Method invocation failed because [Selected.System.Management.ManagementObject] doesn't contain a method named 'ToCharArray'.

Whole code as follows:

$starttimes = Get-WmiObject Win32_LogonSession -ComputerName HM-ITS-KLP |
select StartTime
foreach ($line in $starttimes) {
$dateArray = $line.ToCharArray()
$time = $dateArray[8..9] + ":" + $dateArray[10..11] + ":" + $dateArray[12..13]
$date = $dateArray[6..7] + "/" + $dateArray[4..5] + "/" + $dateArray[0..3]
$LoginTimeAndDate1 = $time + " " + $date
$LoginTimeAndDate = $LoginTimeAndDate1 -join ""
}

最佳答案

您忘记扩展“启动时间”属性。尝试更改为此:

foreach ( $line in $starttimes){
$dateArray = $line.starttime.toCharArray()

或这个:
foreach ( $line in $starttimes.starttime){
$dateArray = $line.toCharArray()

或这个:
$starttimes = Get-WMIObject Win32_LogonSession |  select -Expand starttime
foreach ( $line in $starttimes){

关于arrays - 在PowerShell中到数组的字符串(无定界符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41808318/

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