您好,登錄后才能下訂單哦!
今天豆子需要遠程注銷一個終端服務器的用戶,操作很簡單,首先quser獲取遠程用戶的sessionID,然后logoff命令注銷即可
quser /server:XXXX logoff sessionID /server:XXX
和早期的cmd和Linux的bash命令一樣,quser的結果都是字符串。字符串的處理對于腳本來說比較麻煩,如果能夠轉換成標準的object對象就好了。
仔細觀察一下quser的結果,可以看見他的輸出結果很整齊,我如果能夠把他中間的空格用逗號替換掉,那就是標準的csv格式,那就很容易轉換為對象了。唯一的問題是有的列,比如sessionname為空,那我就需要數數到底多少個空格需要替換了
PS C:\users\yli\Documents> $a=quser /server:sydbcc02 PS C:\users\yli\Documents> $a USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME smiths rdp-tcp#5 16 Active 24692+13:29 3/14/2017 9:06 AM llederbauer rdp-tcp#4 22 Active 1:18 3/14/2017 9:18 AM jedwards 23 Disc 1:39 3/14/2017 7:54 AM tpicken rdp-tcp#2 24 Active . 3/14/2017 8:22 AM okaragiaouris rdp-tcp#9 27 Active 18 3/14/2017 8:55 AM bhue rdp-tcp#7 28 Active 12 3/14/2017 9:20 AM mcherginets 29 Disc 1:37 3/14/2017 8:58 AM abishop 30 Disc 2:48 3/14/2017 9:22 AM fburns 31 Disc 2:28 3/14/2017 9:37 AM ygordony 32 Disc 2:07 3/14/2017 10:04 AM tmoloney rdp-tcp#0 33 Active 1:07 3/14/2017 9:30 AM smaroo rdp-tcp#1 34 Active 1:45 3/14/2017 10:19 AM jwoodford 35 Disc 22 3/14/2017 10:42 AM mozard rdp-tcp#8 36 Active 56 3/14/2017 11:08 AM gmarsh rdp-tcp#6 37 Active 41 3/14/2017 10:56 AM mking 38 Disc 20 3/14/2017 11:11 AM mcolgan rdp-tcp#11 39 Active 59 3/14/2017 11:16 AM jbelgiovane rdp-tcp#12 41 Active 39 3/14/2017 11:33 AM ndrabsch 42 Disc 3 3/14/2017 11:57 AM
直接看不太容易區分,轉換成ASCII看看,可以看見32(代表空格)一共有16個
$a=quser /server:sydbcc02 [int[]][char[]]$a[2] 32 108 108 101 100 101 114 98 97 117 101 114 32 32 32 32 32 32 32 32 32 32 32 114 100 112
okay,那我指定數量從2到17之間的空格都替換為逗號,從2開始是因為‘idle time’這些名字中間有空格就別替換了,最后代碼如下
$quser = (quser /server:sydbcc02) -replace '\s{2,17}', ',' | ConvertFrom-Csv $quser | ft USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME -------- ----------- -- ----- --------- ---------- smiths 16 Disc 3 3/14/2017 9:06 AM llederbauer rdp-tcp#4 22 Active 1:26 3/14/2017 9:18 AM jedwards 23 Disc 1:47 3/14/2017 7:54 AM tpicken rdp-tcp#2 24 Active 7 3/14/2017 8:22 AM okaragiaouris rdp-tcp#9 27 Active . 3/14/2017 8:55 AM bhue rdp-tcp#7 28 Active 20 3/14/2017 9:20 AM mcherginets 29 Disc 1:44 3/14/2017 8:58 AM abishop 30 Disc 2:56 3/14/2017 9:22 AM fburns 31 Disc 2:36 3/14/2017 9:37 AM ygordony 32 Disc 2:15 3/14/2017 10:04 AM tmoloney rdp-tcp#0 33 Active 1:15 3/14/2017 9:30 AM smaroo rdp-tcp#1 34 Active 1:52 3/14/2017 10:19 AM jwoodford 35 Disc 30 3/14/2017 10:42 AM mozard rdp-tcp#8 36 Active . 3/14/2017 11:08 AM gmarsh rdp-tcp#6 37 Active 48 3/14/2017 10:56 AM mking 38 Disc 27 mcolgan 39 Disc . 3/14/2017 11:16 AM jbelgiovane rdp-tcp#12 41 Active 47 3/14/2017 11:33 AM ndrabsch rdp-tcp#3 42 Active . 3/14/2017 11:57 AM alawton 43 Disc 11 3/14/2017 12:02 PM
這樣子就可以很容易的進一步處理了,同樣的思路也可以用在其他的cmd命令上。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。