Board logo

标题: [原创代码] 分享一个PowerShell随机出列的类 [打印本页]

作者: Nsqs    时间: 2023-10-12 08:52     标题: 分享一个PowerShell随机出列的类

  1. class RandomListClass{
  2.     static AddListMember([ref]$InputObject){
  3.         if($InputObject.Value -is [System.Collections.iDictionary] -or $InputObject.Value -is [Hashtable]){
  4.             $InputObject.Value|Add-Member -MemberType ScriptProperty -Name RandomKeys -Force -Value {
  5.                 $this.Keys|Get-Random -Count $this.Count
  6.             }
  7.             $InputObject.Value|Add-Member -MemberType ScriptProperty -Name RandomValues -Force -Value {
  8.                 $this.Values|Get-Random -Count $this.Count
  9.             }
  10.             $InputObject.Value|Add-Member -MemberType ScriptProperty -Name Dequeue -Force -Value {
  11.                 if($this.Count){
  12.                     $Keys=$this.Keys|Get-Random
  13.                     $this.Remove($Keys)|Out-Null
  14.                     $Keys
  15.                 }
  16.             }
  17.         }
  18.     }
  19. }
  20. $RandomList=[System.Collections.Generic.Dictionary[string,string]]@{}
  21. [RandomListClass]::AddListMember($RandomList)
  22. 1..10|%{
  23.     $RandomList.Add($_,$_)
  24. }
  25. $RandomList.Dequeue
复制代码
不过不知为什么换List添加不了成员对象,重载不了此类,但是字典和哈希却可以?




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2