简体   繁体   English

如何在Powershell中合并和重命名数组的属性?

[英]How to combine and rename properties of arrays in powershell?

I am pulling data from an API that ConnectWise offers. 我正在从ConnectWise提供的API中提取数据。 I am trying to pull a list of usernames and password and put them in to a format which I can either reference in another script (or combiner with this one), or that I can export to a CSV. 我正在尝试提取用户名和密码的列表,并将其设置为一种格式,可以在另一个脚本(或与此脚本的组合器)中引用该格式,也可以将其导出为CSV。 I'd prefer the first if possible. 如果可能的话,我希望第一个。

The code: 编码:

Function Get-CWConfiguration
{
[string]$BaseUri     = "$CWServerRoot"
[string]$Accept      = "application/vnd.connectwise.com+json; version=v2015_3"
[string]$ContentType = "application/json"
[string]$Authstring  = $CWInfo + '+' + $CWCredentials1 + ':' + $CWCredentials2 
$encodedAuth         = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(($Authstring)));

$Headers=@{"Authorization"="Basic $encodedAuth"}
Invoke-RestMethod -URI $BaseURI -Headers $Headers -ContentType $ContentType -Method Get
}

$username = (Get-CWConfiguration).questions | Where-Object {($_.questionid -eq 419)} | Sort-Object -Property questionid | select answer
$password = (Get-CWConfiguration).questions | Where-Object {($_.questionid -eq 420)} | Sort-Object -Property questionid | select answer

Everything above the $username variable is the stuff connecting to the API and I did not modify that. $username变量上方的所有内容都是连接到API的内容,我没有进行任何修改。 The code below is the best way I found to pull the info that I need. 下面的代码是我发现提取所需信息的最佳方法。 This is returning each variable as an array, which is fine but if that happens I need to have an array with 2 columns. 这会将每个变量作为数组返回,这很好,但是如果发生这种情况,我需要有一个包含2列的数组。 1 named Username and the other named Password. 1个名为“用户名”,另一个名为“密码”。 if I do a $username | Get-Member 如果我执行$username | Get-Member $username | Get-Member it returns: $username | Get-Member返回:

Name        MemberType   Definition                         
----        ----------   ----------                         
Equals      Method       bool Equals(System.Object obj)     
GetHashCode Method       int GetHashCode()                  
GetType     Method       type GetType()                     
ToString    Method       string ToString()                  
answer      NoteProperty string answer=admin@admin.com

My end goal is to have a variable like $credentials output something that displays like: 我的最终目标是让类似$credentials的变量输出如下所示的内容:

Username             Password
--------            ----------                         
admin1@admin.com      Password1
admin2@admin.com      Password2
admin3@admin.com      Password3

Most methods I have tried have resulted in everything put output to 1 line or 1 column. 我尝试过的大多数方法都将所有内容输出到1行或1列。 I'm not sure if I should be combining the arrays and renaming the $username.answer and $password.answer fields or creating a PSObject ? 我不确定是否应该合并数组并重命名$username.answer$password.answer字段或创建PSObject My main difficulty has been that both variables have that same answer NoteProperty 我的主要困难是,这两个变量有相同answer NoteProperty

You'd want to create new objects with the expanded properties of the current objects. 您想使用当前对象的扩展属性创建新对象。 This could all look something like this (note that I'm manually creating analogs for what you're all dealing with and ignoring the string manipulation you'll likely have to do) 这可能看起来像这样(请注意,我正在为您正在处理的内容手动创建类似物,而忽略了可能要做的字符串操作)

$Username = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:Hithisispatrick"}
$Password = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:HelloThisisSquidward"}

$UsernameAnswer = $Username | Select -ExpandProperty Answer
$PasswordAnswer = $Password | Select -ExpandProperty Answer


$Final = New-Object -TypeName pscustomobject -Property @{UserName= $UsernameAnswer ; Password = $PAsswordAnswer}

This should give you output something like this: 这应该使您输出如下内容:

UserName               Password
--------               --------
Answer:Hithisispatrick Answer:HelloThisisSquidward

Then if you want to parameterize it/Expand it, you should be able then to toss it in to a foreach loop and have it output the Custom Object, setting that equal to an array that then builds out of those objects, like so(I'll use while to make it simpler to show): 然后,如果要对其进行参数化/扩展,则应该能够将其抛入foreach循环并使其输出自定义对象,将其设置为等于然后从这些对象中构建的数组,例如so(I将使用while使其更易于显示):

$Count = 0
$Username = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:Hithisispatrick"}
$Password = New-Object -TypeName pscustomobject -Property @{Answer = "Answer:HelloThisisSquidward"}
$Array = While ($Count -lt 5){
$UsernameAnswer = $Username | Select -ExpandProperty Answer
$PAsswordAnswer = $Password | Select -ExpandProperty Answer

$Final = New-Object -TypeName pscustomobject -Property @{UserName= $UsernameAnswer ; Password = $PAsswordAnswer}
$Final
$Count += 1
}


$Array

This produces something like this: 这将产生如下内容:

Password                    UserName
--------                    --------
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick
Answer:HelloThisisSquidward Answer:Hithisispatrick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM