powershell PowerShell自定义对象示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell自定义对象示例相关的知识,希望对你有一定的参考价值。
#Jake Morrison - @jakemorrison - http://techthoughts.info
$results = [PSCustomObject]@{
HypTotal = $hyptotal
MgmtVMTotal = $mgmtVMTotal
UniqueCustomerCount = $customerTotal
TotalComputerObjects = $allCompsTotal
TotalUserObjects = $userTotal
}
[PSCustomObject]@{
User1 = Get-Credential -Message User1
User2 = Get-Credential -Message User2
} | Export-Clixml -Path $Path
#-------------------------------------------------------------------------------
#a few working examples of creating custom PSObjects that contain multiple values:
#example 1 - using noteProperty
$results = @()
foreach ($user in $users) {
$a = $null
$a = Get-UserInfo -userName $user
$obj = New-Object System.Object
$obj | Add-Member -type NoteProperty -Name UserName -Value $a.UserName
$obj | Add-Member -type NoteProperty -Name Name -Value $a.Name
$obj | Add-Member -type NoteProperty -Name Title -Value $a.Title
$results += $obj
}#foreach_user
#example 2 - using new PSObject
foreach ($user in $users) {
$a = $null
$a = Get-ANTInfo -userName $user
$obj = New-Object PSObject -Property ([ordered]@{
UserName = $a.UserName
Name = $a.Name
Title = $a.Title
})#psobject
$results += $obj
}#foreach_user
#xample 3 - using new PSObject
$results = @()
$vms = get-vm
foreach($vm in $vms){
$name = $vm | Select-Object -ExpandProperty Name
$cpu = $vm | Select-Object -ExpandProperty ProcessorCount
$dyanamic = $vm | Select-Object -ExpandProperty DynamicMemoryEnabled
$memMin = [math]::round($vm.MemoryMinimum / 1MB, 0)
$memMax = [math]::round($vm.MemoryMaximum / 1GB, 0)
$TestObj = New-Object PSObject -Property @{
Name = $name
CPU = $cpu
DynamicMemoryEnabled = $dyanamic
MemoryMinimum = $memMin
MemoryMaximum = $memMax
}
$results += $TestObj
}
#------------------------------------------------------------------------------
#new basic custom ordered object
$stock = New-Object PSObject -Property ([ordered]@{
"FQDN" = "-----"
"WhenCreated" = $created
"Status" = "-----"
})
#------------------------------------------------------------------------------
#array to object
$resultsArray += @{
"Name" = $nameProperty;
"Present" = $evalProperty;
"Contents" = $contentsProperty
}
$a = $resultsArray | ForEach-Object { new-object PSObject -Property $_}
$output = $a | Select-Object Name, Present, Contents | Sort-Object Name
#------------------------------------------------------------------------------
#hash to object
$info = @{}
$info.BIOSVersion = Get-WmiObject Win32_BIOS | Select-Object -ExpandProperty Version
$info.OperatingSystemVersion = Get-WmiObject win32_OperatingSystem | Select-Object -ExpandProperty Version
$info.PowerShellVersion = $PSVersionTable.psversion.ToString()
New-Object PSObject -property $info
#------------------------------------------------------------------------------
#based on code from: http://community.idera.com/powershell/powertips/b/tips/posts/turning-ad-user-into-a-hash-table
#------------------------------------------------------------------------------
#Object to hash
$blacklist = "ServiceHandle", "Container", "Site"
$services = Get-Service -Name BITS
$serviceManip = $services | Get-Member -MemberType *property | Select-Object -ExpandProperty Name
$newHash = [Ordered]@{}
$serviceManip |
Sort-Object |
Where-Object {
$_ -notin $blacklist
} |
ForEach-Object {
$newHash[$_] = $services.$_
}
#------------------------------------------------------------------------------
#multiple objects to hash
$blacklist = "ServiceHandle", "Container", "Site"
$services = Get-Service
$serviceManip = $services | Get-Member -MemberType *property | Select-Object -ExpandProperty Name
$newHash = [Ordered]@{}
$serviceManip |
Sort-Object |
Where-Object {
$_ -notin $blacklist
} |
ForEach-Object {
$newHash[$_] += $services.$_
}
#------------------------------------------------------------------------------
$results = @()
$properties = @{}
$properties.Add("Subnet-$i", $subnet.SubnetId)
$properties.Add("CIDR-$i", $subnet.CidrBlock)
$results += [PSCustomObject]$properties
#------------------------------------------------------------------------------
以上是关于powershell PowerShell自定义对象示例的主要内容,如果未能解决你的问题,请参考以下文章
powershell powershell获取函数的定义。我用它来获取内部自定义函数和自定义模块的定义
PowerShell 添加 自定义的ScriptProperty 属性
powershell PowerShell:Symantec Management Platform Internet Explorer自定义清单